我尝试在家里和Java SDK一起安装它… SDK工作正常,我现在可以使用命令提示符将java程序编译成类…
但是,我不确定如何测试JDBC是否正在连接到我的server / databases / MysqL.
我感觉我的服务器(这是一个共享的网站/ webhost)可能不允许连接…
如何测试JDBC是否已正确安装而无需连接到服务器?
然后,我如何测试(请分开代码)(现已确认工作)JDBC连接到我的数据库?
非常感谢.
最佳答案
How can I test that the JDBC was installed correctly without having to connect to a server?
只需检查JDBC驱动程序上的Class#forName()是否不会抛出ClassNotFoundException.
try {
Class.forName(driverClassName);
// Success.
} catch (ClassNotFoundException e) {
// Fail.
}
And then how can I test (Seperate code please) that the (now confirmed working) JDBC is connecting to my databases?
只需检查DriverManager#getConnection()或DataSource#getConnection()是否不会抛出sqlException.
Connection connection = null;
try {
connection = DriverManager.getConnection(url,username,password);
// Success.
} catch (sqlException e) {
// Fail.
} finally {
if (connection != null) try { connection.close(); } catch (sqlException ignore) {}
}
也可以看看