How to test connection to Oracle Database using Java

后端 未结 10 1712
迷失自我
迷失自我 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: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
    

提交回复
热议问题