This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandeep Swaminathan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo!! It works . . . Thanks a ton
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prepared statement is another best way to handle this..
 
Sandeep Swaminathan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well ! I tried that too & got the result! Cool .. Thanks Saif
[ January 02, 2009: Message edited by: Sandeep Swaminathan ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!