简体   繁体   English

如何读取文件并将其内容添加到数据库?

[英]How to read a file and add its content to database?

I have a text file called patientlist which looks like: 我有一个名为Patientlist的文本文件,看起来像:

    george  19  180 75
    paul    20  182 84
    laura   21  176 73

What I want to do is to read this file and add the lines to a table in mysql database. 我想做的是读取此文件并将行添加到mysql数据库中的表中。 I have written this code that reads the file: 我已经编写了读取文件的代码:

    public static void patients() throws IOException{
    try {
        in= new BufferedReader(new FileReader(new File("patientlist.txt")));
    }
    catch (FileNotFoundException e) {System.out.println("There was a problem: " + e);}
    while((read = in.readLine()) != null){
        System.out.println(read);
     }
    }

"read" is the values from the file. “读取”是文件中的值。 I want to insert these values to my table in my database which has the parameters(name, age, height, weight) the 4 values on each line. 我想将这些值插入到数据库中的表中,该表具有每行4个值的参数(名称,年龄,身高,体重)。 I couldn't find out how to seperate the values on a line. 我找不到如何分隔一行中的值。 Because I want george, paul and laura to be under the parameter of name at the database etc. so I can use select in future? 因为我想让George,Paul和Laura在数据库等的名称下,所以我将来可以使用select吗? Thanks for the help! 谢谢您的帮助!

I have written some code like this can you check it out? 我已经写了一些这样的代码,您可以检查一下吗?

     public static void main(String[] args) throws IOException {

    PreparedStatement preparedstatement = null;
    Connection connection = DBConnection();
    try{
        String read=null;
        in = new BufferedReader(new FileReader("patientlist.txt")); 
        while ((read = in.readLine()) != null) {
            String[] splited = read.split("\\s+");
            name = splited[0];
            age = splited[1];
            height = splited[2];
            weight = splited[3];
        }
    }
    catch (IOException e) {System.out.println("There was a problem: " + e);} 

    try {
        addpatient(connection, preparedstatement, name, age, height, weight);               
        if (connection != null)
            try{connection.close();} catch(SQLException ignore){}
        }

        catch (SQLException error) {System.out.println(error);}

    }
    public static void addpatient(Connection connection, PreparedStatement preparedstatement, String name, String age, String height, String weight) throws SQLException{
    preparedstatement=connection.prepareStatement("insert into allpatients(name, age, height, weight) values(?,?,?,?)");
    preparedstatement.setString(1, name);
    preparedstatement.setString(2, age);
    preparedstatement.setString(3, height);
    preparedstatement.setString(4, weight);
    preparedstatement.executeUpdate();

    }

Connection connection = DBConnection(); 连接连接= DBConnection(); line creates the connection with database which has another method which I didn't write here. 行创建了与数据库的连接,该数据库具有另一个我在这里没有写的方法。 I think the problem is with my while loop, I think I should put a for loop too but my programming isn't very good please help, thanks. 我认为问题出在我的while循环上,我想我也应该放入for循环,但是我的编程不是很好,请帮忙,谢谢。

You can do 你可以做

read.split("\\s+");

or, if your values are separated by tab , 或者,如果您的值用tab分隔,

read.split("\t");

With this code: 使用此代码:

String s = "george  19  180 75";

String[]split = s.split("\\s+");
for (int i = 0; i < split.length; i++) {
    System.out.println(split[i]);
}

The output is: 输出为:

george
19
180
75

A patient list contains "patients" so I'd first create a class Patient . 患者列表包含“患者”,因此我首先创建一个类Patient Then I'd create Patient instances for all lines in the file. 然后,我将为文件中的所有行创建Patient实例。

public class Patient {
   private String name;
   private int age;
   private int height;
   private int weight;

   public Patient(String line) {
     String[] values = line.split("\\s+");
     name = values[0];
     age = values[1];
     height = values[2];
     weight = values[3];
   } 

   // not shown: getters/setters for the fields
}

Now, read the lines and create the objects 现在,阅读这些行并创建对象

List<Patient> patients = new ArrayList<>();
while((line = in.readLine()) != null){
    patients.add(new Patient(line));
}

and use the list of patients to persist the data to the database. 并使用患者列表将数据持久保存到数据库中。 For each patient, prepare an insert statement and persist. 为每个患者准备一个插入语句并坚持下去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM