首先我有三个类,DAO.java(存放对数据的操作方法),DBCity.java(提供City表中各列的get和set 方法),myCon.java(提供对数据库的连接),另外还有一个jsp页面,用来显示,还有一个servlet用来接收页面传来的参数
各类内容如下:
DAO.java
package mypro;
import java.sql.*;
import java.sql.Connection;
import java.util.ArrayList;
import java.io.*;
public class DAO {
public DAO() {
}
public static ArrayList getAllCity(String father) throws Exception{
Connection con =myCon.getCon();
String sql = "select * from city where father = "+father+"";
Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery(sql);
ArrayList list = new ArrayList();
while(rs.next()){
DBCity db = new DBCity();
db.setCityID(rs.getString("cityID"));
db.setCity(rs.getString("city"));
db.setFather(rs.getString("father"));
list.add(db);
}
rs.close();
stm.close();
con.close();
return list;
}
}
DBCity.java
package mypro;
public class DBCity {
private String cityID;
private String city;
private String father;
public DBCity() {
}
public void setCityID(String cityID) {
this.cityID = cityID;
}
public void setCity(String city) {
this.city = city;
}
public void setFather(String father) {
this.father = father;
}
public String getCityID() {
return cityID;
}
public String getCity() {
return city;
}
public String getFather() {
return father;
}
}
myCon.java
package mypro;
import java.sql.DriverManager;
import java.sql.Connection;
public class myCon {
public myCon() {
}
public static Connection getCon() throws Exception{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=52tsrjBBS","sa","");
System.out.println("连接数据库成功");
return con;
}
}
jsp1.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
<script language="javascript" type="">
function onClick_A(){
<%java.util.ArrayList list = (java.util.ArrayList)request.getAttribute("cityList");
System.out.println("得到传来的list的长度为:"+list.size()); ←-----程序中加这个就报错
%>
}
</script>
</head>
<body bgcolor="#ffffff">
<form action="">
<a href="servlet1?father=130000" onclick="onClick_A()">测试</a>
</form>
</body>
</html>
servlet1.java
package mypro;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String father = request.getParameter("father");
System.out.println("得到传来的father的值:"+father);
try {
ArrayList list = DAO.getAllCity(father);
if(list!=null){
System.out.println("list的长度为:" + list.size());
request.setAttribute("cityList", list);
RequestDispatcher dis = request.getRequestDispatcher("jsp1.jsp");
dis.forward(request, response);
}else{
ArrayList list1 = new ArrayList();
list1 = DAO.getAllCity("140000");
request.setAttribute("cityList",list1);
RequestDispatcher dis = request.getRequestDispatcher("jsp1.jsp");
dis.forward(request, response);
}
} catch (Exception ex) {
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
流程如:jsp页面提供一超连接,点击后传一参数到servlet1,通过DAO中的方法,得到一个ArrayList对象,在此ArrayList对象中封装的DBCity对象,再通过servlet中用request装这个list发给jsp页面,在jsp页面中取出DBCity及其各列的值