gpt4 book ai didi

java - 线程 "main"java.sql.SQLException : No value specified for parameter 1 中的异常

转载 作者:行者123 更新时间:2023-11-29 06:08:45 26 4
gpt4 key购买 nike

当我执行程序时,它显示错误:No value specified for parameter 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();
}

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)

最佳答案

出现异常的原因是您在设置所有参数之前调用了 execute() 方法。所以你必须按如下方式设置参数(在 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();

关于java - 线程 "main"java.sql.SQLException : No value specified for parameter 1 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com