How to test connection to Oracle Database using Java

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

提交回复
热议问题