如何读取文件并将其内容添加到数据库? [英] How to read a file and add its content to database?
问题描述
乔治19 180 75
保罗20 182 84
laura 21 176 73
我想要做的是读取这个文件并添加行到mysql数据库中的表。我写了这段代码读取文件:
public static void patients()throws IOException {
try {
in = new BufferedReader(new FileReader(new File(patientlist.txt))); ((read = in.readLine())!= $(
)
catch(FileNotFoundException e){System.out.println(There was a problem:+ e) null){
System.out.println(read);
$ bread是文件中的值。我想插入这些值到我的数据库中的表中,每个行上有参数(名称,年龄,身高,体重)的4个值。我无法找到如何分隔线上的价值观。因为我希望乔治,保罗和劳拉在数据库名称的参数下等,所以我可以使用选择在未来?谢谢您的帮助!
我已经写了一些这样的代码,可以检查一下吗?
public static void main(String [] args)throws IOException {
PreparedStatement preparedstatement = null;
连接连接= DBConnection();
尝试{
String read = null;
in = new BufferedReader(new FileReader(patientlist.txt)); ((read = in.readLine())!= null){
String [] splited = read.split(\\s +);
name = splited [0];
age = splited [1];
height = splited [2];
weight = splited [3];
$ b catch(IOException e){System.out.println(There was a problem:+ e);}
try {
addpatient(连接,准备状态,姓名,年龄,身高,体重);
if(connection!= null)
try {connection.close();} catch(SQLException ignore){}
}
$ b catch(SQLException error){ System.out.println(error);}
$ b $ public static void addpatient(Connection connection,PreparedStatement preparedstatement,String name,String age,String height,String weight)throws SQLException { b $ b 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循环,但我的编程不是很好,请帮助,谢谢。 解决方案患者列表包含患者,所以我要先创建一个类 Patient
。然后,我会为文件中的所有行创建 Patient
实例。
pre $ public class Patient {
private String name;
private int age;
private int height;
private int weight;
$ b $公共病人(字符串行){
String [] values = line.split(\\s +);
name = values [0];
年龄=值[1];
height = values [2];
weight = values [3];
}
//未显示:字段的getters / setters
}
现在,阅读行并创建对象
List< Patient> patients = new ArrayList<>(); ((line = in.readLine())!= null){
patients.add(new Patient(line));
}
并使用患者列表将数据保存到数据库。为每个患者,准备一个插入语句并坚持。
I have a text file called patientlist which looks like:
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. 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. 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? 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(); 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.
解决方案 A patient list contains "patients" so I'd first create a class Patient
. Then I'd create Patient
instances for all lines in the file.
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.
这篇关于如何读取文件并将其内容添加到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!