Is there a way to test my connection to oracle database using Java? Here\'s my code.
public class OracleConnection {
public static void main(String[] args)
You can have an array of the your database server ips and iterate over them. For every failed connection attempt, you can proceed to the next ip from the array and try again. In case of a successful connection, you break the loop there and use the current connection which was established.
Maybe you should ping the IP address of the server. You should check this out: Ping function returns that all pinged IP addresses is reachable
I wrote a mini command line app to do what above code samples do.
https://github.com/aimtiaz11/oracle-jdbc-tester
Saves anyone coding it up. Just build (with maven) and run it.
Simple Java code to check connection to Oracle DB:
import java.sql.*;
public class Test {
private final static String DB_URL = "jdbc:oracle:thin:@//192.168.1.105:1521/MYORA";
private final static String USER = "myuser";
private final static String PASS = "mypwd";
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// ignore
}
}
}
}
}
For testing connection i will create and use 2 methods: for connection to db and for test this connection:
Class Connector {
private static final String CONNECTION_STRING = "jdbc:oracle:thin:@//%s:%d/%s";
private static final String QUERY_IS_CONNECTED = "SELECT * FROM table WHERE field = 'example'";
private static final Log LOG = LogFactory.getLog(Connector.class);
public Connection createConnection() {
Connection connection = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(String.format(CONNECTION_STRING, "127.0.0.1", "1521", "dbName"), "userName", "password");
} catch (Exception e) {
LOG.error("createConnection: connection error");
}
return connection;
}
public boolean isConnected() {
try (Connection connection = createConnection()) {
if (connection.isClosed()) {
return false;
}
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(QUERY_IS_CONNECTED)) {
if (resultSet == null) {
return false;
}
} catch (Exception e) {
LOG.error("isConnected: Query error", e);
return false;
}
} catch (Exception e) {
LOG.error("isConnected: Check connection error", e);
return false;
}
return true;
}
}
createConnection() - default connection to your db. Values of ip,port,dbName,userName and password will be yours.
isConnected() - first part check correct your connection and second part checks the correctness of working with the database. As there can be a connection, but not to be access for requests.
DriverManager#getConnection
it self attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers. and thorws SQLException
if a database access error occurs.
you can test you connection is valid or not with Connection#isValid(int timeout) returns true if the connection has not been closed and is still valid.
...
Connection conn = DriverManager.getConnection(url, username, password);
boolean reachable = conn.isValid(10);// 10 sec