如何在Java中调试SQL连接

如何在Java中调试SQL连接,java,sqlconnection,Java,Sqlconnection,在我的网站,我需要连接到一个数据库。但我做不到,它不起作用,当我做的时候 try { connexion = DriverManager.getConnection(url, name, password); } catch (SQLException sqlex) { System.err.println("Connexion impossible - LoginServlet"); } 当我打印connexion时,它返回我null System.out.println(c

在我的网站,我需要连接到一个数据库。但我做不到,它不起作用,当我做的时候

try {
    connexion = DriverManager.getConnection(url, name, password);
} catch (SQLException sqlex) {
    System.err.println("Connexion impossible - LoginServlet");
}
当我打印
connexion
时,它返回我
null

System.out.println(connexion);
url、名称和密码是有效的,因为我在其他应用程序中使用它们。那么问题从何而来

编辑

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

我使用的是Mistrosoft SQL Server,它看起来像是未创建的连接对象。可能存在一些异常,如未加载驱动程序类

尝试捕获异常和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);
}

您是否加载了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");
}

尝试在catch块中转储sqlex。是否出现异常?System.err.println(“连接不可能-LoginServlet”)是否打印?那么如何打印connexion,您也能给我们看一下这段代码吗?在catch块中,执行
sqlex.printStackTrace()
并向我们显示消息。另外,请显示URL,可能URL语法错误。@Raf无异常,并且System.err.println(“Connexion不可能-LoginServlet”)未打印。打印Connexion是什么意思?是的,我加载了驱动程序类