Struggling with some java / sql code that carries out a stored procedure call, followed by a select followed by an insert into –
The call to the stored procedure works and creates the ‘duration’ table –
while(true) {
boolean var1 = false;
Connection var2 = DBConnPool.getInstance().getConnection();
Statement var3 = null;
ResultSet var4 = null;
try {
var3 = var2.createStatement();
String var5;
var5 = "CALL totalduration('%', NOW() - INTERVAL 20 MINUTE, NOW());";
var3.executeQuery(var5);
The select statement then runs and correctly outputs multiple rows –
var5 = "SELECT duration.name, zone, duration, asset.id as assetid, node.id as nodeid\n" +
"FROM duration\n" +
"inner join asset\n" +
"on asset.name = duration.name\n" +
"inner join node\n" +
"on node.zonename = duration.zone\n" +
"where Duration > '00:10:00'\n" +
"and Zone = 'Reception'\n" +
"group by duration.name DESC";
var4 = var3.executeQuery(var5);
I then want to insert into a table data from the resulting ResultSet, if I use the following then it inserts only 1 row
If (var4.next())
{
int var6 = var4.getInt("assetid");
int var7 = var4.getInt("nodeid");
String var8;
var8 = "insert into alerts (ts, assetid, alerttypeid, nodeid, ack) values (now(), " + var6 + ", " + 11 + ", " + var7 + ", 0)";
var3.executeUpdate(var8);
}
So, I assumed the following would apply the insert to all the ResultSet rows –
while(var4.next())
{
int var6 = var4.getInt("assetid");
int var7 = var4.getInt("nodeid");
String var8;
var8 = "insert into alerts (ts, assetid, alerttypeid, nodeid, ack) values (now(), " + var6 + ", " + 11 + ", " + var7 + ", 0)";
var3.executeUpdate(var8);
}
But the error ‘Operation not allowed after ResultSet closed’ implies that var4 is closed?
I am obviously doing something silly and perhaps the insert into should be before the select so would appreciate some guidance on the ‘correct way’ to solve this.
Regards Active
PreparedStatementwhen you have parameters. Take a lookvar#