Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me rephrase the actionlistener – posting code in comments isn't really helpfull :) First I would put the code from your <code>actionPerformed</code> method somewhere else, best would be even to create some class that handles the whole reading and maybe writing of the database. 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:</p> <pre><code>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(); } } </code></pre> <p>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 <code>name</code>, <code>address</code> and all this in the <code>patient</code> set to <code>public</code>. This isn't wrong but it is recommended to use getter and setters (as you do) and make the variables <code>private</code>. That way you can control <em>how</em> the variables get accessed from outside.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload