Results 1 to 4 of 4
Thread: JDBC Too Many Connection Error
- 02-22-2012, 07:36 AM #1
Member
- Join Date
- Feb 2012
- Location
- Sri Lanka
- Posts
- 2
- Rep Power
- 0
JDBC Too Many Connection Error
Please help me regarding to this error guys...
I'm using a JDBC class for a Java Servlet web application and in my web application, my categories will be loading from the Database and after some page exchanges, I'm getting this error.
please help me...Java Code:com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
My JDBC class is
And, My Category JSP page's code is,Java Code:package db; import java.sql.*; public class DB { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/lkshops"; Connection getConnection() throws Exception { Class.forName(driver); Connection con = DriverManager.getConnection(url, "lkshops", "123"); return con; } public void putdata(String sql) { try { PreparedStatement state = getConnection().prepareStatement(sql); state.executeUpdate(); state.close(); } catch (Exception e) { } } public ResultSet getdata(String sql) throws Exception { Statement state = getConnection().createStatement(); ResultSet rset = state.executeQuery(sql); return rset; } }
Please tell me if there has any of error and I want to correct and learn this method very clearly guys... thanks a lot...Java Code:<%@page import="java.sql.ResultSet"%> <%@page import="db.DB"%> <% DB db = new db.DB(); try { int x = 0; ResultSet rs = db.getdata("SELECT max(Cat_ID) as maxVal FROM categories"); rs.next(); x = rs.getInt("maxVal"); for (int i = 1; i <= x; i++) { ResultSet rset = db.getdata("SELECT * FROM categories WHERE Cat_ID = '" + i + "' "); ResultSet rset2 = db.getdata("SELECT * FROM sub_categories WHERE Cat_ID = '" + i + "'"); rset.next(); %> <div id="smoothmenu1" class="ddsmoothmenu" style="width: 1000px;"> <ul> <li><a href="#"><% out.print(rset.getString("Cat_Name"));%></a> <% if (rset2 != null) { %><ul> <% while (rset2.next()) { %><li><a href="#"><% out.print(rset2.getString("Sub_Cat_Name")); %></a> </li><% }%> </ul> <% } else { %> <%}%> </li> </ul> </div> <% } } catch (Exception ex) { out.print(ex); } %>Last edited by sajithgsm; 02-22-2012 at 08:13 AM. Reason: Bold
- 02-22-2012, 09:36 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
Re: JDBC Too Many Connection Error
You never close your connections, so eventually you end up using up all the sessions that your MySQL setup will allow.
- 02-22-2012, 02:32 PM #3
Member
- Join Date
- Feb 2012
- Location
- Sri Lanka
- Posts
- 2
- Rep Power
- 0
Re: JDBC Too Many Connection Error
Can you please tell me how to close my connections? I did it to putData mothod. but, I can't do it to getData method. please help me...
- 02-22-2012, 02:52 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
Similar Threads
-
Error in JDBC Connection !
By Jack_Tauson_Sr in forum NetBeansReplies: 3Last Post: 11-29-2011, 10:14 PM -
protocol violation error in jdbc connection
By ronbigman in forum JDBCReplies: 0Last Post: 05-03-2011, 08:58 AM -
Help! JDBC Connection Error
By pryncipe in forum New To JavaReplies: 5Last Post: 03-19-2011, 04:03 PM -
JDBC connection to Cache - error
By javavick in forum New To JavaReplies: 1Last Post: 05-06-2010, 02:06 PM -
Help me this error in JDBC-ODBC connection
By lordelf in forum Java ServletReplies: 3Last Post: 04-04-2009, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks