1

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 .

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1023696
  • 43
  • 1
  • 2
  • 6
  • 1
    Uncomment the line for driver lookup. – Narendra Pathai Aug 02 '13 at 07:12
  • possible duplicate of [java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver](http://stackoverflow.com/questions/5616898/java-sql-sqlexception-no-suitable-driver-found-for-jdbcmicrosoftsqlserver) – Narendra Pathai Aug 02 '13 at 07:13
  • also: for mysql there is 2 string which loads the driver, one iscommented, sometimes nort loading others starts with "org...." google it! –  Aug 02 '13 at 07:14
  • 2
    You need to put the JDBC driver for your database in the classpath when you run your program. – Jesper Aug 02 '13 at 07:15
  • And don't use com.mysql.jdbc.* classes. Use java.sql.* classes. – JB Nizet Aug 02 '13 at 07:16
  • mysql port number is 3306. are you using a different port. check CONNECTION_STRING – Learn More Aug 02 '13 at 08:11

1 Answers1

0

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!

Peteanv
  • 16
  • 1
  • the problem is i am in a web project so it requires a jar file in the webinf/lib/ folder try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("Where is your MySQL JDBC Driver?"); e.printStackTrace(); return; } this try catch helps to resolve the problem Thank you all... – user1023696 Aug 05 '13 at 13:22