Results 1 to 4 of 4
  1. #1
    sajithgsm is offline Member
    Join Date
    Feb 2012
    Location
    Sri Lanka
    Posts
    2
    Rep Power
    0

    Default 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.


    Java Code:
    com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException:
    Data source rejected establishment of connection, message from server: "Too many connections"
    please help me...

    My JDBC class 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;
        }
    }
    And, My Category JSP page's code is,

    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);
        }
    %>
    Please tell me if there has any of error and I want to correct and learn this method very clearly guys... thanks a lot...
    Last edited by sajithgsm; 02-22-2012 at 08:13 AM. Reason: Bold

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default 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.

  3. #3
    sajithgsm is offline Member
    Join Date
    Feb 2012
    Location
    Sri Lanka
    Posts
    2
    Rep Power
    0

    Default 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...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: JDBC Too Many Connection Error

    You didn't do it in putData.
    You only closed the statement.

    You need to close the connection you create each time you call getConnection.
    In order to do that you'll have to assign it to a Connection variable.

Similar Threads

  1. Error in JDBC Connection !
    By Jack_Tauson_Sr in forum NetBeans
    Replies: 3
    Last Post: 11-29-2011, 10:14 PM
  2. protocol violation error in jdbc connection
    By ronbigman in forum JDBC
    Replies: 0
    Last Post: 05-03-2011, 08:58 AM
  3. Help! JDBC Connection Error
    By pryncipe in forum New To Java
    Replies: 5
    Last Post: 03-19-2011, 04:03 PM
  4. JDBC connection to Cache - error
    By javavick in forum New To Java
    Replies: 1
    Last Post: 05-06-2010, 02:06 PM
  5. Help me this error in JDBC-ODBC connection
    By lordelf in forum Java Servlet
    Replies: 3
    Last Post: 04-04-2009, 08:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •