简体   繁体   中英

JDBC - java can't see installed drivers

I have a problem with JDBC drivers. I can't connect with my SQL Server database. Following code for test:

public class Test {

public static void main(String[] args) {

       Connection con = null;
       String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=mydb; user=root; password=psswd;";

   try {
    con = DriverManager.getConnection(conUrl);
    System.out.println("OK");
  } catch (Exception e) { e.printStackTrace(); }
         finally {
           if (con != null) try { con.close(); } catch(Exception e) {}
         }
}}

When i try run this code i still getting error:

java.sql.sqlexception no suitable driver found for (..)

I have added path to sqljdbc4.jar to classpath variable and enu\\auth\\x64 localization to Path variable. I'm working on JRE 1.8, SQL Server 2014 and Windows 7.

It's because you haven't loaded the driver. Just modify your existing code

try {
 //this will load the driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

con = DriverManager.getConnection(conUrl);
System.out.println("OK");
 } catch (Exception e) { e.printStackTrace(); }
     finally {
       if (con != null) try { con.close(); } catch(Exception e) {}
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM