This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hey guys, Howdy! Well I was trying out a simple UI with Java which retrives values from MySQL database. I'm using Netbeans Version 6.1 & Linux Platform. The whole idea is pretty simple! I use a list, button and 4 text boxes. The list contains names of friends table and on clicking the button I display values in those textboxes. The database connection works perfect as I could see the names in the list(names from 'name' column in friends table). But when I click the button I get these following errors: SQLException: Unknown column 'x' in 'where clause' SQLState: 42S22 ErrorCode: 1054
This is the piece of code where the above mentioned action takes place:
getNameButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { //connectToDB(); Statement statement = connec.createStatement(); ResultSet rs = statement.executeQuery("select * from friends where name = " + detailsList.getSelectedValue());
If you compare string attributes you need to enclose the string values in single quotes, like "select * from xyz where abc = 'x' ". Otherwise the DB will think that "x" is the name of an attribute.
A good way to debug this kind of problem is to print the offending query to the console or a log file, and then to run it directly against the DB, using the DB's command-line interface. That makes it easier to pinpoint the problem.