线程“main”中的异常java.sql.SQLException:没有为参数1指定值

时间:2016-09-24 19:55:39

标签: java mysql jdbc

当我执行程序时,它显示错误:没有为参数1指定值

此语句中发生错误:'rowSet.execute();'

public static void main(String[] args) throws SQLException {
    // Create a RowSet Instance
    RowSet rowSet = new com.sun.rowset.JdbcRowSetImpl();

    // Establish the Databse Connection
    rowSet.setUrl("jdbc:mysql://localhost:3306/jmysql?autoReconnect=false&useSSL=false");
    rowSet.setUsername("root");
    rowSet.setPassword("root!@#$Mysql2016");
    rowSet.setCommand("SELECT userpassword FROM user WHERE userid = ? "
            + " AND userName = ? ");
    //Execute the Sql Command
    rowSet.execute(); 

    rowSet.setInt("userid", 415);
    rowSet.setString("UserName", "anjeetSharma415@gmail.com");

     // Display the UserPassword
    System.out.print("Password : " + rowSet.getString(3));

    // Close the Connetion
    rowSet.close();
}
  

线程“main”中的异常java.sql.SQLException:未指定值   参数1 at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)at at   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)at at   com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2611)     在   com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2586)     在   com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2510)     在   com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2259)     在com.sun.rowset.JdbcRowSetImpl.execute(JdbcRowSetImpl.java:567)at at   com.advjdbc.database.chapter.RowSetPerParepedStatement.main(RowSetPerParepedStatement.java:30)

1 个答案:

答案 0 :(得分:2)

您获得异常的原因是您在设置所有参数之前调用execute()方法。 所以你必须按如下方式设置params(在execute()方法之前):

rowSet.setUrl("jdbc:mysql://localhost:3306/jmysql?autoReconnect=false& useSSL=false");
rowSet.setUsername("root");
rowSet.setPassword("root!@#$Mysql2016");
rowSet.setCommand("SELECT userpassword FROM user WHERE userid = ? AND userName = ? ");

// set the param values
rowSet.setInt(1, 415);
rowSet.setString(2, "anjeetSharma415@gmail.com");

//Execute the Sql Command
rowSet.execute();