Results 1 to 14 of 14
  1. #1
    rahulraj is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Thumbs up Please Help...CommunicationsException

    I am using tomcat 5.5 for running some jsp code(login.jsp). The code is given below:

    Java Code:
    [B] <%@ page language="java" import="java.sql.*" %>[/B]
     <html>
    
     <body background="file:///C:/Users/RAHUL/hack/180%20National%20Geographic%20Wallpapers%201600%20X%201200/180%20Stunning%20National%20Geographic%20Wallpapers%20%5B2010-2011%5D%201600%20X%201200/180%20Stunning%20National%20Geographic%20Wallpapers%20%5B2010-2011%5D%201600%20X%201200/Wallpapers/25.jpg">
    
     <form method="post">
     <p> Enter your username :  <input type="text" name="username"/> </p>
     <p> Enter your password :  <input type="text" name="password"/> </p>
     <input type="submit" value="Login"/> 
    
     [B]<%
        String usn = request.getParameter("username");
    	String pass = request.getParameter("password");
    
    	try{
    	     Class.forName("com.mysql.jdbc.Driver");
    		 String url = "jdbc:mysql://localhost:3306/rahulraj";
    		 Connection con = DriverManager.getConnection(url, "admin", "amma123");
    		 Statement s = con.createStatement();
    		 
    		 ResultSet rs = s.executeQuery("select * from temp");
    		 while(rs.next())
    		 {
    		   String u = rs.getString(1);
    		   String p = rs.getString(2);
    		   if(usn.equals(u) && pass.equals(p))
    		   {
    		      out.println("login successfull");
    			  }
    		   else
               {
                  out.println("login failed");
                 }
               }
            }
    		
    		catch(Exception e)
    		{
    		   out.println("Error "+e);
    		   }[/B]
     			  
     %> 
     </form> 
     </body>
     </html>

    DBMS is mysql version 5.1.41
    jdk version - 1.6


    When login.jsp loads, it shows the following error:

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ption: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

    Can anyone help me to resolve this?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Do not use scriptlets like that in a JSP and definately do not create a new connection on every call to the page, use a connection pool and, at the least, the JSTL sql package.

  3. #3
    rahulraj is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    I am a beginner, can you explain more? I have to implement this in a project...

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    All the more reason not to do things the way you are.


    The J2EE(TM) 1.4 Tutorial
    - The Java EE 5 Tutorial
    - The Java EE 6 Tutorial

    see the link for the appropriate version.

  5. #5
    rahulraj is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    Still the question is unanswered..."why communication exception occurred?"

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Your MySQL is probably not letting you login.
    Have you told MySQL to allow access from localhost for that user?

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That error usually means that you cannot reach the db server at all, probably due to a firewall dropping packets.

  8. #8
    rahulraj is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    Your MySQL is probably not letting you login.
    Have you told MySQL to allow access from localhost for that user?
    How to do that? How to resolve this?

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by rahulraj View Post
    How to do that? How to resolve this?
    ask at a mysql forum, that is a mysql question.

  10. #10
    rahulraj is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    The reason why asked is

    Connection con = DriverManager.getConnection(url, "admin", "amma123");
    I think this is enough for getting connection to database. I have working jdbc program with me. I used the same code here also.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default

    Not if there is a firewall between you and the server, or if the database itself isn't setup to accept your connection.

  12. #12
    rahulraj is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Not if there is a firewall between you and the server, or if the database itself isn't setup to accept your connection.
    The jdbc code is changed for working with MS ACCESS and that error is resolved. But new error came!

    SQLException : General Error

    Changed code is given below:

    Java Code:
    	try{
    	     
    	     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    		 String url = "jdbc:odbc:rahul";
    		 Connection con = DriverManager.getConnection(url, "", "");
    		 Statement s = con.createStatement();
    		 
    		 ResultSet rs = s.executeQuery("select * from customers");
    		 while(rs.next())
    		 {
    		   String u = rs.getString(1);
    		   String p = rs.getString(2);
    		   if(usn.equals(u) && pass.equals(p))
    		   {
    		      out.println("login successfull");
    			  }
    		   else
               {
                  out.println("login failed");
                 }
               }
            }
    		
    		catch(SQLException e)
    		{
    		   out.println("sql error");
    		   }
    		   
    		  
    		catch(Exception e)
    		{
    		   out.println("Error"+e);
    		   }

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Why? Why? Why?

    Why the h_ll would you want to exchange Oracle for Access? Fix your problems do not run away from them.

    As far as the exception goes, ask microsoft what it means, and good luck with that.

  14. #14
    rahulraj is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    can anyone say how "SQLException: General Error" occurs? There is no wrong sql statements in my code.

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
  •