[英]How do I use Java to read a .csv file and insert its data into SQL Server?
I am very new to Java.我对Java很陌生。 I have a task with two steps.我有一个包含两个步骤的任务。
.csv
file.我想从.csv
文件中读取所有数据。 I have done step one.我已经完成了第一步。 I'm able to read the .csv
file data, but I don't know that how to insert it into a database.我能够读取.csv
文件数据,但我不知道如何将其插入数据库。
Here's my code for fetching the .csv
file data:这是我获取.csv
文件数据的代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class DBcvsdataextractor {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName="D:/USPresident Wikipedia URLs Thumbs HS.csv";
try {
BufferedReader br = new BufferedReader( new FileReader(fileName));
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while( (fileName = br.readLine()) != null)
{
if(lineNumber++ == 0)
continue;
//break comma separated line using ","
st = new StringTokenizer(fileName, ",");
while(st.hasMoreTokens())
{
//display csv values
tokenNumber++;
System.out.print(st.nextToken() + '\t');
}
//new line
System.out.println(" ");
//reset token number
tokenNumber = 0;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Now, how do I insert that data into SQL Server?现在,我如何将该数据插入到 SQL Server 中?
The SQL Server has a tool for this. SQL Server 为此提供了一个工具。
Like this:像这样:
BULK INSERT CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
Use this command in your java, using JDBC connection, for example.例如,在您的 java 中使用此命令,使用 JDBC 连接。
Hope this helps.希望这可以帮助。
If you really want to do this with Java, and really want to write your own CSV parser as you currently did, you can如果你真的想用 Java 做到这一点,并且真的想像现在一样编写自己的 CSV 解析器,你可以
ArrayList
for each column in the CSV file, and populate those while reading the CSV file例如,您可以对 CSV 文件中的每一列使用ArrayList
,并在读取 CSV 文件时填充这些列ArrayList
instances again to construct one big INSERT
statement for all data, or one INSERT
statement for each row you encountered in the CSV file.一旦文件被读取,你可以遍历这些ArrayList
情况下,再构建一个大的INSERT
对所有数据的语句,或者一个INSERT
为您在CSV文件中遇到的每一行语句。 If you would opt for the last option, it is not even necessary to use those ArrayList
instances.如果您选择最后一个选项,则甚至不需要使用那些ArrayList
实例。 In that case you could construct an indiviual INSERT
statement while reading the CSV file, and submit it to the DB after each time a row has been read.在这种情况下,您可以在读取 CSV 文件时构造一个单独的INSERT
语句,并在每次读取一行后将其提交给数据库。 I know the approach of constructing your query while reading the CSV file would be possible as well if you want to go for one big INSERT
statement, but separating the INSERT
from the reading of the CSV file has the big advantage you can replace your own CSV parser later on by a standard one without too much trouble.我知道如果您想使用一个大的INSERT
语句,在读取 CSV 文件时构建查询的方法也是可能的,但是将INSERT
与读取 CSV 文件分开具有很大的优势,您可以替换自己的 CSV解析器稍后由标准解析器使用,没有太多麻烦。
You can customize below class to insert data into sql server.您可以自定义以下类以将数据插入 sql server。
File ImportCsv.java文件导入CSV.java
package com.example.demo;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.opencsv.CSVReader;
public class ImportCsv
{
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException
{
readCsv();
}
private static void readCsv() throws UnsupportedEncodingException, FileNotFoundException
{
Reader readerstream = new InputStreamReader(new FileInputStream("D:\\file.csv"), "Unicode");
try (CSVReader reader = new CSVReader(readerstream, ',');
Connection connection = DBConnection.getConnection();)
{
String insertQuery = "Insert into [dbo].[tableName] ([Column1],[Column2],[Column3], [Column4],...[Column8]") values (?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = connection.prepareStatement(insertQuery);
String[] rowData = null;
int i = 0;
while((rowData = reader.readNext()) != null){
for (String data : rowData)
{
//System.out.println(new String(data.replace(" ","")));
String strLine = new String(data.replace(" ",""));
String[] splited = strLine.split("\\s+");
pstmt.setNString(1, splited[0]);
pstmt.setNString(2, splited[1]);
pstmt.setNString(3, splited[2]);
pstmt.setNString(4, splited[3]);
pstmt.setNString(5, splited[4]);
pstmt.setNString(6, splited[5]);
pstmt.setNString(7, splited[6]);
try {
pstmt.setNString(8, splited[7]);
}catch(Exception e) {
}
if (++i % 8 == 0) {
pstmt.addBatch();// add batch
}
if (i % 80 == 0) {// insert when the batch size is 10
pstmt.executeBatch();
}
}}
System.out.println("Data Successfully Uploaded");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
DBConnection.java数据库连接程序
package com.example.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
String url = "jdbc:sqlserver://localhost:1433;" +
"databasename=myDBName;user=user;password=pwd;sendStringParametersAsUnicode=true;";
Connection con =DriverManager.getConnection(url);
return con;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.