my dog learned polymorphism*
The moose likes JSP and the fly likes Help with JSP Page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Help with JSP Page" Watch "Help with JSP Page" New topic
Author

Help with JSP Page

Darren Thomas
Greenhorn

Joined: Nov 06, 2003
Posts: 1
keep getting a error java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state. Have opened two database connections. Need to get the username from my users table to be displayed in a table along with info from my items table, hope you can help
Here is the code from the JSP page
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc dbc:GuestBook");
Statement statement = conn.createStatement();
String sql = "SELECT * FROM Items";
ResultSet rs = statement.executeQuery(sql);
String Email = rs.getString("Email");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn2 = DriverManager.getConnection("jdbc dbc:GuestBook");
Statement statement2 = conn.createStatement();


String sql1=("SELECT Username FROM Users WHERE Email='"+Email+"'");
ResultSet rs1= statement2.executeQuery(sql1);
while (rs1.next()) {
while (rs.next()) {
%>
<tr>
<td><%= rs.getString("Isbn") %></td>
<td><%= rs.getString("Title") %></td>
<td>�<%= rs.getString("Price") %></td>
<td><%= rs1.getString("Username") %></td>
<td width="34%"><%= rs.getString("Email") %></td>
</tr>
<%
}
%>
<%
if (statement != null)
statement.close();
if (conn != null)
conn.close();
if (statement2 != null)
statement2.close();
if (conn2 != null)
conn2.close();
}
}
catch (Exception e) {out.print(e);}
%>
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
String Email = rs.getString("Email");


You must first call rs.next() before you can do this. There are alot
of other errors in the code but this is the first.
You don't have to make 2 instances of Connection when you are accessing
the same database.
sql1=("SELECT Username FROM Users WHERE Email='"+Email+"'");

You are doing this before the while loop plus you have set the Email variable outside the loop! This means that you will only
get the users with the same email. ( When the code works ) You must have both queries in a loop!
It is better if you do this.

Then you don't have to make two recordSets.
// Hope this helps for a starter
[ November 06, 2003: Message edited by: Mathias Nilsson ]

SCJP2 , MCP( 70-229 ) , Preparing For SCWDC
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with JSP Page
 
Similar Threads
cookies problem
Getting Problem in Delete operation in Struts application
two dropdown menus
Invalid cursor state
Display a list based on JSP input