I wrote the following class DBManager to access the mySQL DB server from java programs.
And use it in the following way:
Is there any problem in this approach? Will the connection created in getQueryResultSet() closed automatically after I call this method? There seems to be a limit on the number of available connections. As my program query the DB using the above approach continously, I am concerned whether the available connections would be exhausted. I started my program this afternoon and went out for a hour or so, when I came back, I found the following error message:
What could be the reason of this error? To see whether it's the problem of connections, I wrote the following code to test it,
And it didn't generate any error so far: the count is already 68000! I am confused. Anybody can help me? Thanks. [ January 14, 2004: Message edited by: W Sun ] [ January 14, 2004: Message edited by: W Sun ]
I would be interested to know what's the deal also. I haven't had that problem. What is you system config? I would pre-pend each of you sql exceptions with specific word to capture where exactly the error occured. The SQLException error messages(some) are a bit vague. Example System.out.println("SELECT Error: " + sqle); Example System.out.println("CONNECT Error: " + sqle); Why are you leaving all those connections open anyway?
Thanks Jason. I was trying to use a piece of simple code to get query result from the DB, and WISH the connection created by calling resultSet = new DBManager().statement.executeQuery(querySQL); is temporary and would be closed automatically when the call is finished. But if I rewrite the method getQueryResultSet like the following, the returned result set is not accessible because the connection is closed.(believe it, I tried it.)
line 5 should have been: resultSet = dbManager.statement.executeQuery(querySQL); I haven't tried it, but I think that's why the connection was closed I would like to share my wrapper with you: Note: It's not completed, but it works
I call it like this:
This sets up a persistent connection that will last the life of the app. Further, if you want to switch databases or users, you can do that with the setter methods and then call renewConnection() You can add/change/delete how you see fit if you want to...It's young is intended that at completion, it will support most DB Servers.