"没有为jdbc找到合适的驱动程序:microsoft:sqlserver"

时间:2017-12-05 20:52:03

标签: java sql-server ubuntu jdbc

我正在运行Ubuntu 16.04并使用Oracle Java 9。

$ java --version
java 9.0.1
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

我在https://www.microsoft.com/en-us/download/details.aspx?id=55539

中安装了SQL Server jdbc驱动程序(sqljdbc_6.2.2.0_enu.tar.gz
$ ls /home/t/program_files/RDBMS/JDBC/mssqlserverjdbc_6.0/enu/jre*
/home/t/program_files/RDBMS/JDBC/mssqlserverjdbc_6.0/enu/jre7:
sqljdbc41.jar

/home/t/program_files/RDBMS/JDBC/mssqlserverjdbc_6.0/enu/jre8:
sqljdbc42.jar

我写了一些简单的代码来测试使用驱动程序:

// JDBC driver name and database URL                                                                                                                                       
String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// static final String DB_URL = "jdbc:mysql://localhost/"; // connect to a DBMS                                                                                            
String DB_URL = "jdbc:microsoft:sqlserver://localhost/STUDENTS"; // connect to a specific database in a DBMS                                                               
//  Database credentials                                                                                                                                                   
String USER = "SA";
String PASS = "password";

//Register JDBC driver                                                                                                                                                     
try{
    Class.forName(JDBC_DRIVER);
}catch(Exception e){ //Handle errors for Class.forName                                                                                                                     
    e.printStackTrace();
}

    //Open a connection                                                                                                                                                    
System.out.println("Connecting to database...");

try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); // connect to DBMS, not to a DB                                                                     
    Statement st = conn.createStatement()){

}catch(SQLException se){ //Handle errors for JDBC                                                                                                                          
    se.printStackTrace();
}

编译很好:

$ javac BasicOperations.java

跑步不是。

$ java -cp .:/home/t/program_files/RDBMS/JDBC/mssqlserverjdbc_6.0/enu/jre8/sqljdbc42.jar BasicOperations

Connecting to database...
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost/STUDENTS
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:703)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
        at BasicOperations.main(BasicOperations.java:46)

我该如何解决这个问题?

是因为我使用Java 9而SQL Server jdbc驱动程序是否有jre7和jre8的驱动程序?

感谢。

1 个答案:

答案 0 :(得分:0)

看起来数据库连接URL的格式不正确。 请检查https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url

具体来说:应从网址中删除microsoft:。格式如下:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;p‌​roperty=value[;prope‌​rty=value]]