简体   繁体   English

如何使用 Java 读取 .csv 文件并将其数据插入 SQL Server?

[英]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.我有一个包含两个步骤的任务。

  1. I want to read all data from a .csv file.我想从.csv文件中读取所有数据。
  2. After reading that data I have to put it into a SQL Server database.读取该数据后,我必须将其放入 SQL Server 数据库中。

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.希望这可以帮助。

SQL Server file CSV SQL Server 文件 CSV

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 解析器,你可以

  1. Instead of printing out each 'CSV-file value', you will have to store them.您不必打印出每个“CSV 文件值”,而是必须存储它们。 You could for example use an ArrayList for each column in the CSV file, and populate those while reading the CSV file例如,您可以对 CSV 文件中的每一列使用ArrayList ,并在读取 CSV 文件时填充这些列
  2. Once the file is read, you can loop over those 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.

相关问题 如何使用 JAVA 读取 CSV 文件 - How do I read a CSV File with JAVA 从Java读取100 GB .csv文件并插入MS SQL Server DB - Read 100 GB .csv file from java and insert into MS SQL server DB 如何以一种方法获取从Java中的CSV文件读取的值,并以另一种方法使用它们? - How do I take values read from a CSV file in java in one method and use them in another method? 如何上传CSV文件,然后自动将数据插入数据库? - How do I upload a CSV file and then automatically insert the data into the DB? 如何读取文本/CSV 文件并忽略 java 中的“\n” - How do I read a text/CSV file and ignore the "\n" in java 如何仅读取 Java 中 csv 文件的最后一行? - How do I read only the last line of a csv file in Java? 如何读取多个 CSV 文件以对 Java 中的数据进行排序? - How do I read multiple CSV files to sort data in Java? 如何将文本文件中的数据从 Java 插入到 SQL 服务器? - How to insert data from a text file from Java to SQL server? 如何使用ucanaccess访问Java接口的远程服务器/如何找到远程SQL Server文件路径? - How do I use ucanaccess to access a remote server for a java interface / how do I find my remote SQL Server file path? 如何使用Java中的jena将从csv读取的行数中的数据插入到rdf文件中? - How to insert data from numbers of rows read from csv into a rdf file using jena in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM