So I have a method which returns the items names that I get. But with the first query it returns an error. Which to my opinion shouldn't be there because its the right syntax. Here is the code:
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 + "
");
}
}
sendMessage(invent, event);
} catch (SQLException e) {
e.printStackTrace();
}
}
This is the error:
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)
The error is talking about the 1st String called sql. Which is called in:
ResultSet results = state.executeQuery(sql);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…