我有一个管理表,我试图从MySQL数据库中检索信息以供管理员编辑。当我运行应用程序时,表格显示但没有填充表格的信息。以下是我的admin.jsp页面。
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="../styles/style1.css">
<meta http-equiv="Content-Type" content="text/css; charset=ISO-8859-1">
<title>admin</title>
<script language="javascript">
function editRecord(id){
window.location.href="editServlet/"+id;
}
function deleteRecord(id){
window.location.href="deleteUser/"+id;
}
</script>
</head>
<body>
<div class="content">
<br>
<table align="center">
</table>
<br>
<table width="600px" align="center" style="background-color:#EDF6EA;border:1px solid #000000;">
<tr><td colspan=9 align="center" height="10px"></td></tr>
<tr><td colspan=9 align="center" height="10px"></td></tr>
<tr style="background-color:#7BA88B;font-weight:bold;">
<td>CourseID</td><td>Course Rate</td><td>Professor</td>
<td>Professor Rate</td><td>Review</td><td>Edit</td><td>Delete</td>
</tr>
<%
String bgcolor="";
int count=0;
List viewList = new ArrayList();
Iterator viewItr;
if(request.getAttribute("courseList")!=null && request.getAttribute("courseList")!="")
{
List courseList = (ArrayList)request.getAttribute("courseList");
Iterator itr = courseList.iterator();
while(itr.hasNext())
{
if(count%2==0)
{
bgcolor = "#C8E2D1";
}
else
{
bgcolor = "#EAF8EF";
}
viewList = (ArrayList)itr.next();
int id = Integer.parseInt(viewList.get(0).toString());
viewItr = viewList.iterator();
%>
<tr style="background-color:<%=bgcolor%>;">
<%
while(viewItr.hasNext())
{
%>
<td><%=viewItr.next()%></td>
<%
}
count++;
%>
<td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=id%>);" ></td>
<td><input type="button" name="delete" style="background-color:#ff0000;font-weight:bold;;color:#ffffff;" value="Delete" onclick="deleteRecord(<%=id%>);"></td>
</tr>
<%
}
}
if(count==0)
{
%>
<tr><td colspan="9" align="center"> </td></tr>
<tr><td colspan="9" align="center">No Record Avaliable</td></tr>
<%
}
%>
<tr><td colspan=9 align="center" height="2px"></td></tr>
</table>
</div>
</body>
</html>
这是我的一个servlet:
package com.admin;
import java.sql.*;
import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CourseServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String str[] = request.getRequestURI().toString().split("/");
int id = Integer.parseInt(str[3]);
Connection conn = null;
String DB_CONNECTION_URL = "jdbc:mysql://localhost:3306/mydatabase";
String DB_USERNAME = "";
String DB_PASSWORD = "";
// out.println(request.getRequestURI());
Statement st;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(DB_CONNECTION_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("Connected to the database");
ArrayList<Comparable> courseList=null;
String query = "select * from coursereview where id="+id;
System.out.println("query " + query);
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next())
{
courseList =new ArrayList<Comparable>();
courseList.add(rs.getInt(1));
courseList.add(rs.getString(2));
courseList.add(rs.getString(4));
courseList.add(rs.getString(5));
courseList.add(rs.getString(6));
courseList.add(rs.getString(7));
}
request.setAttribute("courseList",courseList);
String nextJSP = "/editUser.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的Servlet记录:
package com.admin;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletRecord extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = -7311509570234925786L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
System.out.println("MySQL Connect Example.");
Connection conn = null;
String DB_CONNECTION_URL = "jdbc:mysql://localhost:3306/mydatabase";
String DB_USERNAME = "";
String DB_PASSWORD = "";
Statement st;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(DB_CONNECTION_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("Connected to the database");
ArrayList<Comparable> al=null;
ArrayList<ArrayList<Comparable>> courseList =new ArrayList<ArrayList<Comparable>>();
String query = "select * from coursereview order by id";
System.out.println("query " + query);
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next())
{
al = new ArrayList<Comparable>();
al.add(rs.getInt(1));
al.add(rs.getString(2));
al.add(rs.getString(4));
al.add(rs.getString(5));
al.add(rs.getString(6));
al.add(rs.getString(7));
al.add(rs.getString(8));
System.out.println("al :: "+al);
courseList.add(al);
}
request.setAttribute("courseList",courseList);
String nextJSP = "/admin.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
任何建议都将不胜感激。