File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes JDBC - Retriving data from Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "JDBC - Retriving data from Database" Watch "JDBC - Retriving data from Database" New topic

JDBC - Retriving data from Database

Shashank Anand

Joined: Oct 30, 2008
Posts: 26
I have written a program to display data from sql server database table using ComboBox. First, I added the column of my table to ComboBox as below :
Conection con;
Statement stmt;
ResultSet rs;
JComboBox combo= new JComboBox();
con= DriverManager.getConnection("jdbcdbc:MyDSNname", "MyLogin", "MyPassword");
rs= stmt.executeQuery("SELECT user_id FROM user_table");
while (
//Two columns user_id & user_name: both of VARCHAR type

After running the code, I could see the ComboBox populated with the values of column user_id. Now I want to display the column values in corresponding textfields.

My code goes like this:

public void actionPerformed (ActionEvent e) {
if (e.getSource()==combo) {
try {
con= DriverManager.getConnection("jdbcdbc:MyDSNname", "MyLogin", "MyPassword");
String st= (String)combo.getSelectedItem();
stmt= con.createStatement();
rs= stmt.executeQuery ("SELECT user_id, user_name FROM user_table WHERE user_id = " + st);;
catch (Exception ex) { System.out.println ("Error:" + ex); }

The code compiled fine but I got runtime java.sql.SQLException - unknown column 'xxx' in 'where clause'.

Will anybody please tell me what went wrong with my code ?
[ November 12, 2008: Message edited by: Shashank Anand ]
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1082

Hi Shashank,

Welcome to javaranch !

What is datatype of user_id ?

If dataype of user_id is varchar or varchar2

then you will have to make a query like this


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Shashank Anand

Joined: Oct 30, 2008
Posts: 26
Thank you Shailesh.

This really solved my problem.

I agree. Here's the link:
subject: JDBC - Retriving data from Database
It's not a secret anymore!