老师,检查作业!

来源: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;

}

}

}

http://img.mukewang.com/climg/5e3bec8f09ba8cfd07130179.jpg

写回答

1回答

好帮手慕阿莹

2020-02-06

同学写的不错哦,符合作业要求,继续加油哦!!!

如果我的回答解决了你的疑问,请采纳,祝学习愉快。

0

0 学习 · 8016 问题

查看课程