servlet calls database but brings back an extra row how?
I have a servle which connects to a database and brings back the items and puts them in a table the problem is if i look in the table there are 41 rows but my table display 42 so if i click a button its always one out. can somebody see any errors with this
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
publicclass adminupdateextends HttpServlet{
publicvoid doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String user;
Cookie[] cookies = request.getCookies();
Cookie cookie;
for(int i=0; i < 1; i++){
cookie = cookies[i];
cookie.getValue();
user = cookie.getValue();
System.out.println(" cookie vlaue is " + user);
// Database connection code starts here
Connection conn =null;
// loading jdbc driver for mysql (help in mysql.jar file in classpath)
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e){
System.out.println(e);
}
// connecting to database
try{
conn = DriverManager.getConnection
("jdbc:myurl");
// System.out.println("Connection to database successful.");
}
catch(SQLException se){
System.out.println(se);
}
// Create select statement and execute it
try{
// Build up the SQL statement from our data requirements
String selectSQL ="select user_name, recording_title, recording_id "+
"from video_rental" +
" order by user_name desc";
Statement stmt = conn.createStatement();
ResultSet rs1 = stmt.executeQuery(selectSQL);
System.out.println(selectSQL);
// output html headers
String title ="Check DVDS In";// this would just show recid need to show film name!
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#a00e0e\">\n" +
"<center><img src=\"http://localhost:8080/examples/LOGO.jpg\" width=350 height=200/></center>\n" +
"<H1 ALIGN=\"CENTER\">" + title +"</H1>\n" +
"These are all the DVDS which are currently lent out" +// need better message then that!!! need film title and priorty
"<P> When a dvd is returned simply match the sticker on the dvd to the code on this page
" +"<P> once you have checked the dvd is in the box
" +"<P> click the checked in button.
" +"<p align=\"right\"><a href=\"http://localhost:8080/examples/servlet/home">Home</a>
\n" +"<p align=\"right\"><a href=\"http://localhost:8080/examples/admin.html\">Admin Menu</a>
\n");// close the html
out.println("</BODY></HTML>");
out.println("<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
" <TH>USER NAME\n" +
" <TH>TITLE\n" +// put another row with button to remove
" <TH>RECORDING_ID\n" +// put another row with button to remove
" <TH> CHECK IN\n" );
// Retrieve the results
while(rs1.next()){
// getInt or getString or getFloat etc to get the appropriate column data
// wrap output in html for web
String formURL =
"http://localhost:8080/examples/servlet/.adminupdate2";// needs changing
// Pass URLs that reference own site through encodeURL.
formURL = response.encodeURL(formURL);
out.println(
"<TR>" +
"<TD>" + rs1.getString("user_name") +"</TD>" +
"<TD>" + rs1.getString("recording_title") +"</TD>" +
"<TD>" + rs1.getString("recording_id") +"</TD>" +
"<TD>" +"<P>\n<CENTER>\n" +
"<INPUT TYPE=\"SUBMIT\" " +
"VALUE=\"Checked in\">\n" +
"</CENTER>\n<P>\n</FORM>" +"</TD>\n");
out.println("<FORM ACTION=\"" + formURL +"\">\n" +// need servlet utilites here!!
"<INPUT TYPE=\"HIDDEN\" NAME=\"filmid\" " +
"VALUE=\"" + rs1.getString("recording_id") +"\">\n" +// was item.get rec does it matter?
"<INPUT TYPE=\"HIDDEN\" NAME=\"filmtitle\" " +
"VALUE=\"" + rs1.getString("recording_title") +"\">\n" +// was item.get rec does it matter?
"<INPUT TYPE=\"HIDDEN\" NAME=\"user\" " +
"VALUE=\"" + rs1.getString("user_name") +"\">\n");// was item.get rec does it matter?
}
// close the html
out.println("</TABLE></BODY></HTML>");
// Close the stament and database connection
//(must remember to always do this)
stmt.close();
conn.close();
}catch(SQLException se){
System.out.println(se);
}
}
}
}

