-3

Why application cannot find jdbc driver?

//  TODO Auto-generated method stub
Connection connection = null;
try {
    // String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
    String serverName = "serverName";
    String databaseName = "databaseName";
    String portNumber = "portNumber";
    String myDatabase = serverName + ":" + portNumber;
    String domainName = "domain name";
    String url = "jdbc:jtds:sqlserver://" + myDatabase + ";Database=" + databaseName + ";domain=" + domainName; // a JDBC url
    String username = "username";
    String password = "password";

    // Load the JDBC driver
    Class.forName(driverName);

    // Create a connection to the database
    connection = DriverManager.getConnection(url, username, password);


    // Execute a query
    Statement stmt = connection.createStatement();
    String sql;
    sql="SELECT CODFISC, SURNAME FROM tbPersonale order by SURNAME;";
    ResultSet rs = stmt.executeQuery(sql);

    // Estrazione Dati  
    while(rs.next() ) {

        // Legge i valori
        String CODFISC = rs.getString("CODFISC"); //CODFISC is the fiscal code
        String SURNAMENAME = rs.getString("SURNAME");

        // Visualizza i dati
        System.out.print("Codice Fiscale: " + CODFISC );
        System.out.println("SURNAME and NAME: " + SURNAMENAME );

    }

} catch (ClassNotFoundException e) {
    System.out.println("Could not find the database driver"); // here is where i always end up :(
} catch (SQLException e) {
    System.out.println("Could not connect to the database");
}   
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158

1 Answers1

1

Since you apparently want to connect to a SQL Server using the JTDS driver make sure

  • that the JTDS jar is in the classpath
  • and you use the correct driver class name.

Right now you are using the ODBC Bridge driver (which was removed in Java 8), instead you want

String driverName = "net.sourceforge.jtds.jdbc.Driver";
wero
  • 30,527
  • 3
  • 46
  • 72