简体   繁体   English

从数据库加载到jlist会带来错误

[英]Loading from a database to a jlist brings errors

I have created a database and every entry from the JList will be added to a table in the database. 我已经创建了一个数据库,并且JList中的每个条目都将添加到数据库中的表中。 This work perfectly, but my next task is to get whatever is in the database to load to the JList. 这可以完美地工作,但是我的下一个任务是获取数据库中的任何内容以加载到JList。 I have a function created within the button but it brings up errors. 我在按钮内创建了一个函数,但出现错误。 I'm struggling with how to fix this so I hope somebody can resolve it. 我正在努力解决此问题,希望有人能解决。

Thanks 谢谢

Here is my code: 这是我的代码:

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

Here is my patient class: 这是我的病人课:

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 +"" ;
    }       
}

Let me rephrase the actionlistener – posting code in comments isn't really helpfull :) First I would put the code from your actionPerformed method somewhere else, best would be even to create some class that handles the whole reading and maybe writing of the database. 让我改写动作侦听器–在注释中发布代码并没有真正的帮助:)首先,我将把您的actionPerformed方法中的代码放在其他地方,最好甚至是创建一个类来处理整个数据库的读取和编写。 That way you don't mix reading the database with pushing buttons (maybe you want to create another button that reads the database, too. Then you don't have to write all the code again). 这样,您就不会将读取数据库与按钮混在一起(也许您也想创建另一个读取数据库的按钮。这样就不必再次编写所有代码)。 Anyway, this is an example: 无论如何,这是一个示例:

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

This still may not work right away, if you get errors, edit your post and tell us what goes wrong. 如果您发现错误,请立即编辑并告诉我们出了什么问题,这可能仍然无法立即解决。 BTW: I noticed that you have your variables name , address and all this in the patient set to public . BTW:我注意到,你有你的变量nameaddress而这一切在patient设置为public This isn't wrong but it is recommended to use getter and setters (as you do) and make the variables private . 这没错,但是建议使用getter和setters(如您所愿)并将变量设为private That way you can control how the variables get accessed from outside. 这样,您可以控制如何从外部访问变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM