老师,检查作业!
来源:3-4 自由编程
BirdMan98
2020-02-06 18:38:09
//更新数据
@Test
public void update() {
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = (Connection) DriverManager.getConnection("jdbc:mysql:///test"+"?useUnicode=true&characterEncoding=utf-8","root","root");
statement = (Statement) connection.createStatement();
String sqlString = "update goods set price = 300 where id = 5";
int i = statement.executeUpdate(sqlString);
if (i>0) {
String sqlString2 = "select * from goods";
ResultSet resultSet = statement.executeQuery(sqlString2);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String desp = resultSet.getString("desp");
float price = resultSet.getFloat("price");
System.out.println(id+" "+name+" "+price+" "+desp);
}
}
} 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回答
同学写的不错哦,符合作业要求,继续加油哦!!!
如果我的回答解决了你的疑问,请采纳,祝学习愉快。