• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

conditional select stmt problem in java servlet

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys.. im new on this forum so i dont really know if i should post this topic at the Java beginner's thread or here..

Anyway, i am new in studying java and java servlet. i am primarily interested with JBDC and java servlet's interaction and linkage. currently, i am doing some hands-on experiment using microsoft access as my testing database. Here's a snippet of my code..(warning: they may look really crude..but i hope you will bear with my limited coding experience using servlet).

I have defined a datasource called 'test' that points to my test database.

// connecting to database

Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:test");

stmt = con.createStatement();
ResultSet usrName = null;
String myname = "Renee";
String sqlstmt = "SELECT userID from userlist where username = " + myname;

usrName = stmt.executeQuery(sqlstmt);

while(usrName.next()) {
out.print(usrName.getObject(1).toString());
out.print("\n");
}

A simple 'Select statement' works fine. but as soon as i include the 'where clause' i always get this problem ..java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

Please help...
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try adding single quotes:

String sqlstmt = "SELECT userID from userlist where username = '" + myname+ "'";

Dave
 
Renee de Castro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear david,

thanks for your reply...it worked!




best regards,
renee
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Happy coding.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic