老师帮我看看我的回答
来源:3-4 自由编程
Wonwayshon
2020-12-31 11:56:34
public class JDBCTest3 {
@Test
public void JDBCTest34() {
Connection con=null;
Statement stm=null;
ResultSet rs=null;
try {
//加载驱动com.mysql.cj.jdbc.Driver
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql:///JDBC?useSSL=false&serverTimezone=Hongkong&useUnicode=true&CharacterEncoding=utf-8", "root", "123456");
stm=con.createStatement();
String sql="UPDATE goods SET desp='有线耳机' WHERE id=5";
//执行sql语句
int i=stm.executeUpdate(sql);
if(i>0) {
System.out.println(i+" line effected");
String sql_desplay="SELECT * FROM goods";
rs=stm.executeQuery(sql_desplay);
while(rs.next()) {
int id=rs.getInt("id");
String price= rs.getString("price");
String desp=rs.getString("desp");
System.out.println(id+" "+price+" "+desp);
}
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
con=null;
if(stm!=null) {
try {
stm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
stm=null;
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
rs=null;
}
}
}
1回答
同学你好,代码整体完成不错,运行效果正确。根据题目要求,建议在if语句后添加else语句,使用输出语句输出添加失败。
相似问题