简体   繁体   中英

No suitable driver found for jdbc in java

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.

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.

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");
}

This issue will normally occur when you are using a older verison of JDBC driver if you are working with JDBC 4.0

When you download the SQlserver JDBC driver from the Microsoft website, you will get two jar files: sqljdbc4.jar and sqljdbc.jar.

1.sqljdbc.jar: Provides support for JDBC 3.0 specification

2.sqljdbc4.jar: Provides support for JDBC 4.0 specification

So.check with sqljdbc4.jar

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