0

I have been given a task to write a script in java, to connect oracle database from an app server. Scheduling tool will be used to schedule the job every 10 minutes.

If connection exists, the script will do nothing and just disconnect it. (and will be run again after 10 minutes). If cannot connect after 10 secs, the program will send an email notification.

The whole thing is to ensure connection can be established between the app server and the oracle db.

Really have no clue on this. Could you please advise what are the steps to do this and what Java APIs I'll need??

Many many thanks in advance!

2 Answers 2

1

You will need classes out of the java.sql package. Assuming Java 7+.

import java.sql.*;

public class Test {
    public static void main(String[] args) {
        String url = "..."; // Specify according to JDBC driver in use
        String sql = "SELECT 1 FROM DUAL"; // Test statement. Change depending on SQL vendor
        try (Connection conn = DriverManager.getConnection(url);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {/*Nothing to do*/}
        } catch (SQLException e) {
            // Send email here
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Bob Thanks for the update. I didn't see the oracle tag.
Thank you for your prompt reply!
1

If you are using J2EE App Server, create a database connection pool there which your application is also going to use.

One of the parameters of the database pool can be to verify the connection at regular intervals by sending simple SQL. Then your task boils down just to monitor the logs to see if database connection pool throws any errors when polling.

1 Comment

Thanks so much for the quick response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.