简体   繁体   中英

java database connection end up with the db:java.sql.SQLException: No suitable driver found

i am brand new to Java . Here i am trying to connect to mysql database here my code: package Services;

package Services;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class LoginService {
     private static final String USERNAME="root";
     private static final String PASSWORD="";
     private static final String CONNECTION_STRING="jdbc:mysql://localhost/testdb";

    public void  Authenticate(String uname,String password){

        Connection connection=null;
        Statement statement=null;
        ResultSet result =null;
        try{
            //Class.forName("com.mysql.jdbc.Driver").newInstance();//if java 6 or higher there is no need to load cass      
        connection =(Connection)DriverManager.getConnection(CONNECTION_STRING,USERNAME,PASSWORD);
        System.out.println("Connection to the database is established ");
        }catch(SQLException e){
            System.out.println("Can't connect to db:" +e );

        }   

    }


}

This end up with an error like this:

Can't connect to db:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/testdb

i have added the jar file and by right clicking in it added the same to build path;what wrong with my code .

You have to download the JDBC driver .. after that ... add it to your library.. You can read this! It's really simple after you get the idea! here is the link! http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html

And this one! http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

Good luck!

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