Java读取本地文件,然后把数据存到mysql.(以csv文件为例)

“`
package a;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class read{
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
*/
public static void readTxtFile(String filePath){
try {
BufferedReader bufferedReader = null;
Connection conn = null;
String driver = “com.mysql.jdbc.Driver”;
String url = “jdbc:mysql://localhost:3306/read_data”;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, “root”, “xinghai”);
String encoding=”utf-8”;
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
String[] columns = lineTxt.split(“,”);
if(columns[0].trim().equalsIgnoreCase(“序号”)){
continue;
}
PreparedStatement pstmt = conn.prepareStatement(“insert into a(uid,mid)values(?,?)”);
pstmt.setString(1, columns[0]);
pstmt.setString(2, columns[1]);
pstmt.executeUpdate();
}
read.close();
}else{
System.out.println(“找不到指定的文件”);
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.println(“读取文件内容出错”);
e.printStackTrace();
}

}

public static void main(String argv[]){
    String filePath = "E:\\test.csv";

// “res/”;
readTxtFile(filePath);
}
}“`

ps:前提是你有mysql链接的jar包啊。

你可能感兴趣的:(java,数据)