User.java的源代码:
package com.ncu.beans;
public class User {
private String userId;
private String userName;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
DataBaseCon.java源代码: package com.ncu.beans;
import java.sql.*;
public class DataBaseCon
{
private Connection con=null;
private String url=null;
private String user=null;
private String pwd=null;
public DataBaseCon()
{
try{
Class.forName("com.mysql.jdbc.Driver");
url="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=gb2312";
user="root";
pwd="123456";
}
catch(Exception e){}
}
public Connection getCon()
{
try
{
con = DriverManager.getConnection(url, user, pwd);
}catch(Exception e){}
return con;
}
public void close()
{
try
{
if(con!=null)
con.close();
}catch(Exception e){}
}
}
UserBean.java源代码:
package com.ncu.beans;
import java.sql.*;
import java.util.*;
public class UserBean{
private DataBaseCon dbcon=new DataBaseCon();
private Connection con=null;
public UserBean(){
this.con=dbcon.getCon();
}
public Collection getUsers() throws Exception
{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from user");
Collection ret=new ArrayList();
while(rs.next())
{
User temp=new User();
temp.setUserId(rs.getString("userId"));
temp.setUserName(rs.getString("userName"));
ret.add(temp);
}
return ret;
}
}
user.jsp源代码:
<%@ page language="java" pageEncoding="gb2312"% >
<%@page import="com.ncu.beans.UserBean"% >
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" % >
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" % >
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" % >
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" % >
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" % >
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html:html lang="true" >
<head >
<html:base / >
<title >user.jsp </title >
</head >
<body >
<jsp:useBean id="users" scope="session" class="com.ncu.beans.UserBean"/ >
<c:set var="users" value="${users.users}"/ >