2015-05-21 199 views
-1

当我尝试用下面的代码连接到我的数据库:错误连接到SQL Server数据库

try 
{ 
    String connectionUrl = "jdbc:sqlserver://192.168.0.78;instanceName=SQL2014;databaseName=GIDS;user=sa;password=invent"; 
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
    Connection con = DriverManager.getConnection(connectionUrl); 
    System.out.println("Database connection = "+con); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
    System.out.println("Error connecting to database. Error: "+e.getMessage()); 
} 

我得到以下异常:

Error: com.microsoft.sqlserver.jdbc.SQLServerDriver

我在做什么错?

+0

编译期间没有错误 –

+3

为什么'php'在标签中? –

+1

添加完整的堆栈跟踪。 – Jens

回答

0

尝试这个代码

try 
{ 
    String username = "sa"; 
    String password = "invent"; 
    String connectionUrl = "jdbc:sqlserver://192.168.0.78;instanceName=SQL2014;databaseName=GIDS"; 
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
    Connection con = DriverManager.getConnection(connectionUrl,username,password); 
    System.out.println("Database connection = "+con); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
    System.out.println("Error connecting to database. Error: "+e.getMessage()); 
} 
0

确保你有实现类(驱动程序jar)以连接o MS SQL类路径罐子。