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) {}
}
也可以看看