所以我有一个方法可以返回我得到的项目名称。但是第一个查询会返回错误。我认为不应该存在,因为它的语法正确。这是代码:
void showInventory(String userId, MessageReceivedEvent event) throws HTTP429Exception, DiscordException,
MissingPermissionsException{
sendMessage("test0",event);
String sql = "SELECT itemID FROM inventory WHERE playerID=?";
String sql2 = "SELECT Name FROM items WHERE ID=?";
java.sql.PreparedStatement state;
java.sql.PreparedStatement state2;
try {
state = Main.conn.prepareStatement(sql);
state2 = Main.conn.prepareStatement(sql2);
state.setString(1, userId);
ResultSet results = state.executeQuery(sql);
ResultSet resultname = null;
String invent = "";
sendMessage("test",event);
while(results.next()){
sendMessage("test1",event);
int result = results.getInt("itemID");
state2.setInt(1, result);
resultname = state2.executeQuery(sql2);
while(resultname.next()){
String name = resultname.getString("Name");
invent += (name + "\n");
}
}
sendMessage(invent, event);
} catch (SQLException e) {
e.printStackTrace();
}
}
这是错误:
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 '?' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370)
at martacus.mart.bot.rpg.InventoryHandler.showInventory(InventoryHandler.java:48)
at martacus.mart.bot.rpg.InventoryHandler.OnMesageEvent(InventoryHandler.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sx.blah.discord.handle.EventDispatcher.dispatch(EventDispatcher.java:104)
at sx.blah.discord.api.internal.DiscordWS.messageCreate(DiscordWS.java:323)
at sx.blah.discord.api.internal.DiscordWS.onMessage(DiscordWS.java:144)
at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:312)
at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:368)
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:157)
at org.java_websocket.client.WebSocketClient.interruptableRun(WebSocketClient.java:230)
at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:188)
at java.lang.Thread.run(Unknown Source)
错误在于讨论第一个名为sql的String。这是在:
ResultSet results = state.executeQuery(sql);
答案 0 :(得分:3)
通过调用executeQuery(sql)
,您实际上会在Statement
上调用方法,而不是PreparedStatement
。因此,您丢弃已分配的参数值并执行占位符?
未解析的查询 - 因此您会收到该错误。
更改
ResultSet results = state.executeQuery(sql);
到
ResultSet results = state.executeQuery();
你应该没事。
(原因
resultname = state2.executeQuery(sql2);
需要
resultname = state2.executeQuery();
同样)