Results 1 to 3 of 3
Thread: Database error
- 04-26-2011, 07:43 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
Database error
Hi all,
I've connected my program up to a database. When i issue one statement against the DB it works fine but if i issue more then one, i get an error. heres the partial code i have:
I think i'm closing all connections after use so I'm not sure what the problem is. If i just run one of the two methods (getRS or CountMyRS) they both work fine. it's just when i try to run them both one after the other. the error in nondescript: it in the second method run (depending on the order) and it says : java.sql.SQLException: General error. anyone have any ideas?Java Code:private Statement rsStatement; private ResultSet rsDistricts; private ResultSet rsCountDistricts; private Connection conn; .... public void SetUpTree(){ try { getRS("SELECT * FROM tblDistricts ORDER BY DistrictID"); } catch (Exception e) { this.setColor(Color.red); this.setLable("Error in SetUpTree1(): " + e.toString()); } try { int getCount= CountMyRS("SELECT COUNT (DistrictID) FROM tblDistricts;"); System.out.print("count" + getCount); } catch (Exception e) { this.setColor(Color.red); this.setLable("Error in SetUpTree2(): " + e.toString()); } } public int CountMyRS(String SQL) throws Exception { try{ conn = connectToDatabase("ADR_DB.accdb"); rsStatement = conn.createStatement(); rsCountDistricts = rsStatement.executeQuery(SQL); rsCountDistricts.next(); int count=rsCountDistricts.getInt(1); conn.close(); return count; } catch(Exception e){ this.setColor(Color.red); this.setLable("Error in CountMyRS: " + e.toString()); conn.close(); return 0; } } public void getRS (String SQL) throws Exception { conn = connectToDatabase("ADR_DB.accdb"); rsStatement = conn.createStatement(); rsDistricts = rsStatement.executeQuery(SQL); conn.close(); rsStatement.close(); } public Connection connectToDatabase(String dbName) throws Exception { if (conn != null) { return conn; } String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; if (System.getProperty("java.vendor").equals("Microsoft Corp.")) { //Microsoft VM users driver = "com.ms.jdbc.odbc.JdbcOdbcDriver"; } String curDir = System.getProperty("user.dir"); String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + curDir + "\\" + dbName; String username = ""; String password = ""; Class.forName(driver); return DriverManager.getConnection(url, username, password); }
Thanks
jason
- 04-26-2011, 08:25 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Seems you are trying to use a Connection which has already been closed. conn is created in either method, then closed in that method. When you call connectToDatabase again in the next method call, conn is not null and returns that reference (which has already been closed). Easy fixes include recreating the reference, or closing only after you are finished with the connection
- 04-26-2011, 08:31 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
Similar Threads
-
Error creating a new database in Oracle 10g
By dav007 in forum JDBCReplies: 2Last Post: 03-04-2011, 08:48 AM -
Database Connectivity error
By vaibhavdeshmukh17 in forum JDBCReplies: 4Last Post: 01-11-2011, 10:11 AM -
Database error?
By r035198x in forum Suggestions & FeedbackReplies: 1Last Post: 10-27-2009, 11:10 AM -
Database Driver Error.....Plz Help!
By sayan751 in forum NetBeansReplies: 0Last Post: 02-19-2009, 04:59 PM -
Error while connecting to a database
By Fukushuusha in forum JDBCReplies: 2Last Post: 12-16-2007, 09:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks