I tried installing this at home along with Java SDK... The SDK worked fine and I can now use the command prompt to compile java programs into classes...
However I am unsure how to test if JDBC is working to connect to my server/databases/mysql.
As I have a feeling my server (Which is a shared website/webhost) may not allow the connection...
How can I test that the JDBC was installed correctly without having to connect to a server?
And then how can I test (Seperate code please) that the (now confirmed working) JDBC is connecting to my databases?
Thanks alot.
The version of the installed Microsoft JDBC Driver for SQL Server can be found in the following ways: Call the SQLServerDatabaseMetaData methods getDriverMajorVersion, getDriverMinorVersion, or getDriverVersion. The version is displayed in the readme. txt file of the product distribution.
First, it submits a validation query to the database. Second, it uses the timeout parameter as a threshold for the operation. Finally, the connection is marked as valid if the operation succeeds within the timeout.
How can I test that the JDBC was installed correctly without having to connect to a server?
Just check if Class#forName()
on the JDBC driver doesn't throw 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?
Just check if DriverManager#getConnection()
or DataSource#getConnection()
doesn't throw SQLException
.
try (Connection connection = DriverManager.getConnection(url, username, password)) {
// Success.
}
catch (SQLException e) {
// Fail.
}
First, download MySQL's JDBC driver and put it somewhere in your application's classpath.
Second, try to register that driver in your Java code, using
Class.forName("com.mysql.jdbc.Driver");
If that doesn't throw an exception, you've managed to register sucessfully.
Third, check if your connection works:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","user", "pass");
Substitute your URL, username and password as needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With