• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

JDBC - Retriving data from Database

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection("jdbc:odbc:MyDSNname", "MyLogin", "MyPassword");
stmt=con.createStatement();
rs= stmt.executeQuery("SELECT user_id FROM user_table");
while (rs.next())
{
combo.addItem(rs.getString(1));
}
//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:

...
combo.addActionListener(this);
public void actionPerformed (ActionEvent e) {
if (e.getSource()==combo) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection("jdbc:odbc: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);
rs.next();
textField1.setText(rs.getString(1));
textField2.setText(rs.getString(2));
con.close();
}
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 ]
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



Shailesh
 
Shashank Anand
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Shailesh.

This really solved my problem.

Regards,
Shashank
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic