import java.io.*;
import java.sql.*;
public class ReadFromFile {
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
* @param fileName 文件名
*/
public static void readFileByLines(String fileName){
File file = new File(fileName);
BufferedReader reader = null;
try {
//--------------------------------------------------------------------------
Connection con;
Statement stmt;
ResultSet rs;
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); //改成你的驱动
con = DriverManager.getConnection( "jdbc:odbc:meritlinedb"); //改成你的DB
PreparedStatement pstmt=con.prepareStatement( "insert into sale (id,name,sex) values(?,?,?)"); //用PreparedStatement,速度快十倍
//--------------------------------------------------------------------------
System.out.println( "以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader( new FileReader(file));
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null){
//显示行号
System.out.println( "line " + line + ": " + tempString); //这里可以放你要处理的字符串的语句
xxxx1="";
xxxx2="";
xxxx3=""; //用你自己的方法得到这三个(xxxx1,xxxx2,xxxx3)
//--------------------------------------------
pstmt.setString(1,xxxx1);
pstmt.setString(2,xxxx2);
pstmt.setString(3,xxxx3);
pstmt.executeUpdate(); //写入数据库
//--------------------------------------------
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
String fileName = "C:/temp.txt";
ReadFromFile.readFileByLines(fileName);
}
}
import java.sql.*;
public class ReadFromFile {
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
* @param fileName 文件名
*/
public static void readFileByLines(String fileName){
File file = new File(fileName);
BufferedReader reader = null;
try {
//--------------------------------------------------------------------------
Connection con;
Statement stmt;
ResultSet rs;
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); //改成你的驱动
con = DriverManager.getConnection( "jdbc:odbc:meritlinedb"); //改成你的DB
PreparedStatement pstmt=con.prepareStatement( "insert into sale (id,name,sex) values(?,?,?)"); //用PreparedStatement,速度快十倍
//--------------------------------------------------------------------------
System.out.println( "以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader( new FileReader(file));
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null){
//显示行号
System.out.println( "line " + line + ": " + tempString); //这里可以放你要处理的字符串的语句
xxxx1="";
xxxx2="";
xxxx3=""; //用你自己的方法得到这三个(xxxx1,xxxx2,xxxx3)
//--------------------------------------------
pstmt.setString(1,xxxx1);
pstmt.setString(2,xxxx2);
pstmt.setString(3,xxxx3);
pstmt.executeUpdate(); //写入数据库
//--------------------------------------------
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
String fileName = "C:/temp.txt";
ReadFromFile.readFileByLines(fileName);
}
}
转载于:https://blog.51cto.com/ilexes/341153