Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.sql.SQLException: Connection is closed.

Status
Not open for further replies.

SilverStray

Programmer
Oct 25, 2001
47
AU
Somewhere between the loop, I encounter an error which says: java.sql.SQLException: Connection is closed.

What is the proper way of using PreparedStatement and Connection when inside a loop? I have the following similar code which results on a Connection Closed problem. I wonder what could be wrong in this...

---
Connection conn = this.jdbcFactory.getConnection();
String val = "";
String sqlString = "";

for(int m=0; m<array1.length; m++){

val = array1[m];
sqlString = "UPDATE TABLE1 "
"SET COL1 = '" + val + "'";

PreparedStatement ps = conn.prepareStatement(sqlString);

try{
ps.executeUpdate();
ps.close();
} catch (SQLException e){
ps.close();
conn.close();
}
conn.close();
}

Thanks in advance!
 
Well, I don't know much about PreparedStatements, but I see that you close your connection inside the loop with the conn.close()

Maybe you should do it after all SQL sentences, outside the loop.

Furthermore, if you catch an Excepcion, I think you should stop the loop.

Cheers.

Dian

 
close your connection outer from your "for loop".

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top