Monday, August 27, 2018

DB:: Test Remote DB Connectivity Using Java Client

Following steps will help you to test connectivity to remote database system using simple Java client.

1. Create a Java file named 'Test.java' and copy below content (Update remote database details as applicable).

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Test {
  public static void main (String[] args) throws java.lang.Exception {
    String DB_URL = "jdbc:oracle:thin:@//lkakarla.com:1521/XE";
    String USER = "SYS AS SYSDBA";
    String PASS = "xxxxxxx";
    Connection conn = null;
    try {
       System.out.println("Connecting to database...");
       conn = DriverManager.getConnection(DB_URL,USER,PASS);
       if (conn != null) {
           System.out.println("Connected to Database!");
          }
    } catch (Exception e) {
         e.printStackTrace();
    } finally {
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException e) {
          // ignore
        }
      }
    }
  }
}

2. Download the latest JDBC driver file from Oracle site and copy it to the same directory, where Java client code is located.

3. Compile and Run Java class. (Include JDBC driver in classpath).

Compile: javac -cp ojdbc7.jar:. Test.java
Run:     java -cp ojdbc7.jar:. Test

No comments:

Post a Comment

Provide your thoughts !