mysql+DButiles+JSTL
新建数据库和表
问题:记得编码字符集的问题,让mysql支持中文。
create database web_test2_student;
use web_test2_student;
create table student(
sid int primary key auto_increment,
sname varchar(20),
sex varchar(10),
age int
);
insert into student values(null,"小王","男",20),
(null,"小红","女",18),(null,"小明","男",15),(null,"张三","男",25),(null,"李四","男",28);
安装jar包
java代码目录
StudentServlet.java
package com.item.controller;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.item.domain.Student;
import com.item.model.StudentModel;
/**
* Servlet implementation class StudentServlet
*/
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
*
*/
public StudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
*
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.getWriter().write("1231231");
//return ;
//1.调用java类处理数据
StudentModel studentmodel=new StudentModel();
try {
List<Student>list=studentmodel.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("jsp/list.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.显示到jsp页面
}
/**
*
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Student.java
package com.item.domain;
public class Student {
private Integer sid;
private String sname;
private String sex;
private Integer age;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
StudentModel.java
package com.item.model;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.item.domain.Student;
import com.item.utiles.JDBCutiles;
public class StudentModel {
public List<Student> findAll() throws SQLException {
QueryRunner queryRunner=new QueryRunner(JDBCutiles.getDataSource());
List<Student>list=queryRunner.query("select * from student",new BeanListHandler<Student>(Student.class));
return list;
}
// public static void main(String[] args) throws SQLException {
// QueryRunner queryRunner=new QueryRunner(JDBCutiles.getDataSource());
// String sql="insert into student values(null,'刘园','男',12)";
// int cnt=queryRunner.update(sql);
// if(cnt>0) {
// System.out.println("成功");
// }else {
// System.out.println("失败");
// }
// List<Student>list=queryRunner.query("select * from student",new BeanListHandler<Student>(Student.class));
// if(list==null) {
// System.out.println("空");
// }
// for( Student stu:list) {
// System.out.println(stu.getSid()+" "+stu.getSname()+" "+stu.getSex()+" "+stu.getAge());
// }
// }
}
JDBCutiles.java
package com.item.utiles;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCutiles {
public static final ComboPooledDataSource datasource=new ComboPooledDataSource();
public static DataSource getDataSource() {
return datasource;
}
}
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3308/web_test2_student</property>
<property name="user">root</property>
<property name="password">3308</property>
<property name="initialPoolSize">5</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
</c3p0-config>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>学生信息页面</h1>
<table border="2" width="600px">
<tr>
<td>学生编号</td>
<td>学生姓名</td>
<td>学生性别</td>
<td>学生年龄</td>
</tr>
<c:forEach var="student" items="${ list}">
<tr>
<td>${student.sid}</td>
<td>${student.sname}</td>
<td>${student.sex}</td>
<td>${student.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>