Java - 将.csv导入数据库 - 排除第一行

时间:2014-03-12 07:09:29

标签: java sql database csv import

我正在从.csv文件导入数据库值。但是,.csv中的第一行实际上是表列的名称,我的导入器使用第一行更新数据库。这会弄乱我稍后会用它做的查询。请帮助我如何防止这种情况?

我的.csv导入程序的代码

try
{
    BufferedReader br=new BufferedReader(new FileReader("v.csv"));
    String line;
    while((line=br.readLine())!=null)
    {
        String[]value = line.split(",");
        String sql = "INSERT into main ([Ticket #], Status, Priority, Department, [Account Name]) "
                + "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"','"+value[3]+"','"+value[4]+"')";
        System.out.println(sql);
        PreparedStatement pst = null;
        try
        {
            pst = db.prepareStatement(sql);
            pst.executeUpdate();
        }finally{
            if(pst != null){
                pst.close();
            }
        }
    }
    br.close();
}
catch(Exception e)
{
    JOptionPane.showMessageDialog(null, e);
}

3 个答案:

答案 0 :(得分:2)

String line;
line=br.readLine(); //read the line and do nothing with the line
while((line=br.readLine())!=null)
{

答案 1 :(得分:2)

很简单,请阅读第一行,然后启动while循环。

br.readline();
while((line=br.readLine())!=null)

答案 2 :(得分:1)

因为你没有读第一行..

As answer is given above... 

String line;
line=br.readLine(); //read the line and do nothing with the line
while((line=br.readLine())!=null)
{