I am stuck. I refuse to stay stuck for long so I am asking for help. What I am trying to do is take a ResultSet that I am retrieving from a select statement from another Database throw it into a ArrayList and then convert the array list to a insert prepared statement in Java.
** UPDATE ** The problem is that I am getting a "java.sql.SQLException: Parameter index out of range (6 > number of parameters, which is 5)." which means that it is receiving 6 parameters and not 5 and I can't see how this is happening. Since in the debugger I am watching as the values update correctly but my logic is now incorrect.
I ran the debugger and saw that I was just dumping all the values into the first parameter. I understand what I need to do. This is to split the ArrayList result into each parameter of the insert statement. My brain is dead. It's the end of the day and this is the last piece of my puzzle so I can move on. I just don't know how to do it. Thank you for any assistance!
This is what my result ArrayList holds.
[800-0022-08, 0025, 2015-09-30, 600040, 115]
[800-0022-08, 0024, 2015-09-30, 600040, 115]
[800-0022-08, 0008, 2015-09-30, 600040, 115]
[800-0022-08, 0036, 2015-09-30, 600040, 115]
[800-0022-08, 0035, 2015-09-30, 600040, 115]
[800-0022-08, 0034, 2015-09-30, 600040, 115]
The code is below.
ResultSet rs = (ResultSet) stmt.executeQuery(query);
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
int rowCount = 1
ArrayList <String[]> result = new ArrayList<>();
while (rs.next()) {
System.out.println("Row " + rowCount + ": ");
String[] row = new String[numberOfColumns];
for (i = 1; i < numberOfColumns + 1; i++) {
System.out.print(" Column " + i + ": ");
System.out.println(rs.getString(i);
row[i - 1] = rs.getString(i);
}
result.add(row);
System.out.println("");
rowCount++;
}
String Connection = "CONNECTIONSTRING";
String UserName = "USERNAME";
String Password = "PASSWORD";
String MySQLQuery = "INSERT INTO serial (MTSER_CODE, MTSER_SERIAL, MTSER_RECDATE, MTSER_WO, MTSER_WOSUF)" + "VALUES (?,?,?,?,?)";
String Driver = "org.gjt.mm.mysql.Driver";
Class.forName(Driver);
java.sql.Connection MyConnection = (java.sql.Connection) DriverManager.getConnection(Connection, UserName, Password);
java.sql.PreparedStatement preparedStmt;
preparedStmt = MyConnection.prepareStatement(MySQLQuery);
// ** UPDATED FOR LOOP **
for (String[] arr : result) {
try (java.sql.PreparedStatement preparedStmt = MyConnection.prepareStatement(MySQLQuery)) {
for (int q = 0; q < arr.length; q++) {
preparedStmt.setString(q + 1, arr[0]);
preparedStmt.setString(q + 2, arr[1]);
preparedStmt.setString(q + 3, arr[2]);
preparedStmt.setString(q + 4, arr[3]);
preparedStmt.setString(q + 5, arr[4]);
}
preparedStmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
The Insert statement after an iteration looks like this:
INSERT INTO serial (MTSER_CODE, MTSER_SERIAL, MTSER_RECDATE, MTSER_WO, MTSER_WOSUF)" + "VALUES ([800-0022-08, 0025, 2015-09-30, 600040, 115],** Not Specified **,** Not Specified **,** Not Specified **,** Not Specified **)
for(int p = 1; p < result.size(); p++)looks suspicious: did you meanfor(int p = 0 ; p < result.size(); p++)instead (and then usingp+1insidesetString)?preparedStmt.executeUpdate();statement outside of the loop, see if that helps. As it is now, you're executing thePreparedStatementbefore all the parameters are set, hence the exception