My name is Zemian Deng, and I am a Senior Application Engineer working at Oracle for the Enterprise Knowledge Management product. NOTE: The views expressed on my blog and social network are my own and do not necessarily reflect the views of my employer. Zemian is a DZone MVB and is not an employee of DZone and has posted 77 posts at DZone. You can read more from them at their website. View Full User Profile

Checking DB Connection Using Java

12.14.2012
| 7616 views |
  • submit to reddit

For the sake of completeness, here is a Java version of the Groovy post to test your Oracle Database connection.  

package atest;
import java.sql.*;
/**
 * Run arguments sample:
 * jdbc:oracle:thin:@localhost:1521:XE system mypassword123 oracle.jdbc.driver.OracleDriver
 */
public class DbConn {
    public static void main(String[] args) throws Exception {
        String url = args[0];
        String username = args[1];
        String password = args[2];
        String driver = args[3];

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("SELECT SYSDATE FROM DUAL");
            while(rs.next()) {
                System.out.println(rs.getObject(1));
            }
        } finally {
            conn.close();
        }
    }
}
Published at DZone with permission of Zemian Deng, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Tomasz O. replied on Fri, 2012/12/14 - 4:23am

I do not have any experience with databases and handling them from java, but I am curious why are you doing this:

Class.forName(driver)

I mean why do you create object and don't store a reference to it?

Xavier Dury replied on Fri, 2012/12/14 - 4:40am in response to: Tomasz O.

Loading a Driver class triggers a static initialization block inside that class which registers the Driver into the DriverManager (so DriverManager knows which driver to use when it encounters URLs like jdbc:oracle:...). Hence, loading that class with Class.forName(xxx) is sufficient. Since JDBC 4.0, this is not needed anymore as driver jars must include a file META-INF/services/java.sql.Driver which contains the driver class name. DriverManager can then autodiscover the drivers classes by reading all META-INF/services/java.sql.Driver accessible via the current ClassLoader.

Tomasz O. replied on Fri, 2012/12/14 - 5:04am in response to: Xavier Dury

Ok, thanks :)

Strange solution.. Anyway, if DriverManager needs to know, then I would prefer to pass the driver to it directly, so that anyone that reads the code would also know.

Xavier Dury replied on Fri, 2012/12/14 - 5:39am in response to: Tomasz O.

But then you would have a compile-time dependency on that particular Driver. Changing from one DBMS to another (say from Oracle to MySQL) would be less easy.

Mozart Brocchini replied on Fri, 2012/12/14 - 11:08am in response to: Xavier Dury

Xavier, I don't see the compile-time dependency on a particular driver.

The drive variable is being assigned to the value of 4th element of args parameter.

This could also have a little more flexibility if the assignment of driver variable was  changed to take an optional java property override.

Something like this:

String driver = System.getProperty( "my.jdbc.driver" ,args[3]);

Would use java property "my.jdbc.driver" to configure the driver.

David Cutter replied on Fri, 2012/12/14 - 1:26pm

"SELECT SYSDATE FROM DUAL" won't work from most databases so passing in driver via properties file or command-line doesn't really buy you much if the SQL itself is database-specific.

Could put the test SQL in a properties file with the driver name and have properties files for each database to test.

Xavier Dury replied on Fri, 2012/12/14 - 1:13pm in response to: Mozart Brocchini

I thought Thomasz wanted to pass the Driver instance somehow to the DriverManager, not the Driver class name (which would of course not imply a compile-time dependency).

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.