Err*_*ion 1 java mysql database swing
我使用Java Swing应用程序更新Mysql表中的列.这是我的代码(部分)
String qunty = txtWithdraw.getText();
String partno = txtNo.getText();
int qty = Integer.parseInt(qunty);
con = DriverManager.getConnection(url + db, "username", "password");
Statement st = con.createStatement();
String sell = "update Store_info_table set qnty_received = qnty_received - " + qty + "where Part_number = '" + partno + "'";
st.executeUpdate(sell);
Run Code Online (Sandbox Code Playgroud)
我得到的例外是:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Part_number = 'DF6534'' at line 1
Run Code Online (Sandbox Code Playgroud)
我想更新qnty_received字段,使其等于原始值减去用户传递的值,即(int qty).我犯了什么错误?
在where之前添加一个空格:
" where Part_number = '" + partno + "'";
Run Code Online (Sandbox Code Playgroud)
作为一种好的做法,我建议您使用PreparedStatement和设置相同的参数.动态连接参数可能会强制db引擎每次都解析一个新的SQL语句.
预编译SQL语句并将其存储在PreparedStatement对象中.然后,可以使用此对象多次有效地执行此语句.