Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

invalid handle--preparedStatement.setString()

 
Frances Fang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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

 
asha kiran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!