0
votes

I have a problem connecting to MySQL databse from servlet. Here is my connection code

private static void connectToDatabase(){
    try{
        static Connection conn = null;
        static String url = "jdbc:mysql://localhost/database?userinfo"; 
        conn = DriverManager.getConnection(url);
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Connected");
    } catch(ClassNotFoundException wyjatek) {
        System.out.println("Problem ze sterownikiem");
    } catch(SQLException wyjatek) {
        System.out.println("SQLException: " + wyjatek.getMessage());
        System.out.println("SQLState: " + wyjatek.getSQLState());
        System.out.println("VendorError: " + wyjatek.getErrorCode());
    }
}

and doGet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    String param = request.getParameter("lookFor");

    out.println("TEST");

    connectToDatabase();
 }

and everytime I'm getting this

SQLException: No suitable driver found for jdbc:mysql://localhost/database?userinfo
SQLState: 08001
VendorError: 0

I put the mysql-connector-java-5.1.27-bin in webapps\WEB-INF\lib and apache-tomcat-7.0.55\lib

and it's still the same. In the desktop version of application everything works fine. It's happening only on servlet.

1
which jar did you put in tomcat/lib?Scary Wombat
Only putting mysql-connector-java-5.1.27-bin in lib wont work , have you added in classpath ?Ninad Pingale
Shouldn't this be configured via the app/container XML?MadProgrammer
Have tried calling Class.forName("com.mysql.jdbc.Driver") before calling DriverManager.getConnection(url)?Jack
@scarywombat mysql-connector-java-5.1.27-bin.jarkorek

1 Answers

4
votes

The basic problem is

Your first step should be registering the Driver using this

Class.forName("com.mysql.jdbc.Driver");

Then try to get connection from DriverManager using this

conn = DriverManager.getConnection(url);

I guess you are doing it the reverse way