第80节:Java中的MVC设计模式
了解java中的mvc模式.复习以及回顾!
事务,设置自动连接提交关闭.
setAutoCommit(false);
conn.commit();
conn.rollBack
隔离级别分别有:
读未提交有脏读 读已提交有不可重复读 可重复读有幻读 可串行化可以解决脏读,幻读,不可重复读
数据库连接池用于创建和管理连接对象.
DBCP和C3P0,分别了解代码设置和配置文件设置
DBUtils可以简化数据的增删改查.
QueryRunner runner = new QueryRunner();
runner.update();
runner.query();
DBUtils通用的增删改
public void testInsert(){
// 查询
Connection conn = null;
Statement st = null;
try{
// 获取连接对象
conn = JDBCUtil.getConn();
// 根据连接对象,得到state ment
st = conn.createStatement();
// 执行添加
String sql = "insert into t_stu values(null, 'dashu', 23)";
// 影响行数
int result = st.executeUpdate(sql);
if(result > 0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtil.release(conn, st);
}
通用的增删改方法
package com.dashucoding.commoncrud;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.junit.Test;
import com.dashucoding.util.JDBCUtil;
import com.dashucoding.util.JDBCUtil02;
public class CommonCRUDUtil {
@Test
public void testUpdate() {
// update("insert into account values(null, ?, ?)" , "dashu", 10);
// update("delete from account where id = ?", 1);
update("update account set money = ? where id = ?", 1999, 2);
}
// 通用的增删改功能
public void update(String sql, Object ...args) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtil02.getConn();
ps = conn.prepareStatement(sql);
for(int i = 0; i<args.length; i++) {
ps.setObject(i+1, args[i]);
}
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtil.release(conn, ps);
}
}
}
数据库的元数据
java.sql
接口 DatabaseMetaData
所有超级接口: Wrapper
public interface DatabaseMetaData extends Wrapper
数据库的整体综合信息
方法
getCatalogs()
可以获取在数据库中使用的类别名称
getCatalogSeparator()
获取此数据库用作类别和表名之间的分隔符的String
getCatalogTerm()
获取数据库供应商用于"catalog"的首选手语
getClientInfoProperties()
获取驱动程序支持的客户端信息属性的列表
getConnection()
获取此元数据对象所产生的连接
getDatabaseMajorVersion()
获取底层数据库的主版本号
getDatabaseProductName()
获取此数据库产品的名称
getDatabaseProductVersion()
获取此数据库产品的版本号
getDefaultTransactionIsolation()
获取此数据库的默认事务隔离级别
getDriverMajorVersion()
获取此JDBC驱动程序的主版本号
getDriverMinorVersion()
获取此JDBC驱动程序的次版本号
getDriverName()
获取此JDBC驱动程序的名称
getDriverVersion()
获取此JDBC驱动程序的String形式的版本号
参数的元数据
java.sql
接口 ParameterMetaData
public interface ParameterMetaData extends Wrapper
获取PreparedStatement对象中每个参数标记和属性信息的对象.
方法的摘要
getParameterClassName(int param)
获取Java类的完全限定名称
getParameterCount()
获取PreparedStatement对象中的参数数量
getParameterMode(int param)
获取指定参数的SQL类型
getParameterTypeName(int param)
获取指定参数的特定于数据库的类型名称
getPrecision(int param)
获取指定参数的指定两列大小
getScale(int param)
获取指定参数的小数点右边的位数
isNullable(int param)
获取是否允许在指定参数中使用null值
isSigned(int param)
获取指定参数的值是否可以是带符号的数字
结果集元数据
java.sql
接口 ResultSetMetaData
public interface ResultSetMetaData extends Wrapper
用来描述数据的数据,叫做元数据
数据库元数据,参数元数据,结果集元数据
package com.dashucoding.commoncrud;
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.junit.Test;
import com.dashucoding.util.JDBCUtil;
import com.dashucoding.util.JDBCUtil02;
public class CommonCRUDUtil {
@Test
public void testUpdate() {
// update("insert into account values(null, ?, ?)" , "dashu", 10);
// update("delete from account where id = ?", 1);
update("update account set money = ? where id = ?", 1999, 2);
}
// 通用的增删改功能
public void update(String sql, Object ...args) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtil02.getConn();
ps = conn.prepareStatement(sql);
// 元数据
// 获取的有几个问好
ParameterMetaData metaData = ps.getParameterMetaData();
int count = metaData.getParameterCount();
for(int i = 0; i<count; i++) {
ps.setObject(i+1, args[i]);
}
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtil.release(conn, ps);
}
}
}
TestDBUtils.java
// 删除
queryRunner.update("delete from account where id = ?", 4);
// 更新
queryRunner.update("update account set money=? where id=?", 10000, 5);
// 执行查询,查询到的数据还是在那个result里面然后调用下面的handle方法,由用户手动封装
Account account = queryRunner.query("select * from account where id =?", new ResultSetHandler<Account>(){
@Override
public Account handle(ResultSet rs) throws SQLException{
Account account = new Account();
while(rs.next()){
String name = rs.getString("name");
int money = rs.getInt("money");
account.setName(name);
account.setMoney(money);
}
return account;
}
}, 6);
快速查询方法
@Test
public void testQuery(){
query("select * from account where id = ?", 接口的实现类对象, 3);
}
class A implements ResultSetHandler {
@Override
public void handle(ResultSet rs) {
// TODO Auto-generated method stub
}
}
通用的查询方法
package com.dashucoding.commoncrud;
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.dashucoding.domain.Account;
import com.dashucoding.util.JDBCUtil;
import com.dashucoding.util.JDBCUtil02;
public class CommonCRUDUtil {
class A implements ResultSetHandler<Account> {
@Override
public Account handle(ResultSet rs) {
// TODO Auto-generated method stub
try {
Account account = new Account();
if(rs.next()) {
String name = rs.getString("name");
int money = rs.getInt("money");
account.setName(name);
account.setMoney(money);
}
return account;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/*@Override
public Object handle(ResultSet rs) {
// TODO Auto-generated method stub
return null;
}*/
/*@Override
public void handle(ResultSet rs) {
// TODO Auto-generated method stub
try {
while(rs.next()) {
String name = rs.getString("name");
int money = rs.getInt("money");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}
@Test
public void testQuery() {
/*query("select * from account where id=?", new ResultSetHandler() {
@Override
public void handle(ResultSet rs) {
// TODO Auto-generated method stub
}
},3);*/
/*Account account = query("select * from account where id = ?", new A(), 2);
System.out.println(account.toString());*/
query("select * from account where id = ?", new ResultSetHandler<Account>() {
@Override
public Account handle(ResultSet rs) {
// TODO Auto-generated method stub
return null;
}}, 2);
}
@Test
public void testUpdate() {
// update("insert into account values(null, ?, ?)" , "dashu", 10);
// update("delete from account where id = ?", 1);
// update("update account set money = ? where id = ?", 1999, 2);
}
public <T> T query(String sql, ResultSetHandler<T> handler, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtil02.getConn();
ps = conn.prepareStatement(sql);
// 元数据
// 获取的有几个问好
ParameterMetaData metaData = ps.getParameterMetaData();
int count = metaData.getParameterCount();
for (int i = 0; i < count; i++) {
ps.setObject(i + 1, args[i]);
}
// 执行查询工作,然后得到结果集
ResultSet rs = ps.executeQuery();
// 把结果集丢给调用者, 让它去封装数据,返回封装数据
T t = (T) handler.handle(rs);
return t;
/*while(rs.next()) {
rs.getInt("id");
rs.getString("name");
}*/
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtil.release(conn, ps);
}
return null;
}
// 通用的增删改功能
/*public void update(String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtil02.getConn();
ps = conn.prepareStatement(sql);
// 元数据
// 获取的有几个问好
ParameterMetaData metaData = ps.getParameterMetaData();
int count = metaData.getParameterCount();
for (int i = 0; i < count; i++) {
ps.setObject(i + 1, args[i]);
}
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtil.release(conn, ps);
}
}*/
}
package com.dashucoding.domain;
public class Account {
private String name;
private int money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Account [name=" + name + ", money=" + money + "]";
}
}
package com.dashucoding.commoncrud;
import java.sql.ResultSet;
public interface ResultSetHandler<T> {
// 数据封装的规则,规范
T handle(ResultSet rs);
}
JSP开发模式
mvc的设计模式
javaBean数据的封装+jsp
可以在jsp中直接写java代码
<%
封装数据
执行业务
准备数据
%>
特点维护比较难,jsp的页面代码会臃肿
servlet + javabean + jsp
mvc模式:
m:model
模型层
封装数据javabean
v:view
视图层
jsp专注显示
c:controller
控制层
servlet接收页面的请求,找模型层去处理
三层架构
客户端,web层,业务逻辑层,数据访问层
servlet/jsp web层 javabean 业务逻辑层 dao 数据访问层
web层 对应 controller view
业务逻辑层 对应 model
数据访问层 对应 model
mvc模式: controller view model
controller: 接收请求,调用模型层出来数据,反馈给view
view: 用于显示
model: 数据的封装,数据的处理
学生管理系统中的增删查改,分页,模糊查询
欢迎使用学生管理系统, 按姓名查询, 按性别查询, 添加 有编号,姓名 ,性别,电话,生日,爱好,简介,操作.
创建数据库
数据库
ctrl+t
dao环境搭建
结构
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h3><a href="StudentListServlet">显示所有学生列表</a></h3><br>
</body>
</html>
package com.dashucoding.dao;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.domain.Student;
/*
* 这是针对学生表的数据访问
*
* */
public interface StudentDao {
/*
* 查询所有学生
* list<Student>
* */
List<Student> findAll() throws SQLException;
}
package com.dashucoding.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.domain.Student;
import com.dashucoding.util.JDBCUtil02;
/*
* 这是studentdao的实现,针对前面定义的规范,做出具体的实现
* */
public class StudentDaoImpl implements StudentDao {
/*
* 查询所有学生
* */
@Override
public List<Student> findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
String sql = "select * from stu";
List<Student> list = runner.query(sql, new BeanListHandler<Student>(Student.class));
return list;
}
}
package com.dashucoding.domain;
import java.util.Date;
/*
* 这是学生封装的对象bean
* 根据表写
* */
public class Student {
private int sid;
private String sname;
private String gender;
private String phone;
private String hobby;
private String info;
private Date birthday;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.dashucoding.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtil02 {
static ComboPooledDataSource dataSource = null;
static {
dataSource = new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return dataSource;
}
/**
* 获取连接对象
* @return
* @throws SQLException
*/
public static Connection getConn() throws SQLException{
return dataSource.getConnection();
}
/**
* 释放资源
* @param conn
* @param st
* @param rs
*/
public static void release(Connection conn , Statement st , ResultSet rs){
closeRs(rs);
closeSt(st);
closeConn(conn);
}
public static void release(Connection conn , Statement st){
closeSt(st);
closeConn(conn);
}
private static void closeRs(ResultSet rs){
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}
}
private static void closeSt(Statement st){
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null;
}
}
private static void closeConn(Connection conn){
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
service层
显示
显示
dao只做一件事,数据操作层
service是业务层
查询数据
结构
package com.dashucoding.dao;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.domain.Student;
/*
* 这是针对学生表的数据访问
*
* */
public interface StudentDao {
/*
* 查询所有学生
* list<Student>
* */
List<Student> findAll() throws SQLException;
}
package com.dashucoding.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.domain.Student;
import com.dashucoding.util.JDBCUtil02;
/*
*这是StudentDao的实现,针对前面定义的规范,做出具体的实现
* */
public class StudentDaoImpl implements StudentDao {
/*
* 查询所有学生
* */
@Override
public List<Student> findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
return runner.query("select * from stu", new BeanListHandler<Student>(Student.class));
}
}
package com.dashucoding.domain;
import java.util.Date;
/*
* 这是学生封装的对象bean
*
* */
public class Student {
private int sid;
private String sname;
private String gender;
private String phone;
private String hobby;
private String info;
private Date birthday;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int sid, String sname, String gender, String phone, String hobby, String info, Date birthday) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.phone = phone;
this.hobby = hobby;
this.info = info;
this.birthday = birthday;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", phone=" + phone + ", hobby="
+ hobby + ", info=" + info + ", birthday=" + birthday + "]";
}
}
package com.dashucoding.service;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.domain.Student;
/*
* 这是学生的业务处理规范
* */
public interface StudentService {
/*
* 查询所有学生
* list<Student>
* */
List<Student> findAll() throws SQLException;
}
package com.dashucoding.service.impl;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.dao.impl.StudentDaoImpl;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
/*
* 这是学生业务实现
* */
public class StudentServiceImpl implements StudentService{
@Override
public List<Student> findAll() throws SQLException {
StudentDao dao = new StudentDaoImpl();
return dao.findAll();
}
}
package com.dashucoding.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.dao.impl.StudentDaoImpl;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
public class StudentListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 查询所有的学生
StudentService service = new StudentServiceImpl();
List<Student> list = service.findAll();
// 把数据存储到作用域中
request.setAttribute("list", list);
// 跳转页面
request.getRequestDispatcher("list.jsp").forward(request,response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.dashucoding.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtil02 {
static ComboPooledDataSource dataSource = null;
static {
dataSource = new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return dataSource;
}
/**
* 获取连接对象
* @return
* @throws SQLException
*/
public static Connection getConn() throws SQLException{
return dataSource.getConnection();
}
/**
* 释放资源
* @param conn
* @param st
* @param rs
*/
public static void release(Connection conn , Statement st , ResultSet rs){
closeRs(rs);
closeSt(st);
closeConn(conn);
}
public static void release(Connection conn , Statement st){
closeSt(st);
closeConn(conn);
}
private static void closeRs(ResultSet rs){
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;
}
}
private static void closeSt(Statement st){
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null;
}
}
private static void closeConn(Connection conn){
try {
if(conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
先写一个jsp页面,有个链接<a href="StudentListServlet"></a>
写个Servlet,接收请求,去调用Service,由service去调用dao,写dao,然后做dao实现,再写Service,做Service的实现,在servlet存储数据,做出页面响应,在list.jsp上显示数据.
dao
添加效果
添加按钮
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生页面</title>
</head>
<body>
<form method="post" action="AddServlet">
<table border="1" width="600">
<tr>
<td>姓名</td>
<td><input type="text" name="sname"></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="gender" value="男">男 <input
type="radio" name="gender" value="女">女</td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>生日</td>
<td><input type="text" name="birthday"></td>
</tr>
<tr>
<td>爱好</td>
<td><input type="checkbox" name="hobby" value="游泳">游泳 <input
type="checkbox" name="hobby" value="篮球">篮球 <input
type="checkbox" name="hobby" value="足球">足球 <input
type="checkbox" name="hobby" value="看书">看书 <input
type="checkbox" name="hobby" value="写字">写字</td>
</tr>
<tr>
<td>简介</td>
<td><textarea name="info" rows="3" cols="20"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加"></td>
</tr>
</table>
</form>
</body>
</html>
package com.dashucoding.dao;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.domain.Student;
/*
* 这是针对学生表的数据访问
*
* */
public interface StudentDao {
/*
* 查询所有学生
* list<Student>
* */
List<Student> findAll() throws SQLException;
void insert(Student student) throws SQLException ;
}
package com.dashucoding.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.domain.Student;
import com.dashucoding.util.JDBCUtil02;
/*
*这是StudentDao的实现,针对前面定义的规范,做出具体的实现
* */
public class StudentDaoImpl implements StudentDao {
/*
* 查询所有学生
*/
@Override
public List<Student> findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
return runner.query("select * from stu", new BeanListHandler<Student>(Student.class));
}
@Override
public void insert(Student student) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
runner.update("insert into stu values(null, ?,?,?,?,?,?)",
student.getSname(),
student.getGender(),
student.getPhone(),
student.getBirthday(),
student.getHobby(),
student.getInfo()
);
}
}
package com.dashucoding.domain;
import java.util.Date;
/*
* 这是学生封装的对象bean
*
* */
public class Student {
private int sid;
private String sname;
private String gender;
private String phone;
private String hobby;
private String info;
private Date birthday;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int sid, String sname, String gender, String phone, String hobby, String info, Date birthday) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.phone = phone;
this.hobby = hobby;
this.info = info;
this.birthday = birthday;
}
public Student(String sname, String gender, String phone, String hobby, String info, Date birthday) {
super();
this.sname = sname;
this.gender = gender;
this.phone = phone;
this.hobby = hobby;
this.info = info;
this.birthday = birthday;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", phone=" + phone + ", hobby="
+ hobby + ", info=" + info + ", birthday=" + birthday + "]";
}
}
package com.dashucoding.service;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.domain.Student;
/*
* 这是学生的业务处理规范
* */
public interface StudentService {
/*
* 查询所有学生
* list<Student>
* */
List<Student> findAll() throws SQLException;
void insert(Student student) throws SQLException ;
}
package com.dashucoding.service.impl;
import java.sql.SQLException;
import java.util.List;
import com.dashucoding.dao.StudentDao;
import com.dashucoding.dao.impl.StudentDaoImpl;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
/*
* 这是学生业务实现
* */
public class StudentServiceImpl implements StudentService{
@Override
public List<Student> findAll() throws SQLException {
StudentDao dao = new StudentDaoImpl();
return dao.findAll();
}
@Override
public void insert(Student student) throws SQLException {
// TODO Auto-generated method stub
StudentDao dao = new StudentDaoImpl();
dao.insert(student);
}
}
效果
效果
package com.dashucoding.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dashucoding.domain.Student;
import com.dashucoding.service.StudentService;
import com.dashucoding.service.impl.StudentServiceImpl;
/**
* 用于处理学生的添加请求
*/
public class AddServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
// 1. 获取客户端提交上来的数据
String sname = request.getParameter("sname");
String gender = request.getParameter("gender");
String phone = request.getParameter("phone");
String birthday = request.getParameter("birthday");
String info = request.getParameter("info");
// String hobby = request.getParameter("hobby");//hobby : 游泳,写字, 足球。
String[] h = request.getParameterValues("hobby");
String hobby = Arrays.toString(h);
hobby = hobby.substring(1, hobby.length() - 1);
// 2. 添加到数据库
// string -- date
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
Student student = new Student(sname, gender, phone, hobby, info, date);
StudentService service = new StudentServiceImpl();
service.insert(student);
// 3. 跳转到列表页
request.getRequestDispatcher("StudentListServlet").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
要的jar包,都在这.
群里
好了,欢迎在留言区留言,与大家分享你的经验和心得。
感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。
达叔小生:往后余生,唯独有你 You and me, we are family ! 90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通 简书博客: 达叔小生 https://www.jianshu.com/u/c785ece603d1