Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

java - Error with PreparedStatement

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...