【问题标题】:Why application cannot find JDBC driver?为什么应用程序找不到 JDBC 驱动程序?
【发布时间】:2016-11-02 09:35:25
【问题描述】:

为什么应用程序找不到 jdbc 驱动程序?

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

【问题讨论】:

标签: java database jdbc jtds


【解决方案1】:

既然您显然想使用 JTDS 驱动程序连接到 SQL Server,请确保

  • JTDS jar 在类路径中
  • 并且您使用了正确的驱动程序类名。

现在您正在使用 ODBC Bridge 驱动程序(在 Java 8 中已删除),而不是您想要

String driverName = "net.sourceforge.jtds.jdbc.Driver";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2015-06-15
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    相关资源
    最近更新 更多