簡體   English   中英

如何讀取文件並將其內容添加到數據庫?

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

我有一個名為Patientlist的文本文件,看起來像:

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

我想做的是讀取此文件並將行添加到mysql數據庫中的表中。 我已經編寫了讀取文件的代碼:

    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);
     }
    }

“讀取”是文件中的值。 我想將這些值插入到數據庫中的表中,該表具有每行4個值的參數(名稱,年齡,身高,體重)。 我找不到如何分隔一行中的值。 因為我想讓George,Paul和Laura在數據庫等的名稱下,所以我將來可以使用select嗎? 謝謝您的幫助!

我已經寫了一些這樣的代碼,您可以檢查一下嗎?

     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();

    }

連接連接= DBConnection(); 行創建了與數據庫的連接,該數據庫具有另一個我在這里沒有寫的方法。 我認為問題出在我的while循環上,我想我也應該放入for循環,但是我的編程不是很好,請幫忙,謝謝。

你可以做

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

或者,如果您的值用tab分隔,

read.split("\t");

使用此代碼:

String s = "george  19  180 75";

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

輸出為:

george
19
180
75

患者列表包含“患者”,因此我首先創建一個類Patient 然后,我將為文件中的所有行創建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
}

現在,閱讀這些行並創建對象

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

並使用患者列表將數據持久保存到數據庫中。 為每個患者准備一個插入語句並堅持下去。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM