- package imp;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class ReadText {
- public void readTxt(String filePath,PreparedStatement state)
- throws IOException {
- FileReader f = new FileReader(filePath);
- BufferedReader bufferedreader = new BufferedReader(
- (new InputStreamReader(new FileInputStream(new File(filePath)),"utf-8")));//编码方式为utf-8,txt保存时编码方式也要选择为utf-8
- String instring;
- String[] strArr = null;
- while ((instring = bufferedreader.readLine()) != null) {
- if (0 != instring.length()) {
- strArr = instring.split(",");//与txt文件中的分隔符要一致
- addDataToState(strArr,state);
- }
- }
- }
- public void addDataToState(String[] strArr, PreparedStatement state) {
- try {
- state.setString(1, strArr[0]);
- state.setString(2, strArr[1]);
- state.setString(3, strArr[2]);
- state.setString(4, strArr[3]);
- state.setString(5, strArr[4]);
- state.setString(6, strArr[5]);
- state.setString(7, strArr[6]);
- state.setString(8, strArr[7]);
- state.setString(9, strArr[8]);
- state.setString(10, strArr[9]);
- state.setString(11, strArr[10]);
- state.setString(12, strArr[11]);
- state.setString(13, strArr[12]);
- state.setString(14, strArr[13]);
- state.setString(15, strArr[14]);
-
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void saveData(String filePath) {
- Connection conn = null;// 得到你的数据库连接
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e2) {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- String url = "jdbcracle:thinlocalhost:1521:XE";
- String user = "chqf429";
- String password = "167459";
- try {
- conn = DriverManager.getConnection(url, user, password);
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- PreparedStatement state = null;
- try {
- conn.setAutoCommit(false);// 设置手动提交事务
- state = conn
- .prepareStatement("insert into adult(age,workclass,fnlwgt,education,education-num," +
- "marital-status,occupation,relationship,race,sex,capital-gain," +
- "capital-loss,hours-per-week,native-country,salary) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
- this.readTxt(filePath,state);//第一个参数为txt文件路径
- conn.commit();//提交数据
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- close(conn, state);
- }
- }
- public void close(Connection conn, PreparedStatement state) {
- try {
- if (conn != null) {
- conn.close();
- conn = null;
- }
- if (state != null) {
- state.close();
- state = null;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- String filePath= "G:/adult.txt";
- ReadText rt = new ReadText();
- rt.saveData("");//参数为你的txt文件路径
- }
- }
- 错误为
- java.io.FileNotFoundException:
- at java.io.FileInputStream.open(Native Method)
- at java.io.FileInputStream.<init>(FileInputStream.java:106)
- at java.io.FileInputStream.<init>(FileInputStream.java:66)
- at java.io.FileReader.<init>(FileReader.java:41)
- at imp.ReadText.readTxt(ReadText.java:21)
- at imp.ReadText.saveData(ReadText.java:80)
- at imp.ReadText.main(ReadText.java:107)
复制代码 我想把文件adult.txt中的数据读进表adult中,可就是出错
|