File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes conditional select stmt problem in java servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "conditional select stmt problem in java servlet" Watch "conditional select stmt problem in java servlet" New topic
Author

conditional select stmt problem in java servlet

Renee de Castro
Greenhorn

Joined: Mar 12, 2005
Posts: 4
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

Joined: Mar 06, 2001
Posts: 13459

try adding single quotes:

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

Dave
Renee de Castro
Greenhorn

Joined: Mar 12, 2005
Posts: 4
dear david,

thanks for your reply...it worked!




best regards,
renee
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Happy coding.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conditional select stmt problem in java servlet