Error with PreparedStatement

穿精又带淫゛_ 提交于 2021-02-05 07:45:06

问题


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 + "\n");
            }
        }
        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);

回答1:


By invoking executeQuery(sql) you actually invoke a method on Statement - not on PreparedStatement. So you throw away the parameter value already assigned and execute a query where the placeholder ? remains unparsed - so you get that error.

Change

 ResultSet results = state.executeQuery(sql);

to

 ResultSet results = state.executeQuery();

and you should be fine.

(And of cause

resultname = state2.executeQuery(sql2);

needs to be

resultname = state2.executeQuery();

as well)



来源:https://stackoverflow.com/questions/35408416/error-with-preparedstatement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!