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.
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.
}
See also
- Exceptions tutorial
- JDBC+MySQL mini tutorial
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.
Install a database locally and connect to that.
You can check the connection to the server by using telnet to connect to the port where the database should be listening; if you can open a connection, you know there isn't a network problem (such as a firewall). If JDBC is okay and the network is okay, then you can try connecting to the remote system. If that doesn't work, it's some kind of configuration problem.