Results 1 to 16 of 16
  1. #1
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default problem in jdbc connection

    public class Dataex extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    pw.println("java1");
    conn=DriverManager.getConnection("jdbc:mysql://localhost/employee","root"," ");
    stmt=conn.createStatement();
    rs=stmt.executeQuery("select * from student");
    while(rs.next())
    {
    String name=rs.getString("name");
    pw.println(name);
    }
    rs.close();
    stmt.close();
    conn.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }


    there is problem with database connection. the statement [ pw.println("java1");] is not getting executed. y am i getting this problem?
    Last edited by KiranA; 07-29-2011 at 08:34 AM.

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

    Default

    Either the servlet is not being called or you are getting an exception.

  3. #3
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    im not getting any exception. any statement before try block is getting executed...

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

    Default

    Ah, hang on, it's this one again.
    You haven't called flush on the writer at the end fo the method.

  5. #5
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    after seeing your post i've included the flush method. but still the print statement in DB connection is not getting exexcuted.

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

    Default

    Show us your code.
    Also, do a pw.println("Test") before going into the try block.

    Indeed, simply comment everything out and just do that, with a flush.
    Because if that doesn't work then that servlet above isn't being called.

    At this point you (not us here) need to start checking everything, and this involves pulling back to the basics and testing the servlet is called and prints anything in the response. Once you show that it gets into the servlet you then need to see where it gets to in the code, because at that point we're talking exceptions.

  7. #7
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    the print statement before the try block is getting printed. from the class.forName statement onwards nothing is getting printed.

    the code is



    import java.sql.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class Dataex extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    pw.println("java1");
    conn=DriverManager.getConnection("jdbc:mysql://localhost/employee","root"," ");
    stmt=conn.createStatement();
    rs=stmt.executeQuery("select * from student");
    while(rs.next())
    {
    String name=rs.getString("name");
    pw.println(name);
    }
    rs.close();
    stmt.close();
    conn.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    pw.flush();
    }
    }

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

    Default

    So you're getting an exception then.
    That is the only other option.
    It'll be in your logs somewhere.
    Probably a class not found one.

  9. #9
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    How should i make my code to connect to the database?

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

    Default

    Not sure what you're asking.
    The above code is correct...I'm assuming you simply haven't got the MySQL JDBC library in the correct place, but you haven't told us your exception.

  11. #11
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    there is no exception displayed. the [ Class.forName("com.mysql.jdbc.Driver");] statement is not getting executed. the database is not getting connected. the simple print statement after the try block is not getting executed.

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

    Default

    It won't get displayed!
    You are printing the stack trace to wherever System.out is pointing, which will likely be your log files.
    As I said a month ago.
    If that servlet is being executed (and you said a pw.println("Test") prior to the try block was being displayed, so it is being executed) the only way it will not reach that first ps.println() in the try block is if the Class.forName() is throwing an exception.

    You can choose to believe me or not, but until you can show me something else to prove it is somehow skipping that try block then I'm afraid this is as far as we can go.

    Find your log files, because that stack trace is going somewhere. Hell, do a pw.println("EXCEPTION!!") in the catch block as well as the stack trace if you must.

  13. #13
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    the problem is solved ...thanks for the help.

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

    Default

    And the problem was?

  15. #15
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    code was absolutely fine...i didnt add the driver jar file in the webapps diretory of tomcat.

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

Similar Threads

  1. JDBC Connection Creation Problem.Help needed?
    By sandeepsai39 in forum New To Java
    Replies: 2
    Last Post: 06-24-2011, 10:20 AM
  2. JDBC Connection problem.
    By chyrl in forum Advanced Java
    Replies: 14
    Last Post: 09-22-2010, 08:33 AM
  3. Problem in jdbc connection with servlets
    By illusion1912 in forum Java Servlet
    Replies: 0
    Last Post: 01-06-2009, 07:34 PM
  4. jdbc connection problem
    By Assaf A in forum JDBC
    Replies: 0
    Last Post: 12-01-2008, 05:08 PM
  5. JDBC driver connection problem
    By creativehacker in forum JDBC
    Replies: 3
    Last Post: 07-10-2007, 09:58 PM

Posting Permissions

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