dcsimg
Help With Servlet & SQL, method not returning anything
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, April 15, 2003 10:00 PM

I'm working on a project for my Java class and I'm running into trouble with the JDBC section. My project is laid out as follows: This project consists of a HTML page, a servlet, and a database. I have already wrote up the code for the interaction between the login page and the servlet, however, I cannot figure out how to use java to interact with a Microsoft Access database. I'm trying to call to a method (which has the coding for JDBC & SQL) by calling to it through the doPost method. The program refuses to go to that method. I'm not sure if it's a syntax error or if it's not even allowed in Java. I think I've narrowed the problem down to this line: String user = getUser(username, password); getU   More>>

I'm working on a project for my Java class and I'm running into trouble with the JDBC section. My project is laid out as follows:



This project consists of a HTML page, a servlet, and a database. I have already wrote up the code for the interaction between the login page and the servlet, however, I cannot figure out how to use java to interact with a Microsoft Access database.



I'm trying to call to a method (which has the coding for JDBC & SQL) by calling to it through the doPost method. The program refuses to go to that method. I'm not sure if it's a syntax error or if it's not even allowed in Java. I think I've narrowed the problem down to this line:



String user = getUser(username, password);


getUser is the method I was refering to in the previous paragraph. When getUser as it's own class, without dealing with paramters, it works fine. When I calling it with parameters, it doesn't seem to work. It will not even return a string when I call it.



			public String getUser(String username, String password) throws SQLException, ClassNotFoundException {
			

//perform SQL lookup
String dbUrl = "jdbc:odbc:cis375login";
String user = "";
String pass = "";

// Load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Register the driver
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());

// Establish connection to DB
Connection c = DriverManager.getConnection(dbUrl, user, pass);

// send SQL statement to the DBMS
Statement s = c.createStatement();

//quering the database
ResultSet r = s.executeQuery(
"SELECT Username, StudentID " +
"FROM Parking.Books Books " +
"WHERE " +
"(Username='" + username + "') " +
"ORDER BY StudentID");

while(r.next()) {
System.out.println(r.getString("Username") + " "+ r.getString("StudentID"));
}

s.close(); // Also closes ResultSet

return "Bob"; //This is not even being returned when the method is called
}


I hope I am being clear, if not please let me know. Thanks in advance for any help you can provide. It is much appreciated.

   <<Less

Re: Help With Servlet & SQL, method not returning anything

Posted By:   John_Smith  
Posted On:   Wednesday, April 16, 2003 04:10 AM

Hi,

I am a JSP as opposed to servlet person. However, ...

My Access connecting code looks like this:


Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

java.sql.Connection con = java.sql.DriverManager.getConnection("jdbc:odbc:test","","");
java.sql.Statement stmt = con.createStatement();
java.sql.ResultSet rs = stmt.executeQuery



I assume you have registered your database in Microsoft Wndows, and you have referenced "java.sql" . Only other thing I can see is that you are using getString as opposed to getInt for the student ID (which I assume is an integer).

Hope this helps

Good Luck

J smith

Aylmer, PQ

About | Sitemap | Contact