MVC模式实现数据库的增删改查
什么是MVC模式
MVC模式中M代表模型、V代表视图、C代表控制器。
Model(模型)表⽰应⽤程序核⼼(⽐如数据库记录列表)。
View(视图)显⽰数据(数据库记录)。
Controller(控制器)处理输⼊(写⼊数据库记录)。
数据库设计
表名:student、使⽤的为mysql数据库
字段名类型备注
sno varchar(10)学号、主键
sname varchar(10)姓名
sage int年龄M层
package cn.jdbc.domain;
public class student {
//学号
private String sno;
//姓名
private String sname;
//年龄
private int sage;
public String getSno(){
return sno;
}
public void setSno(String sno){
this.sno = sno;
}
public String getSname(){
return sname;
}
public void setSname(String sname){
this.sname = sname;
}
public int getSage(){
return sage;
}
public void setSage(int sage){
this.sage = sage;
}
}
V层
jsp页⾯
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="java.sun/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学⽣数据</title>
</head>
<body>
<table border="1" border="1" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<c:forEach  items="${list}"var="i">
<tr >
<td>${i.sno}</td>
<td>${i.sname}</td>
servlet负责jsp页⾯与数据库的数据交互
package cn.jdbc.servlet;
import java.io.IOException;
import java.util.ArrayList;
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 cn.jdbc.dao.StudentDao;
import cn.jdbc.domain.student;
import st.search_id;
/**
* Servlet implementation class display
*/
@WebServlet("/display")
public class display extends HttpServlet {
private static final long serialVersionUID =1L;
/**
* @see HttpServlet#HttpServlet()
*/
public display(){
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // TODO Auto-generated method stub
//获取数据库操作的对象
StudentDao dao =new StudentDao();
//获取数组对象
List<student> list =new ArrayList<student>();
//获取查询的学号
String search_sno = Parameter("search_no");
//获取添加的数据
String add_sno = Parameter("add_sno");
String add_sname = Parameter("add_sname");
String add_sage = Parameter("add_sage");
//获取修改的数据
String cg_sno = Parameter("cg_sno");
String cg_sname = Parameter("cg_sname");
String cg_sage = Parameter("cg_sage");
//获取删除的学号
String del_sno = Parameter("del_sno");
//查询数据
if(search_sno!=null){
student student= dao.find(search_sno);
list.add(student);
search_sno="";
}else{//显⽰所有的数据
list=dao.findAll();
list=dao.findAll();
}
//添加数据
if(add_sage!=null){
student student =new student();
student.setSno(add_sno);
student.setSname(add_sname);
student.setSage(Integer.parseInt(add_sage));
dao.insert(student);
list=dao.findAll();
add_sno="";
add_sname="";
add_sage="";
}
//修改数据
if(cg_sage!=null){
student student =new student();
student.setSno(cg_sno);
student.setSname(cg_sname);
student.setSage(Integer.parseInt(cg_sage));
dao.update(student);
list=dao.findAll();
cg_sno="";
cg_sname="";
cg_sage="";
}
//删除数据
if(del_sno!=null){
dao.delete(del_sno);
list=dao.findAll();
del_sno="";
}
//传递数组到jsp页⾯
request.setAttribute("list", list);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // TODO Auto-generated method stub
doGet(request, response);
基本的增删改查语句}
}
C层
⼯具类(负责连接数据库)
package cn.jdbc.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class utils {
// 加载驱动,并建⽴数据库连接
public static Connection getConnection()throws SQLException,
ClassNotFoundException {
//  加载驱动
Class.forName("sql.jdbc.Driver");
//  获取数据库连接
String url ="jdbc:mysql://localhost:3306/kinjaze";
//  ⽤户名
String username ="root";
//  密码
String password ="********";
Connection conn = Connection(url, username,
password);
return conn;
}
// 关闭数据库连接,释放资源
public static void release(ResultSet rs, Statement stmt, Connection conn){ if(rs != null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
rs = null;
}
if(stmt != null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
conn = null;
}
}
}
实现数据库的增删改查的操作类
package cn.jdbc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
SimpleDateFormat;
import java.util.ArrayList;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。