简体   繁体   English

使用Java在App Server和Oracle数据库之间建立连接

[英]Establish connection between app server and oracle database using java

I have been given a task to write a script in java, to connect oracle database from an app server. 我被赋予了用Java编写脚本,从应用程序服务器连接oracle数据库的任务。 Scheduling tool will be used to schedule the job every 10 minutes. 计划工具将用于每10分钟计划一次作业。

If connection exists, the script will do nothing and just disconnect it. 如果存在连接,脚本将不执行任何操作,而只是断开连接。 (and will be run again after 10 minutes). (并且将在10分钟后再次运行)。 If cannot connect after 10 secs, the program will send an email notification. 如果10秒钟后无法连接,程序将发送电子邮件通知。

The whole thing is to ensure connection can be established between the app server and the oracle db. 整个过程是确保可以在应用程序服务器和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?? 您能否建议执行此步骤的步骤以及我需要哪些Java API?

Many many thanks in advance! 在此先谢谢了!

If you are using J2EE App Server, create a database connection pool there which your application is also going to use. 如果您使用的是J2EE App Server,请在该数据库中创建一个数据库连接池,您的应用程序也将在其中使用该数据库。

One of the parameters of the database pool can be to verify the connection at regular intervals by sending simple SQL. 数据库池的参数之一可以是通过发送简单的SQL定期验证连接。 Then your task boils down just to monitor the logs to see if database connection pool throws any errors when polling. 然后,您的任务会变得很简单,仅监视日志以查看轮询时数据库连接池是否引发任何错误。

You will need classes out of the java.sql package. 您将需要java.sql包之外的类。 Assuming Java 7+. 假设使用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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM