[Java] MySQL結果集 - 沒有為參數1指定值


Answers

也許是因為你沒有為你的請求設置參數:

SELECT * FROM mydatabase WHERE TickerID= ?

你必須添加:

preparedStatement.setString(1, "youUserIdValue");

在執行查詢之前。

您也可以使用命名參數:

preparedStatement = connect
        .prepareStatement("SELECT * FROM mydatabase "
                    + " WHERE TickerID=:userID");
preparedStatement.setString("userID", "youUserIdValue");
Question

我不知道這個錯誤意味著什麼或如何解決它。 我試圖從我的一個數據庫檢索一些數據,但仍然遇到下面的這個錯誤消息。

preparedStatement = connect
            .prepareStatement("SELECT * FROM mydatabase "
                        + " WHERE TickerID=?");
            resultSet = preparedStatement.executeQuery(); //where it says the error is, line 132
            while(resultSet.next())
            {
                aIDTA = resultSet.getInt("AccountID");
                nameTA = resultSet.getString("Name");
                CashBalance = resultSet.getDouble("CashBalance");
                TradeFeeBuy = resultSet.getDouble("TradeFeeBuy");
                TradeFeeSell = resultSet.getDouble("TradeFeeSell");
                AssetsBalance = resultSet.getDouble("AssetsBalance");
            }



Exception in thread "main" java.sql.SQLException: No value specified for parameter 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
    at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2281)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2261)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2191)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2004)
    at BuyAndSell.BuyAndSell(BuyAndSell.java:132)
    at Main.main(Main.java:21)