一、填空题
1、 Java Database Connectivity
2、 java.sql.*
3、 classpath
4、 registerDriver()
5、 next()
二、判断题
1、对 2、错 3、对 4、错 5、错
三、选择题
1、A 2、C 3、A 4、D 5、AC
四、简答题
1、JDBC是一套用于执行SQL语句的Java API。应用程序可通过这套API连接到关系型数据库,并使用SQL语句来完成对数据库中数据的查询、新增、更新和删除等操作。
2、(1)加载数据库驱动
(2)通过DriverManager获取数据库连接
(3)通过Connection对象获取Statement对象
(4)使用Statement执行SQL语句
(5)操作ResultSet结果集
(6)关闭连接,释放资源
3、(1)代码的可读性和可维护性
(2)PreparedStatement尽最大可能提高性能,因为预编译语句有可能被重复调用,所以语句在被DB的编译器编译后的执行代码被缓存下来,那么下次调用时只要是相同的预编译语句就不需要编译,只要将参数直接传入编译过的语句执行代码中(相当于一个函数)就会得到执行。
(3)极大地提高了安全性传。递给PreparedStatement对象的参数可以被强制进行类型转换,使开发人员可以确保在插入或查询数据时与底层的数据库格式匹配;在公共Web站点环境下,防止Sq注入问题。
五、编程题
1.public static void main(String[] args) throws Exception {
// int i = insert();
// int i = delete();
// int i = update();
select();
// System.out.println(i);
}
// 获取连接对象
private static Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/javatest";
String username = "root";
String password = "1234";
Connection conn = null;
try {
Class.forName(driver); // classLoader,加载对应驱动
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 插入操作
private static int insert() {
Connection conn = getConn();
int i = 0;
String sql = "insert into tb_user(name,sex,email,birthday) values(?,?,?,?)";
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "itcast");
pstmt.setString(2, "男");
pstmt.setString(3, "[email protected]");
pstmt.setString(4, "2000-01-01");
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
// 删除操作
private static int delete() {
Connection conn = getConn();
int i = 0;
String sql = "delete from tb_user where name=?";
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "itcast");
i = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
// 更新操作
private static int update() {
Connection conn = getConn();
int i = 0;
String sql = "update tb_user set name=? where name =?";
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "itcast");
pstmt.setString(2, "abc");
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
// 查询操作
private static void select() {
Connection conn = getConn();
String sql = "select * from tb_user";
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id"); // 通过列名获取指定字段的值
String name = rs.getString("name");
String sex = rs.getString("sex");
String email = rs.getString("email");
Date birthday = rs.getDate("birthday");
System.out.println(id + " | " + name + " | " + sex + " | " + email + " | " + birthday);
}
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.import java.sql.*;
public class Test {
public static void main(String[] args) throws Exception {
transferAccounts(2, 1, 100);
}
public static void transferAccounts(int fromid,int toid,int transferMoney) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jdbc";
Connection con = DriverManager.getConnection(url,"root", "root");
int fromMoney = getMoney(fromid);
// 转出账户
String sql = "update tb_count set money=? where id =?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, fromMoney-transferMoney);
ps.setInt(2, fromid);
// 转入账户
int toMoney = getMoney(toid);
String sql2 = "update tb_count set money=? where id =?";
PreparedStatement ps2 = con.prepareStatement(sql2);
ps2.setInt(1, toMoney+transferMoney);
ps2.setInt(2, toid);
ps.executeUpdate();
ps2.executeUpdate();
ps.close();
ps2.close();
con.close();
}
// 获取当前账户id的账户余额
public static int getMoney(int id) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jdbc";
Connection con = DriverManager.getConnection(url,"root", "root");
String sql = "select money from tb_count where id =?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
ResultSet resultSet = ps.executeQuery();
int money = 0;
while(resultSet.next()){
money = resultSet.getInt(1);
}
ps.close();
con.close();
return money;
}
}
六、原题及其解析
暂无。