老师帮我看看我的回答

来源:3-6 自由编程

Wonwayshon

2020-12-31 12:49:03

public class JDBCTest3 {
@Test
public void JDBCTest36() {
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="DELETE FROM goods WHERE name='耳机';";
//执行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);
}
}else {
System.out.println("删除失败!");
}
}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回答

好帮手慕小班

2020-12-31

同学的代码完成的不错,继续加油。

祝学习愉快!

0

0 学习 · 8016 问题

查看课程