So I am making a small software for a Hotel Management System and I need some help calling the values from the database. In my database I have a table called "keys". This table has the following columns: idfood , iditem , amount. 'idfood' and 'iditem' are both primary keys forming a composite key.
This is what the database looks like with test values : database.JPG
Now I made an interface in NetBeans which looks like this: interface.JPG
What I want to happen is, when I enter a value in the text box (For example '1') all the records which have idfood as '1' should display in the table. I would appreciate any help.
Following is my code (the button named 'Search'):
DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel(); String a = jTextField1.getText(); try { Statement s = DB.getConnection().createStatement(); ResultSet rs = s.executeQuery("SELECT * FROM keys WHERE idfood = '"+a+"'"); while(rs.next()){ Vector v = new Vector(); v.add(rs.getString("idfood")); v.add(rs.getString("iditem")); v.add(rs.getString("amount")); dtm.addRow(v); } } catch (Exception e) { e.printStackTrace(); }
When I run the above code I get the following error in NetBeans 7.3:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys WHERE idfood = '1'' at line 1
Please help. I'm still a bit new to java. Still a student. So please don't use complicated jargon. Much appreciated