No suitable drivers found for jdbc in java

advertisements

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.


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