老师帮我看看作业
来源:3-4 自由编程
夏蔚海
2021-01-02 23:36:45
public void Zybc2(){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
//注册驱动
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
// 连接数据库
connection = DriverManager.getConnection("jdbc:mysql:///jdbctest?serverTimezone=GMT","root","luolimo+");
// 获取执行sql语句的对象
statement = connection.createStatement();
// 获取sql
String sql = "update doods set name = '苹果手机',price = 9870.00,desp = '金色,内存126G' where id = 1";
// 执行sql
int i = statement.executeUpdate(sql);
if (i > 0) {
System.out.println("修改成功!");
sql = "select * from doods";
resultSet = statement.executeQuery(sql);
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String price = resultSet.getString("price");
String desp = resultSet.getString("desp");
System.out.println(id + " " + name + " " + price + " " + desp);
}
}else{
System.out.println("修改失败!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
// 释放资源
if (resultSet != null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
resultSet = null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
statement = null;
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
connection = null;
}
}
}
1回答
好帮手慕小脸
2021-01-03
同学写的不错哦,符合作业要求,继续加油!
祝学习愉快~
相似问题