如何在java中将csv中的值插入数据库

时间:2015-06-09 08:38:59

标签: java csv

我尝试过以下代码来分割csv值,现在如何将其插入到DB中?我是否将值保存到单独的变量中以匹配列名?我无法理解。

注意:我现在不想使用任何csv解析器。我只想手动完成

public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        String name;
        String email;
        String phone;
        String ID;

        Connection con = OracleDBConnection.getConnection();
        String query = "Insert into NEWSTUDENT values(?,?,?,?)";
                PreparedStatement st = con.prepareStatement();
                st.executeUpdate(query);        

        try {
            BufferedReader bReader = new BufferedReader(new FileReader("1000rows.csv"));

            while (bReader != null) {
                String read;
                try {
                    read = bReader.readLine();
                    if (read != null) 
                    {
                        String[] array = read.split(",+");
                        for(String result:array)
                        {
                            System.out.println(result);
                        }
                    } 
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                finally
                {
                   if (bReader == null) 
                    {
                        bReader.close();
                    }
                }
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }
}

输出:

1Kiriti
kiriti@gmail.com
880789939

数据库中的列名:

Name Email Phone ID

3 个答案:

答案 0 :(得分:3)

使用Parepared语句并在While循环中构建查询并执行它。有关准备好的声明的更多信息,请查看Check this link

String sql = " INSERT INTO TABLE_(name,email,phone,id) VALUES(?,?,?,?) ";


try { 
        BufferedReader bReader = new BufferedReader(new FileReader("1000rows.csv"));
        String line = ""; 
        while ((line = bReader.readLine()) != null) {
            try {

                if (line != null) 
                {
                    String[] array = line.split(",+");
                    for(String result:array)
                    {
                        System.out.println(result);
 //Create preparedStatement here and set them and excute them
                PreparedStatement ps = yourConnecionObject.createPreparedStatement(sql);
                 ps.setString(1,str[0]);
                 ps.setString(2,str[1]);
                 ps.setString(3,str[2]);
                 ps.setString(4,strp[3])
                 ps.excuteUpdate();
                 ps. close()
   //Assuming that your line from file after split will folllow that sequence

                    }
                } 
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            finally
            {
               if (bReader == null) 
                {
                    bReader.close();
                }
            }
        }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }

答案 1 :(得分:3)

您可以使用Prepare Statement并在每次迭代时设置参数中的值:

Connection con = OracleDBConnection.getConnection();
String query = "Insert into NEWSTUDENT values(?,?,?,?)";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1,array[0]);
ps.setString(2,array[1]); // and so on
ps.executeUpdate();

如果行数不多,您也可以使用批处理:

String sql = "Insert into NEWSTUDENT values(?,?,?)";


PreparedStatement preparedStatement = null;
try{
    preparedStatement =
            connection.prepareStatement(sql);

    preparedStatement.setString(1, "Gary");
    preparedStatement.setString(2, "Larson");
    preparedStatement.setString (3, "Test");

    preparedStatement.addBatch();

    preparedStatement.setString(1, "Stan");
    preparedStatement.setString(2, "Lee");
    preparedStatement.setString (3, 456);

    preparedStatement.addBatch();

    int[] affectedRecords = preparedStatement.executeBatch();

}finally {
    if(preparedStatement != null) {
        preparedStatement.close();
    }
}

答案 2 :(得分:2)

您可以将数据存储在数组中并将它们绑定到您的语句:

 String query = "Insert into NEWSTUDENT values(?,?,?,?)";

 PreparedStatement st = con.prepareStatement(query);
 st.setString(1,array [0]);
 st.setString(2,array[1]);
 ....
 st.executeUpdate();        

有关预准备语句的更多信息,请参阅oracle documentation