2016-09-24 32 views
1

指定的值當我執行程序,它顯示了錯誤:否參數1異常線程「main」值java.sql.SQLException:否參數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("[email protected]#$Mysql2016"); 
    rowSet.setCommand("SELECT userpassword FROM user WHERE userid = ? " 
      + " AND userName = ? "); 
    //Execute the Sql Command 
    rowSet.execute(); 

    rowSet.setInt("userid", 415); 
    rowSet.setString("UserName", "[email protected]"); 

    // 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)

回答

2

爲什麼你越來越異常的原因是,你在呼喚所有參數的設置之前​​方法。 所以,你必須把params如下(execute()方法前):

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

// set the param values 
rowSet.setInt(1, 415); 
rowSet.setString(2, "[email protected]"); 

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

感謝您的建議ujulu .. –

+0

不客氣。 – ujulu

相關問題