Java将数据从CSV导入SQL可防止双重输入

时间:2014-01-22 05:04:03

标签: java sql csv

我有以下代码。我不确定如何安排它们,以便它可以在将记录添加到SQL数据库之前检查CSV中的条目。 问题是我还在向sql添加双重记录

try {
        br = new BufferedReader(new FileReader(file));
        String[] data1 = br.readLine().split(cvsSplitBy);

        while ((line = br.readLine()) != null) {
            String queryCheck = "SELECT Count(Name) from DB WHERE Name = ?";

            PreparedStatement st = conn.prepareStatement(queryCheck);
            //value is the data of the name column in the CSV
            st.setString(1, data1[0]);
            ResultSet rs = st.executeQuery();
            boolean recordFound = rs.next();

            if (recordFound) {
                //dont add record

                System.out.println("found " + data1[0]);

            } else {


                String[] data = line.split(cvsSplitBy);

                String sql2 = "INSERT INTO DB (Name, ID, Age) values (?, ?, ?)";
                pstmt = conn.prepareStatement(sql2);
                pstmt.setString(1, data[0]);
                pstmt.setString(2, data[1]);
                pstmt.setString(3, data[2]);
                pstmt.executeUpdate();
            }

        }

2 个答案:

答案 0 :(得分:5)

上面的代码只检查数据库中是否存在第一行数据。 将查询检查放在while循环中。

您应该SELECT *而不是SELECT count(Name),因为即使计数结果为SELECT count(Name)0也会给您结果

试试这个:

    br = new BufferedReader(new FileReader(file));
    String queryCheck, insert;
    PreparedStatement st;
    ResultSet rs;
    boolean recordFound;
    while ((line = br.readLine()) != null) {
        queryCheck = "SELECT * from DB WHERE Name = ?";
        String[] data1 = line.split(cvsSplitBy);
        System.out.println("Name= " + data1[0] + " , ID= " + data1[1] + " , Age= " + data1[2]);
        st = conn.prepareStatement(queryCheck);
        st.setString(1, data1[0]);
        rs = st.getResultSet();
        recordFound = rs.next();

        if (recordFound) {
            System.out.println(data1[0]+" already exist in the database!");
        }
        else{
            String[] data = line.split(cvsSplitBy);
            insert = "INSERT INTO DB (Name, ID, Age) values (?, ?, ?)";
            pstmt = conn.prepareStatement(insert);
            pstmt.setString(1, data[0]);
            pstmt.setString(2, data[1]);
            pstmt.setString(3, data[2]);
            pstmt.executeUpdate();
        }

    }

答案 1 :(得分:2)

看起来像是一对错误的逻辑步骤。您需要使用if语句来控制何时调用插入。我将你的recordFound变量反转了,因为如果找到记录你就不想做任何事情,因此不需要else块。看起来像这样:

     if (!recordFound) {
            while ((line = br.readLine()) != null) {

                 String[] data = line.split(cvsSplitBy);

                 String sql2 = "INSERT INTO DB (Name, ID, Age) values (?, ?, ?)";
                 pstmt = conn.prepareStatement(sql2);
                 pstmt.setString(1, data[0]);
                 pstmt.setString(2, data[1]);
                 pstmt.setString(3, data[2]);
                 pstmt.executeUpdate();
           }
     }

我也相信你也在滥用rs.next()电话;即使计数为0,仍然返回该值,它将始终导致rs.next()调用为真。你需要得到结果的值,看看它是否大于0.这样的事情:

boolean recordFound = false;
PreparedStatement st = conn.prepareStatement(queryCheck);
//value is the data of the name column in the CSV
st.setString(1, value);
ResultSet rs = st.getResultSet();
rs.first();//move cursor to single result
if(rs.getInt(1) > 0)
    recordFound = true;     

此外,您正在以静态方式读取文件的第一行,然后稍后在found块中尝试循环它。你需要最初循环它而不是查看第一行然后循环