Java.sql.SQLException doing a SELECT -


when try execute code:

    try {          pstm = connection.preparestatement("select * menu (menuname '?%')");         pstm.setstring(1,searchedmenu.getmenuname());         rs = pstm.executequery();         while (rs.next()) {             menu menu=new menu();             menu.setmenuid(rs.getint("menuid"));             menu.setmenuname(rs.getstring("menuname"));             temp.add(menu);         } 

i error in subject, why? purpose search string or part of it. thank you.

edit: error is: java.sql.sqlexception: parameter index out of range (1 > number of parameters, 0). , referred line: pstm.setstring(1,searchedmenu.getmenuname());

the error fact don't have parameter markers set, because put question mark string:

menuname '?%' 

so when this:

pstm.setstring(1,searchedmenu.getmenuname()); 

there nothing set, , throw error. if check stack trace, it's you're seeing message number of parameter markers.

you should concatenate percent string in java. concatenating in sql provide less consistent results, varying rdbms.

so recommend writing sql parameter this:

select * menu (menuname ?) 

and setting parameter in java code this:

pstm.setstring(1, searchedmenu.getmenuname() + "%"); 

also consider trimming menu name, if know there spaces. results may not expect if have percent sign after bunch of spaces.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -