第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: 数据的封装,数据的处理
学生管理系统中的增删查改,分页,模糊查询
学生管理系统
欢迎使用学生管理系统, 按姓名查询, 按性别查询, 添加
有编号,姓名 ,性别,电话,生日,爱好,简介,操作.
创建数据库
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上显示数据.
<%@ 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
结语
- 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
- 小礼物走一走 or 点赞
第80节:Java中的MVC设计模式的更多相关文章
-
第76节:Java中的基础知识
第76节:Java中的基础知识 设置环境,安装操作系统,安装备份,就是镜像,jdk配置环境,eclipse下载解压即可使用,下载tomcat 折佣动态代理解决网站的字符集编码问题 使用request. ...
-
第68节:Java中的MYSQL运用从小白到大牛
第68节:Java中的MYSQL运用从小白到大牛 前言 学习java必备要求,学会运用!!! 常见关系化数据库 BootStrap是轻量级开发响应式页面的框架,全局css组件,js插件.栅格系统是将页 ...
-
Java基础-Java中23种设计模式之常用的设计模式
Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...
-
javacript中的mvc设计模式
以下内容为原创翻译,翻译不对的地方还请原谅,凑合着看吧. 原文网址是: 来源:http://www.alexatnet.com/articles/model-view-controller-mvc-j ...
-
第83节:Java中的学生管理系统分页功能
第83节:Java中的学生管理系统分页功能 分页功能一般可以做成两种,一种是物理分页,另一种是逻辑分页.这两种功能是有各自的特点的,物理分页是查询的时候,对数据库进行访问,只是查一页数据就进行返回,其 ...
-
第82节:Java中的学生管理系统
第82节:Java中的学生管理系统 学生管理系统的删除功能 删除,点击超链接,点击弹出对话框式是否进行删除,如果确定,就删除,超链接执行的是js方法,在js里访问,跳转servlet,,servlet ...
-
第79节:Java中一些要点
第79节:Java中一些要点 前言 一些知识点忘了没,可以通过一个点引出什么内容呢?做出自己的思维导图,看看自己到了哪一步了呢 内容 如果有人问jre,jdk,jvm是什么,你怎么回答呢? jre的英 ...
-
第78节:Java中的网络编程(上)
第78节:Java中的网络编程(上) 前言 网络编程涉及ip,端口,协议,tcp和udp的了解,和对socket通信的网络细节. 网络编程 OSI开放系统互连 网络编程指IO加网络 TCP/IP模型: ...
-
第77节:Java中的事务和数据库连接池和DBUtiles
第77节:Java中的事务和数据库连接池和DBUtiles 前言 看哭你,字数:8803,承蒙关照,谢谢朋友点赞! 事务 Transaction事务,什么是事务,事务是包含一组操作,这组操作里面包含许 ...
随机推荐
-
#ifndef
关于c的#ifndef条件编译: 1)最好把头文件的内容都放在#ifndef和#endif中 2)一般格式: #ifndef <标识> #define <标识> ...... ...
-
索引超出了数组界限(Microsoft.SqlServer.Smo)
SSMS连接远程SQL Server服务器是很方便的. 昨天我用SQL Server 2008 SSMS连接SQL Server 2012竟然报错,如下图: 在网上搜了,参考这个参考那个,太啰嗦了,确 ...
-
ssh简化后之事务管理
为了能让大家更好的了解,所以今天跟大家分享整个项目.ps:ssh环境的搭建我就不一一讲解了,请大家参考 http://www.cnblogs.com/zczc1996/p/5842367.html. ...
-
79. Word Search
使用的别人的思路,用一个二维数组记录每一个位置是否用过,然后通过递归来判断每一个位置是否符合 public class Solution { public boolean exist(char[][] ...
-
EditText html 出现提示 This text field does not specify an inputType or a hint
1 <EditText 2 android:layout_width="fill_parent" 3 android:layout_height="wrap_c ...
-
记一次zookeeper集群搭建错误的排除
zookeeper官网上的文档说得很清楚. http://zookeeper.apache.org/doc/r3.5.1-alpha/zookeeperAdmin.html#sc_designing ...
-
poj 3304线段与直线相交
http://poj.org/problem?id=3304 Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
-
RFC2889转发性能測试用例设计和自己主动化脚本实现
一.203_TC_FrameRate-1.tcl set chassisAddr 10.132.238.190 set islot 1 set portList {9 10} ;#端口的排列顺序是po ...
-
iOS 判断数组不为空
if (array != nil && ![array isKindOfClass:[NSNull class]] && array.count != 0)
-
python笔记之time模块
方法一:time.perf_counter() 说明:返回计时器的精准时间(系统的运行时间),包含整个系统的睡眠时间.由于返回值的基准点是未定义的,所以,只有连续调用的结果之间的差才是有效的. 代码示 ...