Java 从数据库加载到jlist会导致错误

Java 从数据库加载到jlist会导致错误,java,database,connection,Java,Database,Connection,我已经创建了一个数据库,JList中的每个条目都将添加到数据库中的一个表中。这很好,但我的下一个任务是将数据库中的任何内容加载到JList。我在按钮中创建了一个函数,但它会带来错误。我正在努力解决这个问题,所以我希望有人能解决它 谢谢 这是我的密码: JButton btnDb1 = new JButton("J"); btnDb1.addActionListener(new ActionListener() { public voi

我已经创建了一个数据库,JList中的每个条目都将添加到数据库中的一个表中。这很好,但我的下一个任务是将数据库中的任何内容加载到JList。我在按钮中创建了一个函数,但它会带来错误。我正在努力解决这个问题,所以我希望有人能解决它

谢谢

这是我的密码:

JButton btnDb1 = new JButton("J");          
btnDb1.addActionListener(new ActionListener() {                

    public void actionPerformed(ActionEvent arg0) {            
        try {       
            ResultSet rs = st.executeQuery("SELECT * FROM patient");
            while (rs.next()) {
                Patient patient = new Patient(patientname, patientaddress, patientphone, patientid);
                patient.setName(rs.getString("patientname"));
                patient.setAddress(rs.getString("patientaddress"));
                patient.setPhoneNum(rs.getString("patientphone"));
                patient.setID(rs.getInt("patientid"));
                MainDentist.model.addElement(patient);    
            }
        } catch (Exception e) {
            System.out.println(" Error ");                      
        }
    }
});

btnDb1.setBounds(200, 393, 120, 23);
contentPane.add(btnDb1);
以下是我的患者课程:

public class Patient { 
    public String patientName;
    public String patientAddress;
    public String patientPhone;
    public int patientID;

    public Patient(String patientname, String patientaddress, String patientphone,int patientid){
        patientName = patientname;
        patientAddress = patientaddress;
        patientPhone = patientphone;
        patientID = patientid;
    }

    public String setName(String patientname){            
        return patientName = patientname;
    }

    public String getName(){            
        return patientName;            
    }

    public String setAddress(String patientaddress){            
        return patientAddress = patientaddress;      
    }

    public String getAddress(){            
        return patientAddress;
    }

    public String setPhoneNum(String patientphone){
        return patientPhone = patientphone;
    }

    public String getPhoneNum(){            
        return patientPhone;
    }

    public int setID(int patientid){            
        return patientID = patientid;
    }

    public int getID(){            
        return patientID;            
    }

    public String toString() { // Printing the patient's details to the scroll pane
        return "Patient Name: " + patientName + ", PatientAddress: "
                + patientAddress + ", PatientPhone: " + patientPhone
                + ", patientID: " + patientID +"" ;
    }       
}

让我重新表述一下actionlistener——在评论中发布代码并没有真正的帮助:) 首先,我将把
actionPerformed
方法中的代码放到其他地方,最好是创建一些类来处理整个数据库的读取和写入。这样,您就不会将读取数据库与按下按钮混为一谈(也许您也想创建另一个读取数据库的按钮,这样您就不必再次编写所有代码)。无论如何,这是一个例子:

JButton btnDb1 = new JButton("J");          
btnDb1.addActionListener(new ActionListener() {               

    public void actionPerformed(ActionEvent arg0) {            
        readDatabase();
    }
});

public void onActionPerformed(){
    try { 
        // I don't know where you have that part of code, but you didn't create any statement variable. So here an example DB-connection: 
        String url = "jdbc:mysql://localhost/myDatabase";
        Connection connection = DriverManager.getConnection(url, "username", "password");
        Statement statement = connection.createStatement();      
        ResultSet rs = statement.executeQuery("SELECT * FROM patient");
        while (rs.next()) {
            // read the columns one by one, that way it may be easier to detect errors if one column is wrong
            String name = rs.getString("patientname");
            String address = rs.getString("patientaddress");
            String phone = rs.getString("patientphone");
            int id = rs.getInt("patientid");
            // now that you have all values, create the patient and add it to the model
            // if you have all parameters for the constructor, you don't need to use the setters to set the name and address …
            MainDentist.model.addElement(new Patient(name, address, phone, id));    

        }
    } catch (Exception e) {
        // as JB Nizet suggested, it is easier to detect errors, when you print the whole stack trace (it will tell you in which line the exception gets thrown
        e.printStackTrace();                   
    }        
}

这可能仍然无法立即工作,如果您有错误,请编辑您的帖子并告诉我们哪里出了问题。顺便说一句:我注意到您的变量
名称
地址
,所有这些都在
患者
中设置为
公共
。这并没有错,但建议使用getter和setter(就像您所做的那样),并将变量设置为private。这样,您就可以控制如何从外部访问变量。

这一行如何,创建患者的参数来自哪里:
Patient Patient=new Patient(patientname、patientaddress、patientphone、patientTid)
为什么不直接将它们读入构造函数:
Patient-Patient=new-Patient(rs.getString(“patientname”)、rs.getString(“patientaddress”)、rs.getString(“patientphone”)、rs.getInt(“patientid”)?不管怎样,您到底遇到了什么样的错误?首先要做的是:用
e.printStackTrace()
替换
System.out.println(“错误”)
,甚至
抛出新的运行时异常(e)
,然后阅读您收到的错误消息,而不是忽略它,然后想知道问题可能是什么。它说st没有在下面的行中声明:ResultSet rs=st.executeQuery(“SELECT*FROM patient”);您在哪里申报
st
?在对
语句执行查询时,首先需要根据数据库连接创建语句。如下所示:
stringurl=“jdbc:mysql://localhost/myDatabase";  Connection Connection=DriverManager.getConnection(url,“用户名”、“密码”);语句st=connection.createStatement()