0

So I have this program that reads the text file and add its content to my database. I have no problem here. But when new content is added to file and the program is run again, all the values are stored in the database from the beginning, but i just want the new content to be added on top when i run the program for the second time. How can I do this, here is my code. Thanks in advance!!

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


    }
15
  • How about querying the database to check if the record already exists before you insert it? Commented Sep 12, 2013 at 13:34
  • And how is that done, I'm not an experienced programmer? Commented Sep 12, 2013 at 13:36
  • So your challenge is to detect the existence of previously inserted records? This isn't a Java question. It's pure SQL, really. Your answer will look something like "if not exists (select 1 from allpatients where ....) insert into allpatients values(...)". Commented Sep 12, 2013 at 13:36
  • Is there a UNIQE key? Is name unique? Can you empty your source file after added it in the database? Please consider to adjust indentation in your code to simplify readibility. Commented Sep 12, 2013 at 13:38
  • So i won't change my code but enter a statement in sql? I don't know what to write though.. Commented Sep 12, 2013 at 13:39

1 Answer 1

1
if (!isPatent(connection, preparedstatement, name)) 
    addpatient(connection, preparedstatement, name, age, height, weight);



    public static boolean isPatient(Connection connection,
            PreparedStatement preparedstatement, String name)
            throws SQLException {
        boolean exists = false;
        preparedstatement = connection
                .prepareStatement("select * from allpatients where name = ?");

        preparedstatement.setString(1, name);
        ResultSet rs = preparedstatement.executeQuery();
        if (rs.next()) {
            exists = true;
        }
        rs.close();
        return exists;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

True mauretto. Think that it's the problem he wants to solve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.