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.
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
please help me...
My JDBC class is
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,
<%@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...