0

So I am doing a search GUI for full pc systems. The button Get Info is the action button that retrieves the values from the MySQL database and puts them in the jTextfields, but when I press on Get Info, nothing happends, and no errors.

This is the method that gets all the info, and the method is located in class DBAccess which contains all the SQL methods + the connection to database.

public SQLException getpc(fullpc fp) {
        ResultSet rs;
        connect ();
        try {
            rs = stmt.executeQuery("SELECT * FROM fullsystems WHERE Name = " + fp.name);
            while(rs.next()) {
                fp.id = rs.getString("ID");
                fp.name = rs.getString("Name");
                fp.cpu = rs.getString("Processor");
                fp.gpu = rs.getString("Graphics");
                fp.ram = rs.getString("Memory");
                fp.p = rs.getString("Price");
                fp.quant = rs.getString("Available quantity");
            }
        }
        catch (SQLException e) {
            return e;
        }
        return null;
    }

And this is the class that contains all the attributes of the table:

package area51project;
public class fullpc {
    public String id;
    public String name;
    public String cpu;
    public String gpu;
    public String ram;
    public String mem;
    public String p;
    public String quant;
}

And at last, the code that is put into the Get Info button:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        fullpc fp = new fullpc();
        DBAccess db = new DBAccess();
        fp.name = (String) jComboBox1.getSelectedItem();
        db.getpc(fp);
        cpu.setText(fp.cpu);
        ram.setText(fp.ram);
        storage.setText(fp.mem);
        gpu.setText(fp.gpu);
        price.setText(fp.p);
        quantity.setText(fp.quant);
         
    } 

The jCombobox1 is just the combobox that is near the Get Info button, so I choose the name of the PC, and then pressing the button shows the info of that PC in the jTextfields. However, Price and Quantity are shown in jLabels.

Help?

  • Do not use `public` properties in your pojo. Use `private` and getter and setter – Jens Jun 01 '17 at 12:17
  • You mean in my fullpc class, make the attributes private and give each of them getter and setter? If so, tried that. Didn't work. :( – Ahmad Madi Jun 01 '17 at 12:20
  • Maybe it did not work, but is is better programming – Jens Jun 01 '17 at 12:21
  • Also use prepared Statements – Jens Jun 01 '17 at 12:21
  • Alright. What are prepared statements? Never took them in school... what do they do and where are they placed? – Ahmad Madi Jun 01 '17 at 12:23
  • prepared Statements prevent you for SQL-injection. Use Google to learn about it – Jens Jun 01 '17 at 12:24
  • Class name should be `FullPC` and prepared statements are queries that you can bind parameters to. Also look up DAO because you can have the queries returned back to you already in the class you prefer instead of casting all the values. – Mr. Polywhirl Jun 01 '17 at 12:26
  • Prepared Statement is there, and still nothing... pressing on Get Info does nothing... even the jLabels for prices that hold "N/A" as default they disappear after pressing the button – Ahmad Madi Jun 01 '17 at 12:49

0 Answers0