No suitable driver found for jdbc in java

11,866

Solution 1

First, you need to do this:

private final String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

try {
    Class.forName(dbDriver).newInstance(); // use java reflection to load the database driver
} catch (Exception ex) {
    System.out.println("failure");
}

Solution 2

Your problem is not in the code, it's that the jar file containing the JDBC driver class for sqlserver is not in the classpath when you execute your program.

Visit the appropriate sqlserver web site (google) and download the JDBC driver jar and put it in your build path if executing in an IDE, or in the same directory as your program jar if executing from the command line.

Share:
11,866
sahar
Author by

sahar

MSc of Artificial Intelligence From the Alzahra University

Updated on June 04, 2022

Comments

  • sahar
    sahar almost 2 years

    Im new in sql server, and want to create connection between java and sql server. my connection code is:

     public static void main(String[] args) {
    
        Connection con;
           try {
    
           String connectionUrl = "jdbc:sqlserver://HELLO-PC:1433; databaseName=Attendance Teachers;";
    
            con = DriverManager.getConnection(connectionUrl, "", "");
            System.out.println("connected");
            java.sql.Statement st = con.createStatement();    
          }
            catch (SQLException ex) {
    
            Logger.getLogger(AttendanceTeachers.class.getName()).log(Level.SEVERE, null, ex);
        }
    

    my server name is 'HELLO-PC' an i also add sqljdbc.jar. i see error:

    Feb 01, 2013 11:24:46 AM attendance.teachers.AttendanceTeachers main
    SEVERE: null
    java.sql.SQLException: No suitable driver found for jdbc:sqlserver://HELLO-PC:1433;   databaseName=Attendance Teachers;
    at java.sql.DriverManager.getConnection(DriverManager.java:604)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at attendance.teachers.AttendanceTeachers.main(AttendanceTeachers.java:30)
    

    I realy need help.thanks.

  • sahar
    sahar about 11 years
    i add jdbc.jar file in 'add external jars' from project properties . i also use netbeans.
  • Bohemian
    Bohemian about 11 years
    I have news for you. This does absolutely nothing useful towards solving the problem, except that you'll throw an exception earlier than when you use the driver class. It is not necessary to do this. For some reason many examples on the web do this, but it's not necessary - the class loader will find the class without this code