老师帮我看看我的回答
来源:3-2 自由编程
Wonwayshon
2020-12-31 11:08:51
package com.imooc.jdbc.demo1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class JDBCTest32 {
@Test
public void JDBCTest() {
Connection con=null;
Statement stm=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="INSERT goods(name,price,desp) VALUES('耳机','200.0','蓝牙耳机')";
//执行sql语句
int i=stm.executeUpdate(sql);
if(i>0) {
System.out.println(i+" line effected");
}
}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;
}
}
}
1回答
同学你好,程序运行效果正确,完成得很好。不过建议同学根据题目要求添加else判断,当添加失败时输出添加失败的内容。
祝学习愉快!
相似问题