简体   繁体   中英

Exception in thread “main” java.sql.SQLException: No value specified for parameter 1

When I execute the program, it shows the error: No value specified for parameter 1

The error occurs in this statement: '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();
}

Exception in thread "main" java.sql.SQLException: No value specified for parameter 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2611) at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2586) at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2510) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2259) at com.sun.rowset.JdbcRowSetImpl.execute(JdbcRowSetImpl.java:567) at com.advjdbc.database.chapter.RowSetPerParepedStatement.main(RowSetPerParepedStatement.java:30)

The reason why you are getting the exception is that you are calling the execute() method before setting all the parameters. So you have to set the params as follows (before the execute() method):

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM