I have my database set up and I'm connected to it as I can add in values normally. I want to run a loop and add values now but I'm getting this error:
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.executeUpdate(PreparedStatement.java:2120)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062)
at test.Test.main(Test.java:30)
Also, the first parameter in my table is an auto-increment value that's supposed to be set to NULL but Eclipse won't let me use NULL so any suggestions on that would be appreciated. Here is my code for adding the data:
Connection conn = null;
PreparedStatement pstmt = null;
int loopNumber = 0;
try {
while (loopNumber < 10) {
conn = getConnection();
String query = "insert into username(id, name) values(?, ?)";
pstmt = conn.prepareStatement(query); // create a statement
pstmt.setInt(loopNumber, loopNumber); // set input parameter 1
pstmt.setString(loopNumber, "deptname"); // set input parameter 2
pstmt.executeUpdate(); // execute insert statement
}
loopNumber++;
} catch (Exception e) {
e.printStackTrace();
} finally {
pstmt.close();
conn.close();
}