数据库值重复错误

时间:2013-08-14 13:21:15

标签: java database

当我尝试运行以下代码时,它会在每次运行中重复添加值。我只需要。我如何使用query维护它。我在另一个数据库的帮助下将一些值插入到我的数据库中(值为表名)。我的动机是寻找表中的新变化。不是表中的所有值。

参考代码:

 connection = (Connection) dbSource.getConnection();
        String qry = "SELECT studentName From batcha "; 

        stmt = (PreparedStatement)  connection.prepareStatement(qry);    
        rs =  stmt.executeQuery();

            while (rs.next()) {

                String check=new String(rs.getString(("studentName")));
                String student = check.replaceAll("\\s","");

                String userQry = "SELECT user_name From users "; 
                stmt1=connection.prepareStatement(userQry);
                rs1=stmt1.executeQuery();

                System.out.println(student+"ssssssssssssssssssssssssssssssss");
                System.out.println("");
                System.out.println("");
                System.out.println("");

                while(rs1.next()){

                     Class.forName("com.mysql.jdbc.Driver");
                connection = (Connection) DriverManager.getConnection(DB_URL_table, USER, PASS);

                String fclty=new String(rs1.getString(("user_name")));
                String fcltyog = fclty.replaceAll("\\s","");

                String sql1 = "INSERT IGNORE INTO "+student+"(fcltyName,CommunicationOral,Communicationwritten,Leadership,AnalyticalAbilities,Interpersonalskills,DecisionMakingSkills,SelfConfidence,Creativity,Punctualityregularity,GeneralAwareness,Commitment,HardWork)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)"; 

                newStmt=(PreparedStatement) connection.prepareStatement(sql1); 
                System.out.println(fcltyog+"fffffffffffffffffffffffffffffffffffffffff");
                newStmt.setString(1, fclty);
                newStmt.setInt(2, 0);
                newStmt.setInt(3, 0);
                newStmt.setInt(4, 0);
                newStmt.setInt(5, 0);
                newStmt.setInt(6, 0);
                newStmt.setInt(7, 0);
                newStmt.setInt(8, 0);
                newStmt.setInt(9, 0);
                newStmt.setInt(10, 0);
                newStmt.setInt(11, 0);
                newStmt.setInt(12, 0);
                newStmt.setInt(13, 0);

                newStmt.executeUpdate();

            newStmt.close();

提前致谢

:deepthi

1 个答案:

答案 0 :(得分:1)

您可以在DB1的表中创建一个状态列(值可以是NEW,OLD),并选择状态为NEW的行,插入另一个数据库,将所选行的状态更新为OLD。

你必须反复这样做,可能会以固定的间隔循环。