Java 尝试在Netbeans中将多个患者添加到我的患者表中

Java 尝试在Netbeans中将多个患者添加到我的患者表中,java,database,netbeans,Java,Database,Netbeans,我有一个增强的for循环,它在我的患者阵列中循环 在这个循环中,我有一个insert语句,用于插入患者的号码、姓名、地址和电话号码 但是,当阵列中有多个患者时,以前的患者会被写入数据库。有没有什么方法可以让我转到表的下一行,这样我就不会重写前面的所有条目 这是我正在使用的方法 public void databaseSave( ArrayList <Patient> pList ) { try { String name = "Shaun";

我有一个增强的for循环,它在我的患者阵列中循环

在这个循环中,我有一个insert语句,用于插入患者的号码、姓名、地址和电话号码

但是,当阵列中有多个患者时,以前的患者会被写入数据库。有没有什么方法可以让我转到表的下一行,这样我就不会重写前面的所有条目

这是我正在使用的方法

public void databaseSave( ArrayList <Patient> pList )
    {

    try
    {
        String name = "Shaun";
        String pass = "Shaun";
        String host = "jdbc:derby://localhost:1527/DentistDatabase";

        Connection con = DriverManager.getConnection(host, name, pass);

        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

        //Statement stmt = con.createStatement();

        System.out.println("Before the delete");


        String query = "DELETE "
                    +  "FROM SHAUN.PATIENT";


        System.out.println("After the delete");


        stmt.executeUpdate(query);


        String select = "SELECT * FROM SHAUN.PATIENT";

        ResultSet result = stmt.executeQuery(select);


        System.out.println("Before loop");

        for ( Patient p: pList )
        {

            patientInsertSQL = "Insert Into SHAUN.PATIENT VALUES (" + p.getPatientNum() + ", '"
            + p.getPatientName() + "', '" + p.getPatientAddress() + "', '"
            + p.getPatientPhone() + "')";


            System.out.println("In the loop!");

        }


        int res = stmt.executeUpdate(patientInsertSQL);

        System.out.println(res);


        stmt.close();
        result.close();
        con.commit();

        System.out.println("After Loop and close");

    }
    catch (SQLException err)
    {
        System.out.print(err.getMessage());
    }
}
public void databaseSave(ArrayList pList)
{
尝试
{
String name=“Shaun”;
字符串pass=“Shaun”;
String host=“jdbc:derby://localhost:1527/DentistDatabase";
Connection con=DriverManager.getConnection(主机、名称、过程);
语句stmt=con.createStatement(ResultSet.TYPE\u SCROLL\u敏感,ResultSet.CONCUR\u可更新);
//语句stmt=con.createStatement();
System.out.println(“删除前”);
String query=“删除”
+“来自肖恩,病人”;
System.out.println(“删除后”);
stmt.executeUpdate(查询);
String select=“select*FROM SHAUN.PATIENT”;
结果集结果=stmt.executeQuery(选择);
System.out.println(“循环前”);
用于(患者p:pList)
{
patientInsertSQL=“插入SHAUN.PATIENT值(“+p.getPatientNum()+”,”
+p.getPatientName()+“,”+p.getPatientAddress()+“,”
+p.getPatientPhone()+“')”;
System.out.println(“在循环中!”);
}
int res=stmt.executeUpdate(patientInsertSQL);
系统输出打印项次(res);
stmt.close();
result.close();
con.commit();
System.out.println(“循环结束后”);
}
捕获(SQLException错误)
{
System.out.print(err.getMessage());
}
}

您必须在每次迭代时执行查询,或者使用SQL批处理插入

for ( Patient p: pList )
        {
            patientInsertSQL = "Insert Into SHAUN.PATIENT VALUES (" + p.getPatientNum() + ", '"+ p.getPatientName() + "', '" + p.getPatientAddress() + "', '"
            + p.getPatientPhone() + "')";
        int res = stmt.executeUpdate(patientInsertSQL);

}

for(Patient p:pList) {
PatientInsertSQL = "Insert into patient Values(x,y,z)";
stmnt.addBatch(query);
}
stmnt.executeBatch();

顺便说一句,为了避免使用而不是语句,您必须在每次迭代时执行查询,或者使用SQL批插入

for ( Patient p: pList )
        {
            patientInsertSQL = "Insert Into SHAUN.PATIENT VALUES (" + p.getPatientNum() + ", '"+ p.getPatientName() + "', '" + p.getPatientAddress() + "', '"
            + p.getPatientPhone() + "')";
        int res = stmt.executeUpdate(patientInsertSQL);

}

for(Patient p:pList) {
PatientInsertSQL = "Insert into patient Values(x,y,z)";
stmnt.addBatch(query);
}
stmnt.executeBatch();

顺便说一句,为了避免使用而不是语句,语句
int res=stmt.executeUpdate(patientInsertSQL)应该在for循环中。

语句
int res=stmt.executeUpdate(patientInsertSQL)应该在for循环中。

执行应该在for循环中完成。执行应该在for循环中完成。太好了,这就解决了它。谢谢你的帮助(太好了,修好了。谢谢你的帮助=)