aspose file tools*
The moose likes JDBC and the fly likes y this simple program throws exception?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "y this simple program throws exception??" Watch "y this simple program throws exception??" New topic
Author

y this simple program throws exception??

reehan ishaque
Ranch Hand

Joined: Mar 25, 2001
Posts: 54
hi folks!
im new to jdbc n sending u a very simple program that throws exception "general error accessing database". the program executes fine for the first few iterations but raises exceptions later. i am using oracle server.plz help me in sorting out the error in this program.
thanks n bye

import java.sql.*;
class find{
Connection con;
find()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:data","scott","tiger");

}catch(Exception e){System.out.println(e);}
}
void m()
{
ResultSet r=null;
PreparedStatement pt=null;
try{
pt = con.prepareStatement("select * from tableName");
r=pt.executeQuery();
while(r.next())
{
System.out.println(r.getString("columnName));
}
}catch(Exception e){System.out.println(e);}
finally {
try {
r.close();
}
catch (Exception e) {
}
}
}
public static void main(String args[]){
find f = new find();
for(int i=0;i<=100;i++)
f.m();
}
}
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I think you need to do 1 of 2 things (do one or the other, but not both!):
1. close the prepared statement in your finally block. Each iteration creates a new prepared statement and the memory is not released.
2. make your prepared statement a class variable (along with the connection so it is created only once and reused in each iteration
For more detailed error messages see the new exception handling I've added
performance tip: create the connection once, create the prepared statement once, reuse them in the iteration:

Good luck,
Jamie
[This message has been edited by Jamie Robertson (edited August 08, 2001).]
[This message has been edited by Jamie Robertson (edited August 08, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: y this simple program throws exception??