将解析 CSV 文件插入 SQL 服务器
Insert parse CSV file to SQL Server
我正在使用 OpenCSV 库解析 CSV 文件。我设法跳过了第一个需要的行,只选择了想要的列并将其打印到控制台。
现在我正在努力将其插入 MSSQL 数据库。
那是我解析文件的代码:
JFileChooser fileopen = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter(
"CSV file", "csv");
fileopen.setFileFilter(filter);
int ret = fileopen.showDialog(null, "Choose file");
if (ret == JFileChooser.APPROVE_OPTION) {
CsvParserSettings settings = new CsvParserSettings();
settings.detectFormatAutomatically();
settings.setHeaderExtractionEnabled(true);
settings.selectIndexes(7, 8, 13, 14);
settings.setNumberOfRowsToSkip(9);
List<String[]> rows = new CsvParser(settings).parseAll((fileopen.getSelectedFile()), "UTF-8");
rows.forEach(arr -> System.out.println(Arrays.toString(arr)));
现在代码
INSERT INTO dbo.Glass(Nr_Temp) values(Arrays.toString(rows.get(1)));
让我得到整行而不是列(这是可以理解的:))但是对于 return 列值是否有任何其他解决方案将它们插入到 SQL 数据库?
已更新
您需要遍历 String[]
才能访问列的每个单独值。
PreparedStatement ps = connection.prepareStatement("INSERT INTO dbo.Szyby_temp(nr_zlec_klienta, nr_ref_klienta, szerokosc, wysokosc, ilosc, opis_dodatkowy, data_importu) VALUES(?, ?, ?, ?, ?, ?, getdate())");
int maxBatchSize = 100; //Using batching for performance
int currentBatchSize = 0;
for (String[] row : rows) {
int i = 1;
for (String columnValue : row) {
ps.setString(i++, columnValue); //Parameter indexes start with 1
}
ps.addBatch();
if (++currentBatchSize % maxbatchSize == 0) {
ps.executeUpdate();
}
}
ps.executeUpdate(); //if number of rows in csv file is not divisible by maxbatchSize
谢谢 Ivan,我取消了优化,因为文件很小(每个文件少于 100 行)并且还进行了更改
ps.executeupdate() to `ps.executeBatch()
因为它只上传了最后一行,现在一切正常,感谢您的宝贵时间。
这是我更改后的代码
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO dbo.Szyby_temp(nr_zlec_klienta, nr_ref_klienta, szerokosc, wysokosc, ilosc, opis_dodatkowy, data_importu) VALUES(?, ?, ?, ?, ?, ?, getdate())");
for (String[] row : rows) {
int i = 0;
for (String columnValue : row) {
ps.setString(++i, columnValue); //Parameter indexes start with 1
}
ps.addBatch();
}
ps.executeBatch(); //if number of rows in csv file is not divisible by maxbatchSize
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
我正在使用 OpenCSV 库解析 CSV 文件。我设法跳过了第一个需要的行,只选择了想要的列并将其打印到控制台。
现在我正在努力将其插入 MSSQL 数据库。
那是我解析文件的代码:
JFileChooser fileopen = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter(
"CSV file", "csv");
fileopen.setFileFilter(filter);
int ret = fileopen.showDialog(null, "Choose file");
if (ret == JFileChooser.APPROVE_OPTION) {
CsvParserSettings settings = new CsvParserSettings();
settings.detectFormatAutomatically();
settings.setHeaderExtractionEnabled(true);
settings.selectIndexes(7, 8, 13, 14);
settings.setNumberOfRowsToSkip(9);
List<String[]> rows = new CsvParser(settings).parseAll((fileopen.getSelectedFile()), "UTF-8");
rows.forEach(arr -> System.out.println(Arrays.toString(arr)));
现在代码
INSERT INTO dbo.Glass(Nr_Temp) values(Arrays.toString(rows.get(1)));
让我得到整行而不是列(这是可以理解的:))但是对于 return 列值是否有任何其他解决方案将它们插入到 SQL 数据库?
已更新
您需要遍历 String[]
才能访问列的每个单独值。
PreparedStatement ps = connection.prepareStatement("INSERT INTO dbo.Szyby_temp(nr_zlec_klienta, nr_ref_klienta, szerokosc, wysokosc, ilosc, opis_dodatkowy, data_importu) VALUES(?, ?, ?, ?, ?, ?, getdate())");
int maxBatchSize = 100; //Using batching for performance
int currentBatchSize = 0;
for (String[] row : rows) {
int i = 1;
for (String columnValue : row) {
ps.setString(i++, columnValue); //Parameter indexes start with 1
}
ps.addBatch();
if (++currentBatchSize % maxbatchSize == 0) {
ps.executeUpdate();
}
}
ps.executeUpdate(); //if number of rows in csv file is not divisible by maxbatchSize
谢谢 Ivan,我取消了优化,因为文件很小(每个文件少于 100 行)并且还进行了更改
ps.executeupdate() to `ps.executeBatch()
因为它只上传了最后一行,现在一切正常,感谢您的宝贵时间。 这是我更改后的代码
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO dbo.Szyby_temp(nr_zlec_klienta, nr_ref_klienta, szerokosc, wysokosc, ilosc, opis_dodatkowy, data_importu) VALUES(?, ?, ?, ?, ?, ?, getdate())");
for (String[] row : rows) {
int i = 0;
for (String columnValue : row) {
ps.setString(++i, columnValue); //Parameter indexes start with 1
}
ps.addBatch();
}
ps.executeBatch(); //if number of rows in csv file is not divisible by maxbatchSize
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
}