不';t在java(jdbc)中插入csv文件记录
当我的CSVLoader文件没有在我的数据库中插入数据时,我有一个错误,我在控制台中得到了这个错误 FieldName(UploadDownloadFileServlet)=文件名 文件名(UploadDownloadFileServlet)=prueb2.csv ContentType(UploadDownloadFileServlet)=应用程序/vnd.ms-excel 字节大小(UploadDownloadFileServlet)=47 服务器上的绝对路径(UploadDownloadFileServlet)=C:\Users\SISTEMAS\workspaceEclipse.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SAC sin impression\uploadedCsvFiles\prueba2.csv 查询:插入到MATRICULA(C:UserssitemasWorkspaceEclipse.metadata.pluginsorg.eclipse.wst.server.coretmp0wtpwebappsSAC-sin-ImpressionUploadedCsvFileSprueBa2.csv)值(?) [CSVLoader]:0条记录加载到MATRICULA DB表中 这是我的代码:不';t在java(jdbc)中插入csv文件记录,java,mysql,csv,jdbc,Java,Mysql,Csv,Jdbc,当我的CSVLoader文件没有在我的数据库中插入数据时,我有一个错误,我在控制台中得到了这个错误 FieldName(UploadDownloadFileServlet)=文件名 文件名(UploadDownloadFileServlet)=prueb2.csv ContentType(UploadDownloadFileServlet)=应用程序/vnd.ms-excel 字节大小(UploadDownloadFileServlet)=47 服务器上的绝对路径(UploadDownloadF
public void loadCSV(InputStream csvFile, String tableName,
boolean truncateBeforeLoad) throws Exception {
CSVReader csvReader = null;
if(null == this.connection) {
throw new Exception("Not a valid connection.");
}
try {
/* Modified by rammar.
*
* I was having issues with the CSVReader using the "\" to escape characters.
* A MySQL CSV file contains quote-enclosed fields and non-quote-enclosed NULL
* values written as "\N". The CSVReader was removing the "\". To detect "\N"
* I must remove the escape character, and the only character you can replace
* it with that you are pretty much guaranteed will not be used to escape
* text is '\0'.
* I read this on:
* http://stackoverflow.com/questions/6008395/opencsv-in-java-ignores-backslash-in-a-field-value
* based on:
* http://sourceforge.net/p/opencsv/support-requests/5/
*/
// PREVIOUS VERSION: csvReader = new CSVReader(new FileReader(csvFile), this.seprator);
csvReader = new CSVReader(new InputStreamReader(csvFile), this.seprator, '"', '\0');
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Error occured while executing file. "
+ e.getMessage());
}
String[] headerRow = csvReader.readNext();
if (null == headerRow) {
throw new FileNotFoundException(
"No columns defined in given CSV file." +
"Please check the CSV file format.");
}
String questionmarks = StringUtils.repeat("?,", headerRow.length);
questionmarks = (String) questionmarks.subSequence(0, questionmarks
.length() - 1);
/* NOTE from Ron: Header column names must match SQL table fields */
String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
query = query
.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
query = query.replaceFirst(VALUES_REGEX, questionmarks);
System.out.println("Query: " + query); // Modified by rammar to suppress output
String[] nextLine;
Connection con = null;
PreparedStatement ps = null;
try {
con = this.connection;
con.setAutoCommit(false);
ps = con.prepareStatement(query);
if(truncateBeforeLoad) {
//delete data from table before loading csv
con.createStatement().execute("DELETE FROM " + tableName);
}
final int batchSize = 1000;
int count = 0;
Date date = null;
while ((nextLine = csvReader.readNext()) != null) {
if (null != nextLine) {
int index = 1;
for (String string : nextLine) {
date = DateUtil.convertToDate(string);
if (null != date) {
ps.setDate(index++, new java.sql.Date(date
.getTime()));
} else {
/* Section modified by rammar to allow NULL values
* to be input into the DB. */
if (string.length() > 0 && !string.equals("\\N")) {
ps.setString(index++, string);
} else {
ps.setNull(index++, Types.VARCHAR);
//ps.setString(index++, null); // can use this syntax also - not sure which is better
}
}
}
ps.addBatch();
}
if (++count % batchSize == 0) {
ps.executeBatch();
}
}
ps.executeBatch(); // insert remaining records
System.out.println("[" + this.getClass().getSimpleName() + "]: " +
count + " records loaded into " + tableName + " DB table");
con.commit();
} catch (Exception e) {
con.rollback();
e.printStackTrace();
throw new Exception(
"Error occured while loading data from file to database."
+ e.getMessage());
} finally {
/*if (null != ps)
ps.close();
*/
/*if (null != con)
con.close();*/
csvReader.close();
}
}
查看您的查询:
UserssitemasWorkspaceEclipse
-注意缺少了什么?另外,发布SQL\u INSERT、TABLE\u REGEX、KEYS\u REGEX和values\u REGEX的值也会有所帮助。代码中出错的部分是构建查询…查看您的查询:UserssitemasWorkspaceEclipse
-注意缺少一些内容?另外,发布SQL\u INSERT、TABLE\u REGEX、KEYS\u REGEX和values\u REGEX的值也会有所帮助。代码中出错的部分是构建查询。。。