老师,检查作业!
来源:3-2 自由编程
BirdMan98
2020-02-06 18:29:32
//插入数据
@Test
public void insert() {
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test"+"?useUnicode=true&characterEncoding=utf-8","root","root");
statement = (Statement) connection.createStatement();
String sqlString = "insert goods(id,name,price,desp) values(null,'耳机',200,'蓝牙耳机')";
int i = statement.executeUpdate(sqlString);
if (i>0) {
System.out.println("success!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
connection=null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
statement=null;
}
}
}
1回答
同学,你好!程序运行效果正确,完成得非常好!和上面一个问题一样,注意Connection和Statement的导入类问题,需要导入java.sql包下的类。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!