Model1模式的学生信息增删改查
Student.java
package entity;
public class Student {
private int stuid;
private String stuname;
private String gender;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Student()
{
}
public Student(int stuid, String stuname, String gender) {
super();
stuid = this.stuid;
stuname = this.stuname;
gender = this.gender;
}
}
Model.java
package model; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import entity.Student; import util.DBUtil; public class Model {
private Statement sta;
private ResultSet rs;
PreparedStatement ps;
DBUtil u=new DBUtil(); public int Insert(int stuid,String stuname,String gender) throws SQLException{
Connection conn=u.getCon();
String sql="insert student values(?,?,?)";
ps=conn.prepareStatement(sql);
ps.setInt(1,stuid);
ps.setString(2,stuname);
ps.setString(3,gender);
int a=ps.executeUpdate();
return a;
} public int delete(int stuid) throws SQLException{
Connection conn=u.getCon();
String sql="delete from student where stuid=?";
ps=conn.prepareStatement(sql);
ps.setInt(1,stuid);
int a=ps.executeUpdate();
return a;
} public int update(int stuid,String stuname,String gender) throws SQLException{
Connection conn=u.getCon();
String sql="update student set stuname=?,gender=? where stuid=?";
ps=conn.prepareStatement(sql);
ps.setInt(3,stuid);
ps.setString(1,stuname);
ps.setString(2,gender);
int a=ps.executeUpdate();
return a;
}
public List<Student> queryAll() throws SQLException{
List<Student> students=new ArrayList<Student>();
Connection conn=u.getCon();
String sql="select * from student";
sta=conn.createStatement();
rs=sta.executeQuery(sql);
while(rs.next()){
Student student=new Student();
student.setStuid(rs.getInt("stuid"));
student.setStuname(rs.getString("stuname"));
student.setGender(rs.getString("gender"));
students.add(student);
}
return students;
} public Student queryById(int stuid) throws SQLException{
Student student=new Student();
Connection conn=u.getCon();
String sql="select * from student where stuid=?";
ps=conn.prepareStatement(sql);
ps.setInt(1,stuid);
rs=ps.executeQuery();
if(rs.next()){
student.setStuid(rs.getInt("stuid"));
student.setStuname(rs.getString("stuname"));
student.setGender(rs.getString("gender"));
}
return student; } }
EncodingFilter.java
package servlet; import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class EncodingFilter implements Filter { private String encoding = null;
public void init(FilterConfig config) throws ServletException {
this.encoding = config.getInitParameter("encoding");
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(this.encoding);
response.setCharacterEncoding(encoding);
chain.doFilter(request, response);
} public void destroy() {
this.encoding = null;
} }
DBUtil.java
package util; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; /**
* @author sawyer 2014下午1:20:16
*
*/ public class DBUtil {
private Connection conn = null;
private PreparedStatement stmt = null;
private ResultSet rs = null;
private static String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/userdb";
private String user = "root";
private String password = "orcl"; /**
* Get the driver
*/
static { } /**
* Connect the database
*/
public Connection getCon() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = (Connection) DriverManager
.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} /**
* @param sql
* @param obj
* Update
*//*
public int update(String sql, Object... obj) {
int count = 0;
conn = getCon();
try {
stmt = conn.prepareStatement(sql);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
stmt.setObject(i + 1, obj[i]);
}
}
count = stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return count;
} *//**
* @param sql
* @param obj
* Query
*//*
public ResultSet Query(String sql, Object... obj) {
conn = getCon();
try {
stmt = conn.prepareStatement(sql);
while (obj != null) {
for (int i = 0; i < obj.length; i++) {
stmt.setObject(i + 1, obj[i]);
}
}
rs = stmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return rs;
}*/ /**
* CLose the resource
*/
public void close() {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>servlet.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*,entity.*"%>
<%
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>
<h1><a href="insert.jsp">插入数据</a></h1>
<hr>
<h1><a href="delete.jsp">删除数据</a></h1>
<hr>
<h1><a href="update.jsp">更新数据</a></h1>
<hr>
<h1><a href="queryAll.jsp">查询所有</a></h1>
<hr>
<h1><a href="queryById.jsp">查询单个</a></h1>
</body>
</html>
delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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 'deletes.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>
<h1>删除数据</h1>
<form action="deleteShow.jsp" method="post">
<table>
<tr>
<td>请输入你要删除数据的ID号码:</td>
<td><input type="text" name="stuid" id="stuid">
</td>
<td><input type="submit" value="提 交" id="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
deleteShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*"%>
<%
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 'delete.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>
<h1>删除成功</h1>
<%
int stuid=Integer.parseInt(request.getParameter("stuid"));
Model model=new Model();
model.delete(stuid);
%>
</body>
</html>
insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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 'in.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>
<h1>插入数据</h1>
<form action="insertShow.jsp" method="post">
<table>
<tr>
<td>请插入数据:</td>
<td>学号:<input type="text" name="stuid" id="stuid">
</td>
<td>姓名:<input type="text" name="stuname" id="stuname">
</td>
<td>性别:<input type="text" name="gender" id="gender">
</td>
<td><input type="submit" value="插入" id="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
insertShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*"%>
<%
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 'insert.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">
<!--
<span style="white-space:pre"> </span><link rel="stylesheet" type="text/css" href="styles.css">
<span style="white-space:pre"> </span>--> </head> <body>
<span style="white-space:pre"> </span><h1>插入数据成功</h1>
<span style="white-space:pre"> </span><%
<span style="white-space:pre"> </span>Model model = new Model();
<span style="white-space:pre"> </span>int stuid = Integer.parseInt(request.getParameter("stuid"));
<span style="white-space:pre"> </span>String stuname = request.getParameter("stuname");
<span style="white-space:pre"> </span>String gender = request.getParameter("gender");
<span style="white-space:pre"> </span>model.Insert(stuid, stuname, gender);
<span style="white-space:pre"> </span>%>
</body>
</html>
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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 'update.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>
<h1>更新数据</h1>
<form action="updateShow.jsp" method="post">
<table>
<tr>
<td>请修改数据:</td>
<td>学号:<input type="text" name="stuid" id="stuid">
</td>
<td>姓名:<input type="text" name="stuname" id="stuname">
</td>
<td>性别:<input type="text" name="gender" id="gender">
</td>
<td><input type="submit" value="修改" id="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
updateShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*,entity.*"%>
<%
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 'update.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>
<h1>更新成功</h1>
<%
Model model=new Model();
int stuid = Integer.parseInt(request.getParameter("stuid"));
String stuname = request.getParameter("stuname");
String gender = request.getParameter("gender");
model.update(stuid, stuname, gender);
%>
</body>
</html>
queryById.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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 'queryById.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>
<h1>查询单个</h1>
<form action="queryByIdShow.jsp" method="post">
<table>
<tr>
<td>请输入你要查询的ID号码:</td>
<td><input type="text" name="stuid" id="stuid">
</td>
<td><input type="submit" value="提 交" id="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
queryByIdShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*,entity.*"%>
<%
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 'queryById.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>
<h1>查询单个数据</h1>
<table>
<tr><td>学号</td><td>姓名</td><td>性别</td></tr>
<%
int stuid=Integer.parseInt(request.getParameter("stuid"));
Model model=new Model();
Student student=model.queryById(stuid);
%>
<tr>
<td><%=student.getStuid()%></td>
<td><%=student.getStuname()%></td>
<td><%=student.getGender()%></td>
</tr>
</table>
</body>
</html>
queryAll.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*,entity.*"%>
<%
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 'queryAll.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>
<table>
<tr><td>学号</td><td>姓名</td><td>性别</td></tr>
<%
Model model=new Model();
List<Student> list=model.queryAll();
for(Student stu:list)
{%>
<tr>
<td><%=stu.getStuid()%></td>
<td><%=stu.getStuname()%></td>
<td><%=stu.getGender()%></td>
</tr>
<%} %>
</table>
</body>
</html>
Model1模式的学生信息增删改查的更多相关文章
- MVC模式的学生信息增删改查
准备:建一个名为 userdb的数据库.建一个student表,有stuid,stuname,gender三个字段.其中stuid为主键.j加入相应的驱动包,相应的JSTL标签 先看目录结构 代码: ...
- Java学生信息增删改查(并没用数据库)
一个泛型的应用,Java版本增删改查,写的简陋,望批评指正 2016-07-02 很久前写的一个程序了.拿出来存一下,不是为了展示啥,自己用的时候还可以看看.写的很粗糙. import java.io ...
- Spring Boot实现学生信息增删改查
上一篇博客写了如何初始化一个简单的Spring Boot项目,这次详细记录一下如何连接数据库并实现增删改查基本操作. 我使用的是MySQL 5.5+Navicat,MySQL量级比较轻,当然微软的SQ ...
- Sqlite3 实现学生信息增删改查
import sqlite3 conn = sqlite3.connect('studentsdb.db') # 连接数据库 cursor = conn.cursor( ) # 创建数据表 def c ...
- Elasticsearch 单模式下API的增删改查操作
<pre name="code" class="html">Elasticsearch 单模式下API的增删改查操作 http://192.168. ...
- python学习之-成员信息增删改查
python学习之-成员信息增删改查 主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env p ...
- 05_Elasticsearch 单模式下API的增删改查操作
05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...
- python全栈开发中级班全程笔记(第二模块、第三章)(员工信息增删改查作业讲解)
python全栈开发中级班全程笔记 第三章:员工信息增删改查作业代码 作业要求: 员工增删改查表用代码实现一个简单的员工信息增删改查表需求: 1.支持模糊查询,(1.find name ,age fo ...
- Elasticsearch学习系列之单模式下API的增删改查操作
这里我们通过Elasticsearch的marvel插件实现单模式下API的增删改查操作 索引的初始化操作 创建索引之前可以对索引进行初始化操作,比如先指定shard数量以及replicas的数量 代 ...
随机推荐
- brainfuck
/阅读这样的代码就像在强奸你的大脑 #include<stdio.h> #include<ctype.h> #include<stdlib.h> #include ...
- 5050 [JL] 他爱上了鸭蛋
5050 [JL] 他爱上了鸭蛋 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 小明爱上了零鸭蛋.他喜欢输 ...
- 20135335郝爽 & 20135304刘世鹏 实验一
北京电子科技学院(BESTI) 实 验 报 告 课程: 密码系统设计基础 ...
- 零散知识记录-一个MQ问题
[背景]我有一项零散工作:维护大部门的一台测试公用MQ服务器.当大部分MQ被建立起来,编写了维护手册,大家都按照规程来后,就基本上没有再动过它了.周五有同学跟我反映登录不进去了,周日花了1个小时来解决 ...
- WP & Win10开发:实现ListView下拉加载的两种方法
1.通过ListView控件的ContainerContentChanging方法.该方法在列表项被实例化时触发,在列表项最后一个项目实例化的时候触发刷新数据逻辑就可以实现下拉加载了. 代码如下:// ...
- node 学习笔记 - fs 文件操作
本文同步自我的个人博客:http://www.52cik.com/2015/12/03/learn-node-fs.html 最近看到群里不少大神都开始玩 node 了,我感觉跟他们步伐越来越大了, ...
- unity3d 赛车游戏——复位点检测优化、反向检测、圈数检测、赛道长度计算
接着上一篇文章说 因为代码简短且思路简单 所以我就把这几个功能汇总为一篇文章 因为我之前就是做游戏外挂的 经过验证核实,**飞车的复位点检测.圈数检测就是以下的方法实现的 至于反向检测和赛道长度计算, ...
- IT男的”幸福”生活"续4
翻来翻去,总是睡不觉.大脑口一堆问题.一个又冒出一个,没完没了.明天该怎样去进行下一步呢.. ….. 夜一下子深黑很多,窗外的公路,时而有货车通过,动不动按喇叭,而我住在二楼,真它的吵.也许她住在五楼 ...
- 从日常开发说起,浅谈HTTP协议是做什么的。
引言 HTTP协议作为Web开发的基础一直被大多数人所熟知,不过相信有很多人只知其一不知其二.比如咱们经常用到的session会话机制是如何实现的,可能很多人都说不出来吧.其实session会话就是H ...
- PHP时间日期比较
若要使用PHP来比较日期,最好用DateTime::diff 但是这个是5.3才支持的,如果没有这样的环境,可以使用<.>来比较 如下例子,会输出right $date1=strtotim ...