【发布时间】:2018-03-27 12:52:26
【问题描述】:
这是我关于如何为一个 csv 文件使用两个分隔符的后续问题。我有一个具有以下日期格式的 csv 文件。
2018/01/25
2018/01/27
我需要将 csv 上传到 mySql 表中,该表将日期作为 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();
}
}
}
【问题讨论】:
-
跟进什么? This?如果是这样,请将链接放在您的问题中,或者不要说这是后续问题。
-
@AndyTurner 是的,它是我之前提出的那个(--this?--)问题的后续......
-
幸运,问题出在你的 JDBC 代码上,而不是你在这个问题上面发布的内容。请向我们展示该代码。
-
@TimBiegeleisen 我真的认为问题出在斜杠上,因为 mysql 中的表将 paydate 列设置为 INT,我怀疑它需要除数值之外的任何其他字符集。
-
为什么要在整数列中插入日期/文本日期?你是不是觉得有点过时了?您应该将这些日期插入日期列;它们采用 MySQL 可以处理的格式。
标签: java csv user-interface bufferedreader