Solijoli
Solijoli

Reputation: 474

How to add the content of a file to database?

I have a text file looks like:

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

I want to read this file and add its content to a table with parameters(name, age, height, weight) in my database. The names george, paul, laura should be added to names and etc. I have written a code like this.

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

    PreparedStatement preparedstatement = null;

    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];       
        }
        try {
            addpatient(connection, preparedstatement, name, age, height, weight);   
            if (connection != null)
            try{connection.close();} catch(SQLException ignore){}
            }
        catch (SQLException error) {System.out.println(error);} 
    }

    catch (IOException e) {System.out.println("There was a problem: " + e);}        
    }

    public static void addpatient(Connection connection, PreparedStatement preparedstatement, String name, String age2, String height2, String weight2) 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();

}

I think the problem is with my while loop, maybe I have to add a for loop I couldn't manage to do it, I'm not a very good programmer and I appreciate help. Thanks!

Upvotes: 0

Views: 239

Answers (2)

hanish.kh
hanish.kh

Reputation: 517

You are doing it wrong. What you need to do is add the values to preparedstatement in the loop and then execute the statement out side the loop

preparedstatement = //Initialize
while(condition){
   preparedstatement.setString(1, name);
   preparedstatement.setString(2, age);
   preparedstatement.setString(3, height);
   preparedstatement.setString(4, weight);
   preparedstatement.addBatch();
}
preparedstatement.executeBatch();

Upvotes: 0

newuser
newuser

Reputation: 8466

while ((read = in.readLine()) != null) {
            String[] splited = read.split("\\s+");
            name=splited[0];
            age=splited[1];
            height=splited[2];
            weight=splited[3];   
            addpatient(connection, preparedstatement, name, age, height, weight);   
         }

if (connection != null)
            try{connection.close();} catch(SQLException ignore){}
            }

Place the addpatient() inside the while loop then only it will call.

Upvotes: 1

Related Questions