简体   繁体   English

更新一行mysql表

[英]update a row of mysql table

I am trying to update my table 'total': 我正在尝试更新我的表“总计”:

in the row of id = 1, I want to update the value of 'days_left' to be 47. 在id = 1的行中,我想将“ days_left”的值更新为47。

String values_to_update = "UPDATE total SET days_left = '47' where id = '1'";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, "root", "Admin");
PreparedStatement ps = con.prepareStatement(values_to_update);
con.close();

Why isn't it updated? 为什么不更新?

You need to execute the prepared statement. 您需要执行准备好的语句。 Try: 尝试:

ps.executeUpdate(); 
con.commit();

before you close the connection. 在关闭连接之前。

至少,您需要通过以下ps.executeUpdate();执行SQL语句: ps.executeUpdate();

Do ... 做...

ps.executeUpdate()

... before you close the connection. ...在关闭连接之前。 It doesn't look like you need a prepared statement either. 看起来您也不需要准备好的语句。 A statement will do in your case. 一份声明将针对您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM