0

我创建了一个数据库,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 +"" ;
    }       
}
4

1 回答 1

0

让我重新表述一下 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();                   
    }        
}

这可能仍然无法立即工作,如果您遇到错误,请编辑您的帖子并告诉我们出了什么问题。顺便说一句:我注意到你有你的变量nameaddress所有这些都在patient集合中public。这没有错,但建议使用 getter 和 setter(就像你一样)并制作 variables private。这样您就可以控制如何从外部访问变量。

于 2013-04-28T22:36:27.533 回答