How to test connection to Oracle Database using Java

后端 未结 10 1702
迷失自我
迷失自我 2021-02-04 09:56

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)          


        
相关标签:
10条回答
  • 2021-02-04 10:26

    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.

    0 讨论(0)
  • 2021-02-04 10:26

    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

    0 讨论(0)
  • 2021-02-04 10:36

    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.

    0 讨论(0)
  • 2021-02-04 10:40

    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    
            }    
          }    
        }            
      }    
    }
    
    0 讨论(0)
  • 2021-02-04 10:42

    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.

    0 讨论(0)
  • 2021-02-04 10:44

    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
    
    0 讨论(0)
提交回复
热议问题