2

标题是我得到的错误,当我单击加载时,我的程序冻结了。我认为这是因为我在声明中做声明,但据我所知,这是解决我的问题的唯一方法。通过加载,我只想重新填充患者列表,但为此我还需要了解他们的情况。代码有效,底部方法是我要修复的。我认为问题是我有 2 条陈述打开,但我不确定。加载:

public void DatabaseLoad()
{
    try
    {
        String Name = "Wayne";
        String Pass= "Wayne";
        String Host = "jdbc:derby://localhost:1527/Patients";
        Connection con = DriverManager.getConnection( Host,Name, Pass);
        PatientList.clear();
        
        
        Statement stmt8 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,        
             ResultSet.CONCUR_UPDATABLE);
        String SQL8 = "SELECT * FROM PATIENTS";
        ResultSet rs8 = stmt8.executeQuery( SQL8 );
        ArrayList<PatientCondition> PatientConditions1 = new ArrayList();
        
        while(rs8.next())
        {
            PatientConditions1 = LoadPatientConditions();
        }
        
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,    
            ResultSet.CONCUR_UPDATABLE);
        String SQL = "SELECT * FROM PATIENTS";
        ResultSet rs = stmt.executeQuery( SQL );
        
        while(rs.next())
        {
            int id = (rs.getInt("ID"));
            String name = (rs.getString("NAME"));
            int age = (rs.getInt("AGE"));
            String address = (rs.getString("ADDRESS"));
            String sex = (rs.getString("SEX"));
            String phone = (rs.getString("PHONE"));
            
            Patient p = new Patient(id, name, age, address, sex, phone,
                PatientConditions1);
            PatientList.add(p);
       }
        
        UpdateTable();
        UpdateAllViews();
        
        DefaultListModel PatientListModel = new DefaultListModel();
        
        for (Patient s : PatientList) {
            PatientListModel.addElement(s.getAccountNumber() + "-" + s.getName());
        }

        PatientJList.setModel(PatientListModel);
        
       }
      
    catch(SQLException err)
    {
        System.out.println(err.getMessage());
    }

 
}

这是返回ArrayList患者状况的方法

public ArrayList LoadPatientConditions()     
{ 
    ArrayList<PatientCondition> PatientConditionsTemp = new ArrayList();
    try
    {
        String Name = "Wayne";
        String Pass= "Wayne";
        String Host = "jdbc:derby://localhost:1527/Patients";
        Connection con = DriverManager.getConnection( Host,Name, Pass);
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,   
            ResultSet.CONCUR_UPDATABLE);
        String SQL = "SELECT * FROM PATIENTCONDITIONS";
        ResultSet rs5 = stmt.executeQuery( SQL );

        int e = 0;
        while(rs5.next())
        {
            e++;
            String ConName = (rs5.getString("CONDITION"));
            PatientCondition k = new PatientCondition(e,ConName);
            PatientConditionsTemp.add(k);
        }   
     }
     catch(SQLException err)
     {
         System.out.println(err.getMessage());
     }
  
     return PatientConditionsTemp;
 }
4

4 回答 4

5

我有一个类似的问题。我正在连接到托管在本地服务器上的 derby db。我创建了 2 个同时连接:

  • 松鼠
  • 使用ij工具
当连接对表进行修改时,它首先获得特定表的锁定。只有在提交事务后,连接才会释放此锁。因此,如果第二个连接尝试读/写同一个表,则消息提示说:
错误 40XL1:在请求的时间内无法获得锁

要解决此问题,修改表的连接必须提交其事务。

希望这可以帮助 !
于 2016-10-26T13:10:42.743 回答
3

这是一个很好的起点:http ://wiki.apache.org/db-derby/LockDebugging

于 2013-03-23T14:27:48.433 回答
2

您还需要关闭语句和结果集,以便在重新启动程序时它们不会打开。在 try 和 catch 语句中的代码行末尾添加stmt.close();和。rs.close();

于 2013-10-28T09:25:51.337 回答
0

Why could you not use the same connection object to do both the queries? Like pass that connection object to the LoadPatientConditions() as a parameter and use it there.

于 2013-03-23T04:05:48.393 回答