【发布时间】:2019-04-14 14:00:29
【问题描述】:
我有以下代码可以连接到我在 SQL Server 中的数据库:
public static void main (String [] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://DESKTOP-G8057U8\\SQLEXPRESS:1433;databaseName=Vaporizadores;integratedSecurity=true;";
DBConection=DriverManager.getConnection(url);
DBStatement = DBConection.createStatement();
System.out.println("Conexion exitosa");
}catch(ClassNotFoundException | SQLException e) {
System.out.println("Conexion fallida");
}
}
当我运行它时,我得到“Conexion fallida”,这意味着捕获了一个异常。我尝试了不同的网址,但仍然无法连接到我的数据库。我对jdbc和sql server知之甚少,所以可能是个愚蠢的错误。
【问题讨论】:
-
做一个,catch(ClassNotFoundException | SQLException e) { System.out.println(e); } 并给出结果...
-
不要同时指定端口 (1433) 和实例名称 (SQLEXPRESS)。它们是相互排斥的。删除端口号。
-
@dilanSachi 更好的是:
catch(ClassNotFoundException | SQLException e) { e.printStackTrace(System.out); } -
integratedSecurity=true需要 PATH 中的.dll文件。有吗? -
@Andreas thanx 给小费
标签: java sql-server eclipse jdbc