简体   繁体   中英

No suitable driver found for jdbc:mysql://localhost/database

I am asking the same question,because i didn't find the answer.Here is my problem. I've been trying to use jdbc driver to connect to mysql database. Here is my main sql handler class :

package com.arslanjava.model;

import java.sql.*;

public class SqlHandler {
    private Connection myConnection;
   // final private String serverName = "localhost" ;
    //final private String port = "3306" ;
    //final private String user = "root" ;
   // final private String password = "password" ;

    public SqlHandler ( String username , String password , String fname , String lName ) throws SQLException {
        if ( username != null  && password != null && fname != null && lName != null ) {

            //Establish connection to the server.
            this.establishConnection();

            //execute the addUser method
            this.addUser(username,password,fname,lName);

            //close this connection
            this.close();


        } else {
            throw new NullPointerException () ;
        }
    }


    public SqlHandler()  {

    }

    public void addUser (String us , String pas , String fName , String lName ) throws SQLException {
        PreparedStatement statement = myConnection.prepareStatement("INSERT INTO user_info (username, password, first_name, last_name) VALUES (?,?,?,?)");
        statement.setString(1,us);
        statement.setString(2,pas);
        statement.setString(3,fName);
        statement.setString(4,lName);
        statement.executeUpdate();
    }

    public void establishConnection () throws SQLException {
        myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/app1","root","password") ; ;
    }

    public void close() {
        if (myConnection != null) {
            try {
                myConnection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

Everything works fine,when i use it in plain java code.For example,this works fine :

package com.arslanjava.model;

import com.arslanjava.model.SqlHandler;

import java.io.IOException;
import java.net.ServerSocket;
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) {
        SqlHandler handler = new SqlHandler() ;
        try {
            handler.establishConnection();
            handler.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

But,when i try to use it in my servlet classes i get an error that says :
No suitable driver found for jdbc:mysql://localhost/name_of_my_database .
I checked the answer in the previous question,it was saying that this was caused by 2 things:

  1. Wrong url,which i checked and it was fine.(jdbc:mysql://localhost:3306/app1)
  2. I didn't add jar file in tomcat's lib folder,which i did,but i still get the same error.(screenshots: https://prnt.sc/k2qlxa , https://prnt.sc/k2qm1e,http://prntscr.com/k2qm5h )
    Also i added this jar file to the WEB-INF/lib folder,which ,also,didn't work.
    Is there anything else i can try?Please,help me understand the problem here,because i can't fix this....

Here is my servlet class :

package com.arslanjava.web;

import com.arslanjava.model.SqlHandler;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;

public class MyRegServlet extends HttpServlet {

    @Override
    public void doPost (HttpServletRequest request , HttpServletResponse response) {
        String username = request.getParameter("us") ;
        String password = request.getParameter("psw") ;
        String first_name = request.getParameter("fname");
        String last_name = request.getParameter("lname") ;
        try {
            SqlHandler handler = new SqlHandler(username,password,first_name,last_name) ;
        } catch (SQLException e) {
            request.setAttribute("errorCode","5xx");
            request.setAttribute("errorMessage",e.toString());
            e.printStackTrace();
            RequestDispatcher dispatcher = request.getRequestDispatcher("customErroPage.jsp") ;
            try {
                dispatcher.forward(request,response);
            } catch (ServletException | IOException e1) {
                e1.printStackTrace();
            }
        }
    }

}

Have you tried using this line?

Class.forName("com.mysql.jdbc.Driver");// include this line in your code.

If not try adding this line in this function.

public void establishConnection() throws SQLException {
   myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/app1","root","password");
}

The only thing that solved this for me was using Class.forName

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

Of course, this is supposed to have become redundant back in 1923 but having put mysql-connector jar file just about everywhere, this is the only thing that worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM