Java JDBC - How to connect to Oracle using Service Name instead of SID

前端 未结 8 967
醉话见心
醉话见心 2020-11-22 15:14

I have a Java application that uses JDBC (via JPA) that was connecting to a development database using hostname, port and Oracle SID, like this:

jdbc:oracle:

8条回答
  •  名媛妹妹
    2020-11-22 15:28

    This discussion helped me resolve the issue I was struggling with for days. I looked around all over the internet until I found the answered by Jim Tough on May 18 '11 at 15:17. With that answer I was able to connect. Now I want to give back and help others with a complete example. Here goes:

    import java.sql.*; 
    
    public class MyDBConnect {
    
        public static void main(String[] args) throws SQLException {
    
            try { 
                String dbURL = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=whatEverYourHostNameIs)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=yourServiceName)))";
                String strUserID = "yourUserId";
                String strPassword = "yourPassword";
                Connection myConnection=DriverManager.getConnection(dbURL,strUserID,strPassword);
    
                Statement sqlStatement = myConnection.createStatement();
                String readRecordSQL = "select * from sa_work_order where WORK_ORDER_NO = '1503090' ";  
                ResultSet myResultSet = sqlStatement.executeQuery(readRecordSQL);
                while (myResultSet.next()) {
                    System.out.println("Record values: " + myResultSet.getString("WORK_ORDER_NO"));
                }
                myResultSet.close();
                myConnection.close();
    
            } catch (Exception e) {
                System.out.println(e);
            }       
        }
    }
    

提交回复
热议问题