使用MVC完成学生插入功能
准备工作:
1.将Mysql数据库驱动jar包放入lib目录下
2.创建web servelt类
3.具体实现
概念理解
Servelt处理客户请求
Servelt中doGet请求用来处理get请求,doPost请求用来处理post请求。
传入requset代表一个请求 页面所有的数据都封装到了request对象中
常见的编码:知道UTF-8就行了
Web xml 与Servelt生命周期
Servelt在tomcat服务器关闭时销毁
具体实现及代码
工程项目
* M:模型层,数据库的增、删、修、查 和业务逻辑的实现
* V:视图层,显示数据、获取数据。
* C:控制层,获取视图层的数据,并且调用业务逻辑类
//主要负责加载Mysql数据库驱动,连接数据库
public final class DbUtil {
private DbUtil() {
}
// 数据库地址
private static String dbUrl = "jdbc:mysql://localhost:3306/db_book";
// 用户名
private static String dbUserName = "root";
private static String dbPassvord = "123456";
// 驱动名称
private static String jdbcName = "com.mysql.jdbc.Driver";
public static Connection getConnection() throws Exception {
Class.forName(jdbcName);
System.out.println("驱动加载成功......");
Connection con = DriverManager.getConnection(dbUrl, dbUserName,
dbPassvord);
System.out.println("数据库链接成功.....");
return con;
}
// 关闭链接
public static void colse(ResultSet rs, Statement stmt, Connection con) {
if (rs != null) {
try {
rs.close();
System.out.println("ResultSet接口关闭成功.....");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
System.out.println("Statement接口关闭成功....");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
System.out.println("Connenction接口关闭成功.....");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
// 关闭链接
public static void colse(Statement stmt, Connection con) throws Exception {
if (stmt != null) {
stmt.close();
if (con != null) {
con.close();
System.out.println("Connenction接口关闭成功.....");
}
}
}
}
//M:模型层,数据库的增、删、修、查 和业务逻辑的实现
public class StudentImpl {
//添加数据的方法
private static int addStudent(Student student) throws Exception{
Connection con = DbUtil.getConnection();
PreparedStatement pre =con.prepareStatement("insert into t_student values(null,?,?,?)");
pre.setString(1, student.getSname());
pre.setString(2, student.getSpass());
pre.setString(3, student.getSlikes());
System.out.println(pre);
int result = pre.executeUpdate();
DbUtil.colse(pre, con);
return result;
}
//向数据库添加数据
public void saveStudent(Student student)throws Exception{
//连接数据库完成数据库录入操作
System.out.println("---------------saveStudent-------------");
System.out.println(student.getSname());
System.out.println(student.getSname());
int reult = addStudent(student);
if(reult == 1){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
public String joinString(String[] slikes){
String slikes2 ="";
for(String temp:slikes){
slikes2 +=temp+"-";
}
slikes2 =slikes2.substring(0,slikes2.length()-1);
return slikes2;
}
}
//学生类
public class Student {
private String sname;
private String spass;
private String slikes;
public String getSlikes() {
return slikes;
}
public void setSlikes(String slikes) {
this.slikes = slikes;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSpass() {
return spass;
}
public void setSpass(String spass) {
this.spass = spass;
}
}
//C:控制层,获取视图层的数据,并且调用业务逻辑类
public class StudentSer extends HttpServlet {
public StudentSer() {
super();
System.out.println("----StudentSer() 构造方法-----");
}
public void destroy() {
super.destroy();
System.out.println("----StudentSer destroy-----");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); // post提交表单,规定字符编码为"UTF-8"
StudentImpl impl = new StudentImpl();
Student student = new Student();
student.setSname(request.getParameter("sname"));
student.setSpass(request.getParameter("spass"));
String slikes[] = (request.getParameterValues("slikes"));
student.setSlikes((impl.joinString(slikes)));
try {
impl.saveStudent(student);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void init() throws ServletException {
System.out.println("------init-----初始化方法,在构造方法后面调用-----");
}
}
<!-- V:视图层,显示数据、获取数据。 -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
<form action="/WebDemo/StudentSer" method="post">
<table>
<tr>
<td>姓名: <input type="text" name="sname" />
</td>
<tr>
<tr>
<td>密码: <input type="password" name="spass">
</td>
<tr>
<tr>
<td>爱好: 看书<input type="checkbox" name="slikes" value="看书">
看书<input type="checkbox" name="slikes" value="看书"> 上网<input
type="checkbox" name="slikes" value="上网">
<td>
<td>
<td>
</tr>
</table>
<input type="submit" value="提交">
</form>
</div>
</body>
</html>