删除斜杠的 csv 日期

csv date to remove slashes

这是我关于如何为一个 csv 文件使用两个定界符的后续问题。我有一个具有以下日期格式的 csv 文件。

2018/01/25
2018/01/27

我需要将 csv 上传到 mySql table 中,它将日期作为 INT 而不是 Date 或 String 格式。

当我尝试上传 csv 时,我收到一条错误消息,指出日期列已被截断。

我认为这是由于日期之间的“/”所致。

分隔符由 bufferedReader 处理。下面是我的代码。

  while ((br.readLine()) != null) {
            String line = br.readLine();// br string variable
            String[] rawRow = line.split(",");
            String lastEntry = rawRow[rawRow.length - 1];//this contains the LinkId/branchNo
            String[] properLastEntry = lastEntry.split("/");//this contains the LinkId/branchNo split into two columnms
            String[] oneRow = new String[rawRow.length + 1];
            System.arraycopy(rawRow, 0, oneRow, 0, rawRow.length - 1);
            System.arraycopy(properLastEntry, 0, oneRow, oneRow.length - properLastEntry.length, properLastEntry.length);

            model.addRow(new Object[0]);
            model.setValueAt(oneRow[0], row, 0);
            model.setValueAt(oneRow[1], row, 1);
            model.setValueAt(oneRow[2], row, 2);
            model.setValueAt(oneRow[3], row, 3);
            row++;
            }
         br.close();

这是我的 jdbc 代码。

private void SaveData(){
    Connection connect = null;
    Statement stmt = null;

    try{

        //DriverManager Loader
        Class.forName("com.mysql.jdbc.Driver");

        //connection string url.. the port//schema name//username//password
                                                //this is the test Server ;oginDetails
        connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/riskfin", "root", "riskfin"); //-------------> this is for the localhost server
        stmt = connect.createStatement();

        for(int i = 0;i<table.getRowCount();i++)
        {
            String PayDate = table.getValueAt(i,0).toString();
            String Ammount = table.getValueAt(i,1).toString();
            String LinkID = table.getValueAt(i,2).toString();
            String BranchNo = table.getValueAt(i,3).toString();

            String  sql = "Insert into temp_payment_import "
                    +"VALUES('"+LinkID+"','"
                    +Ammount+"','"
                    +PayDate+"','"
                    +BranchNo+"')";

            stmt.execute(sql);
       }

        JOptionPane.showMessageDialog(null,"Data imported Successfully");

    }catch(Exception ex){
        JOptionPane.showMessageDialog(null,ex.getMessage());
        ex.printStackTrace();
    }
    try{
        if(stmt!= null){
            stmt.close();
            connect.close();
        }
    }catch(SQLException e){
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
}

我发现您当前的 JDBC 代码存在两个主要问题。首先,您没有使用准备好的语句,这增加了插入语句出现问题的可能性。其次,您没有明确列出插入的目标列,这也会引发问题。考虑以下修改:

for (int i=0;i < table.getRowCount(); i++) {
    String sql = "INSERT INTO temp_payment_import "
     + "(PayDate, Amount, LinkID, BranchNo) VALUES "
     + "(?, ?, ?, ?)";
    PreparedStatement ps = connect.prepareStatement(sql);
    ps.setString(1, PayDate);
    ps.setDouble(2, Double.parseDouble(Ammount));
    ps.setInt(3, Integer.parseInt(LinkID));
    ps.setInt(4, Integer.parseInt(BranchNo));
    ps.executeUpdate();
}

虽然这可能不是 运行,但这是朝着正确方向迈出的一步。现在,insert 语句明确列出了列,因此您不可能将错误的值插入特定列。因为您的所有数据似乎都以字符串值的形式出现,您可能需要做一些工作才能获得支付日期的 date/timestamp。我在上面使用了 setString,如果您的日期是 MySQL 接受的 ISO 格式,它应该可以正常工作。但是最好使用 JDBC 识别日期的格式日期类型。