从文件中读取并将文本插入表中

时间:2015-10-29 02:31:52

标签: java sql

我正在从文本文件中读取,然后使用该文本更新表格。我遇到问题的代码是String insertStatement。我用评论标记了问题位置。对不起,这有点乱,但我想包括我到目前为止的所有内容。

该文件读取正常并输出正常我只需找出一种方法将存储在变量line中的文件信息插入到名为EMPLOYEE_COPY的表中。

import java.sql.*;
import java.io.*;

public class lab13 {
    {

    }

    public static void main(String [] args) throws SQLException {
        // The name of the file to open.
        Connection conn = DriverManager.getConnection(
                "jdbc:oracle:thin:@classdb.jccc.edu:1521:cisdb", "jpierc37", "jp2687");
        System.out.println("connected");
            String fileName = "enrollment.txt";
            Statement statement = conn.createStatement();

            // This will reference one line at a time
            String line = null;
            try
            {        
                // FileReader reads text files in the default encoding.
                FileReader fileReader = 
                    new FileReader(fileName);

                // Always wrap FileReader in BufferedReader.
                BufferedReader bufferedReader = 
                    new BufferedReader(fileReader);
                Statement insert = conn.createStatement();                 

                while((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                    String insertStatement =
                            // This is the problem here.**********************************************
                            "INSERT INTO EMPLOYEE_COPY(Employee_ID, Name, Salary, Title)" +
                                    "VALUES(line)";
                    int count2 = insert.executeUpdate(insertStatement);
                    System.out.println("Inserted: " + count2);
                }   

                // Always close files.
                bufferedReader.close();       
            }
            catch(FileNotFoundException ex) {
                System.out.println(
                    "Unable to open file '");                
            }
            catch(IOException ex) {
                System.out.println(
                    "Error reading file '");                  
                // Or we could just do this: 
                // ex.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }       
    }

0 个答案:

没有答案