• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

invalid handle--preparedStatement.setString()

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am working on a project talking to database.But I encounted a problem.
if (XXXQueryStmt==null){
String XXXQuery="SELECT * FROM "+tablename+
"WHERE "+tablename+".columnName=?"; XXXQueryStmt=con.prepareStatement
(XXXQuery);
}

XXXQueryStmt.setString(1,comboBoxItem);//err
rs=XXXQueryStmt.executeQuery();
//... some other codes
private PreparedStatement XXXQueryStmt;
private ResultSet rs;
private String tablename;//It is the tablaname in the database
//comboBoxItem is String type,which is assigned through comboBox.getSelectedItem();
the 1st round: I clicked one item in JComboBox,it shows the string of rs which is type of ResultSet.That's good.
the 2nd round & thereafter: There's a exception stating: invalid handle.I debugged, it is at XXXQueryStmt.setString(1,comboBoxItem);
Why? Anybody can help? Tks!
Frances

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frances Fang:
[B]Hi,
I am working on a project talking to database.But I encounted a problem.
since u r trying to get a string from a combo box and use it in the query
remember that in sql stmts strings should be in single quotes
hence try to alter ur code to
method 1
if (XXXQueryStmt==null){
String XXXQuery="SELECT * FROM "+tablename+
"WHERE "+tablename+".columnName='"+?+"'";

method 2
get the value of the combobox and store into a string(typecast) variable
and use
ps.setString(1,variablename)

 
Frances Fang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,Asha,
Thank you for yr help!
I tried your method,tried to put string of SQL stmt in single quote.But I failed at even the 1st round:-- invalid argument value. So I figure that it should not be in single quote.
I am thinking,why setString(index,string) of PreparedStatement can be ok at the 1st round but failed thereafter? Am I missing something here?
Frances
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic