2

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

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

假设我的系统属性“oracle.net.tns_admin”设置正确,tnsname.ora 文件没有问题。我有 tnsname、用户名、密码三个数据库,例如:

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

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

1> 测试用例1:连接两个数据库1 TO 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 TO 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);
    }

}
4

1 回答 1

1

您交换了 conn2 的用户名和密码。

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

应该

  username = descConfig[1].trim();
  password = descConfig[2].trim(); 
于 2013-11-11T18:08:29.387 回答