如何在Java中调试SQL连接

时间:2015-12-03 15:52:39

标签: java sqlconnection

在我的网站中,我需要连接到数据库。但是,当我这样做时,我不能,它不起作用

try {
    connexion = DriverManager.getConnection(url, name, password);
} catch (SQLException sqlex) {
    System.err.println("Connexion impossible - LoginServlet");
}

当我打印connexion时,它会返回null

System.out.println(connexion);

网址,名称和密码有效,因为我在其他应用程序中使用它们。那么问题可以从何而来?

编辑

url = jdbc:sqlserver://trixsql03;databaseName=TrixnetDev;

我正在使用Miscrosoft SQL Server

2 个答案:

答案 0 :(得分:0)

查找未创建的连接对象。可能会有一些异常,比如没有加载Driver类。

尝试捕获Exception以及SQLException。你应该看到真正的问题。

try {
connexion = DriverManager.getConnection(url, name, password);
} catch (SQLException sqlex) {
  System.err.println("Connexion impossible - LoginServlet"+sqlex);
}catch (Exception sqlex) {
  System.out.println("Connexion impossible - LoginServlet"+sqlex);
}

答案 1 :(得分:0)

您是否加载了sql驱动程序类?它是哪种类型的数据库?例如,使用sql-server,它将是:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
try {
    connexion = DriverManager.getConnection(url, name, password);
} catch (SQLException sqlex) {
    System.err.println("Connexion impossible - LoginServlet");
}