0

I am running Ubuntu 16.04 and using 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)

I have installed SQL Server jdbc driver (sqljdbc_6.2.2.0_enu.tar.gz in https://www.microsoft.com/en-us/download/details.aspx?id=55539)

$ 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

I wrote some simple code to test using the driver :

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

Compilation is fine:

$ javac BasicOperations.java

Running isn't.

$ 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)

How shall I solve the problem?

Is it because I am using Java 9 and SQL Server jdbc driver has drivers for jre7 and jre8?

Thanks.

1 Answers1

0

Looks like format of database connection URL is incorrect. Please check https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url

To be specific: microsoft: should be removed from URL. This is the format:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;p‌​roperty=value[;prope‌​rty=value]]
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ivan
  • 8,508
  • 2
  • 19
  • 30
  • Thanks. Can you be more specific? –  Dec 05 '17 at 21:44
  • Thanks. That solves my problem. But I now has a TCP connection problem https://stackoverflow.com/q/47663342/1224441 Appreciate if you can take a look. –  Dec 05 '17 at 21:59