0

所以我有这个程序可以读取文本文件并将其内容添加到我的数据库中。我这里没有问题。但是当新内容被添加到文件中并且程序再次运行时,所有的值都从一开始就存储在数据库中,但我只想在第二次运行程序时将新内容添加到顶部。我该怎么做,这是我的代码。提前致谢!!

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


    }
4

1 回答 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;
    }
于 2013-09-12T13:54:16.993 回答