JDBC:通过tnsname奇怪错误连接到Oracle DB

时间:2013-11-11 17:55:41

标签: java database oracle jdbc

我编写了一个java程序来同步两个不同远程数据库上的两个表。

如果用户名或密码中没有任何特殊字符,程序可以正常连接到两个数据库,否则,它会成功连接到第一个数据库,但在第二个数据库上失败。

假设我的系统属性“oracle.net.tns_admin”设置正确并且tnsname.ora文件没有问题。我有三个数据库,包括tnsname,username,密码,如:

  1. “TESTDB1”, “AAA”, “AAA”!
  2. “TESTDB2”, “BBB”, “BBB”!
  3. “TESTDB3”,“CCC”,“CCC” //密码部分没有特殊字符。

    以下是我的测试用例和程序的返回值:

  4. 1>测试用例1:连接到两个数据库1到2:

     Source DB:[TESTDB1, AAA, AAA!]  Connect Successfully to source DB! 
     Dest DB:[TESTDB2, BBB, BBB!]  Connect to dest DB failed!
    

    2 - ;测试用例2:连接到同一个数据库两次:

     Source DB:[TESTDB1, AAA, AAA!]  Connect Successfully to source DB! 
     Dest DB:[TESTDB1, AAA, AAA!]  Connect to dest DB failed!
    

    3>测试用例3:连接到两个数据库1到3:

     Source DB:[TESTDB1, AAA, AAA!]  Connect Successfully to source DB! 
     Dest DB:[TESTDB3, CCC, CCC]   Connect Successfully to dest DB! 
    

    4>测试用例4:连接到两个数据库3 TO 1:

     Source DB:[TESTDB3, CCC, CCC]   Connect Successfully to source DB! 
     Dest DB:[TESTDB1, AAA, AAA!]  Connect to dest DB failed!
    

    仅测试案例3成功。失败的消息是:“ORA-01017:用户名/密码无效;登录被拒绝。”对于失败的连接。 因此,如果第二个数据库的用户名或密码中有特殊字符,那么部分原因是程序无法连接到数据库的原因?你可以尝试下面的程序并测试一下吗?

    完整代码:(您必须拥有oracle数据库并将“oracle.net.tns_admin”配置为tnsname.ora文件的路径)

    import java.sql.*; 
    import java.util.Arrays;
    import oracle.jdbc.OracleDriver;
    public class JDBCTest {
        public boolean success = false; 
    
        public JDBCTest() {
            System.setProperty("oracle.net.tns_admin", "S:\\"); // Modify your path here
        }
    
        public static void debug(Object obj) {
            System.out.println(obj);
        } 
        public String processSync(String[] sourceConfig, String[] descConfig) {
            String vReturn = "Successfully copied the data from " + sourceConfig[0] + " to "
                    + descConfig[0];
            String thinConn = "jdbc:oracle:thin:@" + sourceConfig[0];
            String username = sourceConfig[1];
            String password = sourceConfig[2];
            Connection conn1 = null;
            Statement statement = null;
            try {
                DriverManager.registerDriver(new OracleDriver());
                debug("Source DB:"+Arrays.asList(sourceConfig));
                conn1 = DriverManager.getConnection(thinConn, username,
                        password);
                conn1.setAutoCommit(false);
                statement = conn1.createStatement();
                debug("Connect Successfully to source DB!");
            } catch (SQLException sqle) {
                vReturn="Connect to source DB failed!"+"["+sqle.getMessage()+"]";
                debug("Connect to source DB failed!");
            }
    
            thinConn = "jdbc:oracle:thin:@" + descConfig[0].trim();
            password = descConfig[1].trim();
            username = descConfig[2].trim(); 
            Statement statement2 = null;
            Connection conn2 = null;
            try { 
                DriverManager.registerDriver(new OracleDriver());
                debug("Dest DB:"+Arrays.asList(descConfig));
                conn2 = DriverManager.getConnection(thinConn, username,
                        password);
                conn2.setAutoCommit(false);
                statement2 = conn2.createStatement();
                debug("Connect Successfully to dest DB!");
            } catch (SQLException sqle) {
                vReturn="Connect to dest DB failed!"+"["+sqle.getMessage()+"]";
                debug("Connect to dest DB failed!");
            }
    
            if (conn1 != null && conn2 != null) { 
                debug("Successfully connect to both Databases");
            }else{
                return vReturn;
            }
            success = true; 
            return vReturn; 
        } 
        public static void main(String[] args) throws Exception { 
            JDBCTest newTest = new JDBCTest();  
        String[] sourceConfig={"TESTDB1","AAA","AAA!"};
        String[] descConfig={"TESTDB2","BBB","BBB!"}; //{"TESTDB3","CCC","CCC"};
            newTest.processSync(sourceConfig, descConfig);
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

你换了conn2的用户名和密码。

   password = descConfig[1].trim();
   username = descConfig[2].trim(); 

应该是

  username = descConfig[1].trim();
  password = descConfig[2].trim();