0

I have a text file looks like:

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

In my program I read this file and add its content to a table in my database which has parameters(name, age, height, weight). The code looks like

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

    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];      
            addpatient(connection, preparedstatement, name, age, height, weight);
        }
    }
    catch (IOException e) {System.out.println("There was a problem: " + e);}
        if (connection != null)
        try{connection.close();} catch(SQLException ignore){} 
    }

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

    }

When I run this code this adds george, paul to name column etc. But my problem is when I add a new entry to my file like:

   Rachel   20   175  78

and run the program again, it adds all the values into my database again but I just want to add the newest entry. How can I do that. Is there something like append? Thanks for the help!

Solijoli
  • 404
  • 2
  • 6
  • 16
  • 1
    You might find this interesting : http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql – jrochette Sep 10 '13 at 12:15

2 Answers2

0

First thing, in your code prepared statement has no significance meaning. Means every time prepared statement is created when you call the addpatient method. Besides that you can do like this :

In your main method :
{
    preparedstatement=connection.prepareStatement("insert into allpatients(name, age, height, weight) values(?,?,?,?)");

   //Your rest code comes here

    addPatient(preparedstatement,name,age,height,weight);
}

public static void addPatient(PreparedStatment preparedstatement, String name, String age, String height, String weight ){
    preparedstatement.setString(1, name);
    preparedstatement.setString(2, age);
    preparedstatement.setString(3, height);
    preparedstatement.setString(4, weight);
    preparedstatement.executeUpdate();
}

And every time you execute the addPatient method, it adds records in current table only.

Vimal Bera
  • 9,887
  • 4
  • 21
  • 47
  • No what I meant was when I run for the first time it adds the current names and ages etc to the database. I want them to stay in the database. And from another program I put another entry to the file. When I run this program again I want the first records to stay and just add the new entry to my database – Solijoli Sep 10 '13 at 12:12
  • @Solijoli check my answer. And if you are creating the table in the application then you might face the problem. – Vimal Bera Sep 10 '13 at 12:25
-1

u can try preparedstatement.clearBatch() method and when u finished a jdbc commit,u should close it. The connection object has close() method;

I suggested that if u have many data to store in one time u can use addBatch() method with PreparedStatement.

  • I close the connection, that is in the if statement in my code. Where should I put clearBatch(), I don't know how that works actually but it sounds like it can work – Solijoli Sep 10 '13 at 12:16