This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes Not able to retrieve values from MySQL(using simple UI) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Not able to retrieve values from MySQL(using simple UI)" Watch "Not able to retrieve values from MySQL(using simple UI)" New topic
Author

Not able to retrieve values from MySQL(using simple UI)

Sandeep Swaminathan
Ranch Hand

Joined: Apr 23, 2008
Posts: 52
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(rs.next())
{
nameText.setText(rs.getString("name"));
degreeText.setText(rs.getString("degree"));
//remarksText.setText(rs.getString("Remarks"));
initialsText.setText(rs.getString("initials"));
marksText.setText(rs.getString("marks"));
}

//rs.close();
}
catch(SQLException ee)
{
displaySQLErrors(ee);
}
}
}

What do I do to overcome this issue?


Music is all around us. All you have to do is just LISTEN to it,
Sandeep Swaminathan
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
Welcome to JavaRanch.

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.


Ping & DNS - my free Android networking tools app
Sandeep Swaminathan
Ranch Hand

Joined: Apr 23, 2008
Posts: 52
Bingo!! It works . . . Thanks a ton
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

prepared statement is another best way to handle this..


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Sandeep Swaminathan
Ranch Hand

Joined: Apr 23, 2008
Posts: 52
Well ! I tried that too & got the result! Cool .. Thanks Saif
[ January 02, 2009: Message edited by: Sandeep Swaminathan ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Not able to retrieve values from MySQL(using simple UI)