第80节:Java中的MVC设计模式
第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后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
简书博客: 达叔小生
结语
- 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
- 小礼物走一走 or 点赞
上一篇: 冬天容易患上哪些疾病 六大疾病不容忽视
下一篇: 开发万站群CMS系统期间的些许感悟