Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Can't connect Servlet to MySQL DB

Options
  • 09-03-2005 2:32am
    #1
    Registered Users Posts: 3,012 ✭✭✭


    Hi,

    having trouble getting a HttpServlet to connect to my MySQL db.

    I isolated that the Class.forName() function used to obtain the jdbc driver is throwing the exception.
    I dont know why, but this function doesn't seem to work in a HttpServlet.
    I've tried establishing the connection and executing queries via a method, from another class file, called in my servlet, but this doesn't work either.
    Once it reaches the Class.forName() function it fails.
    The code for establishing the connection works fine on its own, just not as a method or as part of the HttpServlet.

    If anyone could explain why it wont work, or suggest an alternantive way I'd really appretiate it.

    Cheers


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Can you post some code or an exception stack trace? Have you got the MySQL lib in the WEB-INF/lib folder?


  • Registered Users Posts: 3,012 ✭✭✭BizzyC


    Here's my code. Have the MySQL lib in it's default location.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Change this:
    try {Class.forName("com.mysql.jdbc.Driver");}
    catch (ClassNotFoundException e) {}
    
    to this:
    try {Class.forName("com.mysql.jdbc.Driver");}
    catch (Exception e) {
    System.out.println("Error: " + e.getMessage());
    e.printStackTrace();
    }
    
    and post the error that's displayed. You say the lib is in it's default location, where's that?


  • Registered Users Posts: 3,012 ✭✭✭BizzyC


    in the MySQL server location. What exactly should I move to web-inf?

    It's giving me an error message for e.getMessage(), but the stackTrace wont print cause I can't call system.out.prints, I had to put the message into a string before it would print.

    Here's the message: Error: com.mysql.jdbc.Driver


  • Closed Accounts Posts: 24 colonelx


    Hi,

    the mysql connector jar file should reside in WEB-INF/lib directory, this is the underlying jar file which does the work of actually connecting to the mysql database.

    Ensure that you have the correct jar file for the version of the mysql server that you are using.

    As a matter of interest whats the version of the mysql jar file that you are using ?


  • Advertisement
  • Registered Users Posts: 3,012 ✭✭✭BizzyC


    Got it! At last.

    Cheers guys. turns out it was just that misplaced jar.


  • Registered Users Posts: 3,012 ✭✭✭BizzyC


    Another problem now.
    I've changed the code to print a string stating wether or not the username and password is in the db, but i'll only get the right answer on the first attempt.
    It'll continually return false unless I recompile the file after each attempt.


Advertisement