Java MySQL更新查询
我收到错误“无法发布数据”。
这是SSCCE
//package mysqltest; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.Applet; import java.awt.TextArea.*; import java.sql.*; import java.util.*; import javax.swing.plaf.*; import javax.swing.plaf.basic.*; import java.net.*; import java.applet.*; public class test extends JApplet { public JTextArea c; public void init() { c = new JTextArea(); add(c); c.append("Looking for database..."); Connection conn = null; Properties props = new Properties(); String url = "jdbc:mysql://localhost:3306/"; String dbName = "mystik"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = ""; String loggedusername = getParameter("name"); try { Class.forName(driver).newInstance(); props.put("user", "root"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mystik", props); c.append("\nConnected to the database"); c.append("\nGetting stats for: " + loggedusername); PreparedStatement statement = conn.prepareStatement( "select * from `user` where `username` = '"+loggedusername+"'"); ResultSet result = statement.executeQuery(); // just a dumb mysql statement! while(result.next()) { c.append("\nUsername: "+result.getString(2)+ "\nLevel: "+result.getString(6)+"\nEXP: "+result.getString(8)+"\n"); } PreparedStatement updateEXP = conn.prepareStatement( "update`user` set `exp` = '666' where `username` = '"+loggedusername+"'"); ResultSet updateEXP_done = updateEXP.executeQuery(); while(result.next()) { c.append("\nUsername: "+result.getString(2)+ "\nLevel: "+result.getString(6)+"\nEXP: "+result.getString(8)+"\n"); } conn.close(); c.append("\nDisconnected from database"); } catch (Exception e) { e.printStackTrace(); } } }
它的工作原理……而且只是update
java查询没有。
这是JTextArea看到的内容:
Looking for database... Connected to the database Getting stats for: weka Username: weka Level: 1 EXP: 1
这是我的错误:
added manifest adding: test.class(in = 2440) (out= 1308)(deflated 46%) adding: mysql-connector-java-5.1.13-bin.jar(in = 767492) (out= 735869)(deflated 4%) Warning: The signer certificate will expire within six months. java.sql.SQLException: Can not issue data manipulation statements with executeQu ery(). at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929) at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:436) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java: 2176) at test.init(test.java:53) at sun.applet.AppletPanel.run(AppletPanel.java:424) at java.lang.Thread.run(Thread.java:619)
最后,这是我如何编译使用.bat文件。
@ECHO OFF C: CD \wamp\www\mystikrpg\mysqltest javac -cp mysql-connector-java-5.1.13-bin test.java jar cvf mysqlTry.jar test.class mysql-connector-java-5.1.13-bin.jar jarsigner -keystore dankey -storepass soccer -keypass soccer mysqlTry.jar gamerpg appletviewer -J-Djava.security.policy=game.policy mysqltry.html
我该如何解决这个错误? 谢谢。
AS PreparedStatement文档:
执行此PreparedStatement对象中的SQL语句,该对象必须是SQL INSERT,UPDATE或DELETE语句; 或者不返回任何内容的SQL语句,例如DDL语句。
要执行更新,删除或插入数据库中的任何数据的查询,您不能使用executeQuery
…您必须使用: .executeUpdate(query)
所以这段代码( 错误 ):
PreparedStatement updateEXP = conn.prepareStatement("update`user` set `exp` = '666' where `username` = '"+loggedusername+"'"); ResultSet updateEXP_done = updateEXP.executeQuery();
看起来像( 好 ):
正确使用
PreparedStatement updateEXP = conn.prepareStatement("update`user` set `exp` = '666' where `username` = '"+loggedusername+"'"); ResultSet updateEXP_done = updateEXP.executeUpdate();
还要考虑使用用户名的参数:
PreparedStatement updateEXP = conn.prepareStatement("update
user set
exp = '666' where
username = ? ");
updateEXP.setString(1, loggedusername);
ResultSet updateEXP_done = updateEXP.executeUpdate();
根据Javadoc,DML查询( INSERT
, UPDATE
, DELETE
)需要使用executeUpdate()
而不是executeQuery()
来执行。 它返回一个带有受影响行数的int
。
PreparedStatement updateEXP = conn.prepareStatement( "update`user` set `exp` = '666' where `username` = '"+loggedusername+"'"); int affectedRows = updateEXP.executeUpdate();
也就是说,关闭conn
(以及Statement
和ResultSet
!)应该在finally
块中完成,否则在调用close()
之前抛出exception时它仍然会打开。 您对PreparedStatement
的使用也是错误的。 您仍然通过简单的字符串连接来内联列值,而不是将它们设置为参数化值。 您没有从它的SQL注入预防function中受益。
也可以看看:
- 基本的JDBC教程
- 使用PreparedStatement
executeUpdate(query)
而不是executeQuery()