• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conditional select stmt problem in java servlet

 
Renee de Castro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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...
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Happy coding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic