任务图片
目录图片
运行link.jsp图片
点留言列表后效果图
数据库表
tab_admin
/*
MySQL Data Transfer
Source Host: localhost
Source Database: test
Target Host: localhost
Target Database: test
Date: 2019/9/14 17:59:27
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tab_admin
-- ----------------------------
DROP TABLE IF EXISTS `tab_admin`;
CREATE TABLE `tab_admin` (
`aid` int(11) NOT NULL AUTO_INCREMENT,
`aname` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`aid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `tab_admin` VALUES ('1', '小洋', '123');
INSERT INTO `tab_admin` VALUES ('2', '郑泽', '123');
INSERT INTO `tab_admin` VALUES ('3', '小慧', '123');
数据库表
tab_admin图片
tab_gestbook数据库表设计表图
程序执行过程如图
== 适合对象javaWeb初学者 ==
留言板案例
开发环境
java的jdk:jdk1.8.0_66
java的开发工具:eclipse(eclipse-photon)
数据库:MySQL Server 5.1
Web容器:apache-tomcat-8.5.32
jar包:c3p0-0.9.1.2.jar mysql-connector-java-5.1.6-bin.jar
配置文件
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--默认配置 -->
<default-config>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<!--配置连接池mysql -->
<named-config name="mysql">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</named-config>
</c3p0-config>
注意:将c3p0-config.xml放在src下面
- src下
com.zz.controller下
ListGestBookServlet.java
package com.zz.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zz.dao.AdminDao;
import com.zz.dao.GestbookDao;
import com.zz.pojo.Gestbook;
public class ListGestBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private GestbookDao gestBookDao;
public ListGestBookServlet() {
super();
this.gestBookDao = new GestbookDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
ArrayList<Gestbook> list = gestBookDao.getAllGestbook();
request.setAttribute("list", list);
// HashMap以 aid<==>aname 的形式组织所有的数据保存进来.
HashMap<Integer,String> map=new HashMap<Integer,String>();
for(Gestbook book:list) {
Integer aid=book.getAid();
String aname=AdminDao.sum(book.getAid());
map.put(aid, aname);
}
request.setAttribute("map", map);
this.getServletContext().getRequestDispatcher("/list.jsp").forward(request, response);
}
}
com\zz\dao
AdminDao.java
package com.zz.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.zz.pojo.Admin;
import com.zz.util.C3P0Utils;
public class AdminDao {
public Admin login(Admin admin) {
Admin result=null;
String sql="select aid,aname,password from tab_admin where aname=? and password=?";
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con = C3P0Utils.getConn();
ps = con.prepareStatement(sql);
ps.setString(1, admin.getAname());
ps.setString(2, admin.getPassword());
rs=ps.executeQuery();
if(rs.next()) {
result=new Admin();
result.setAid(rs.getInt("aid"));
result.setAname(rs.getString("aname"));
result.setPassword(rs.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
C3P0Utils.closeJDBC(con, ps, rs);
}
return result;
}
public static String sum(int aid) {
String sql="select aname from tab_admin where aid=?";
String result=null;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con = C3P0Utils.getConn();
ps = con.prepareStatement(sql);
ps.setInt(1, aid);
rs=ps.executeQuery();
if(rs.next()) {
result=rs.getString("aname");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
C3P0Utils.closeJDBC(con, ps, rs);
}
return result;
}
}
GestbookDao.java
package com.zz.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import com.zz.pojo.Gestbook;
import com.zz.util.C3P0Utils;
public class GestbookDao {
// 获取所有的数据列表
public ArrayList<Gestbook> getAllGestbook() {
ArrayList<Gestbook> list = new ArrayList<Gestbook>();
String sql = "select * from tab_gestbook";
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = C3P0Utils.getConn();
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Gestbook book = new Gestbook();
book.setGst_id(rs.getInt("gst_id"));
book.setAid(rs.getInt("aid"));
book.setGst_title(rs.getString("gst_title"));
book.setGst_content(rs.getString("gst_content"));
book.setGst_ip(rs.getString("gst_ip"));
Timestamp temp = rs.getTimestamp("gst_time");
book.setGst_time(temp);
list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
C3P0Utils.closeJDBC(con, ps, rs);
}
return list;
}
}
com\zz\pojo
Admin.java
package com.zz.pojo;
public class Admin {
private Integer aid;
private String aname;
private String password;
public Admin() {
super();
}
public Admin(String aname, String password) {
super();
this.aname = aname;
this.password = password;
}
public Admin(Integer aid, String aname, String password) {
super();
this.aid = aid;
this.aname = aname;
this.password = password;
}
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Gestbook.java
package com.zz.pojo;
import java.util.Date;
public class Gestbook {
private Integer gst_id;
private Integer aid;
private String gst_title;
private String gst_content;
private String gst_ip;
private Date gst_time;
public Gestbook() {
super();
}
public Gestbook(Integer gst_id, Integer aid, String gst_title, String gst_content, String gst_ip, Date gst_time) {
super();
this.gst_id = gst_id;
this.aid = aid;
this.gst_title = gst_title;
this.gst_content = gst_content;
this.gst_ip = gst_ip;
this.gst_time = gst_time;
}
public Integer getGst_id() {
return gst_id;
}
public void setGst_id(Integer gst_id) {
this.gst_id = gst_id;
}
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getGst_title() {
return gst_title;
}
public void setGst_title(String gst_title) {
this.gst_title = gst_title;
}
public String getGst_content() {
return gst_content;
}
public void setGst_content(String gst_content) {
this.gst_content = gst_content;
}
public String getGst_ip() {
return gst_ip;
}
public void setGst_ip(String gst_ip) {
this.gst_ip = gst_ip;
}
public Date getGst_time() {
return gst_time;
}
public void setGst_time(Date gst_time) {
this.gst_time = gst_time;
}
}
com\zz\util
C3P0Utils.java
package com.zz.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Utils {
static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");
public static Connection getConn() {
try {
Connection conn = dataSource.getConnection();
return conn;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void closeJDBC(Connection con,Statement st,ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st!=null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
if(con!=null && con.isClosed()==false) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
DateUtil.java
package com.zz.util;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static String getStringByDate(Date date) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return sdf.format(date);
}
}
demo下
MyFunctions.java
package demo;
import com.zz.dao.AdminDao;
public class MyFunctions {
public static String sum(int aid) {
String aname = AdminDao.sum(aid);
return aname;
}
}
WebContent下
list.jsp
<%@ page language="java"
import="java.util.*,com.zz.pojo.Gestbook,com.zz.util.DateUtil"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="myfn" uri="http://www.imti.com/myfunctions"%>
<%@page import="com.zz.pojo.Gestbook"%>
<%@page import="com.zz.controller.ListGestBookServlet"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<hr />
<c:forEach items="${requestScope.list}" var="t">
<c:choose>
<c:when test="${empty list}">
列表为空!
</c:when>
<c:otherwise>
用户名:${myfn:sum(t.aid) } ip地址:${t.gst_ip} 时间:${t.gst_time}<br/>
标题:${t.gst_title}<br/>
内容:${t.gst_content}
<hr />
<br />
</c:otherwise>
</c:choose>
</c:forEach>
</body>
</html>
jspPage
link.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言列表</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/ListGestBookServlet">留言列表</a>
</body>
</html>
WEB-INF\lib
c3p0-0.9.1.2.jar, jstl.jar, mysql-connector-java-5.1.6-bin.jar, standard.jar
WEB-INF
myfunction.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 myfunctions library</description>
<display-name>JSTL myfunctions</display-name>
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>http://www.imti.com/myfunctions</uri>
<function>
<name>sum</name>
<function-class>demo.MyFunctions</function-class>
<function-signature>java.lang.Object sum(int)</function-signature>
</function>
</taglib>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MassageLable</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ListGestBookServlet</display-name>
<servlet-name>ListGestBookServlet</servlet-name>
<servlet-class>com.zz.controller.ListGestBookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListGestBookServlet</servlet-name>
<url-pattern>/ListGestBookServlet</url-pattern>
</servlet-mapping>
</web-app>