增删改查
bean:
package bean;
public class Dbean {
private String date;
private String zi;
private String zong;
private String days ;
private String Mdays ;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getZi() {
return zi;
}
public void setZi(String zi) {
this.zi = zi;
}
public String getZong() {
return zong;
}
public void setZong(String zong) {
this.zong = zong;
}
public String getDays() {
return days;
}
public void setDays(String days) {
this.days = days;
}
public String getMdays() {
return Mdays;
}
public void setMdays(String mdays) {
this.Mdays = mdays;
}
public Dbean(String date, String zi, String zong, String days, String mdays) {
this.date = date;
this.zi = zi;
this.zong = zong;
this.days = days;
this.Mdays = mdays;
}
}
dao:
package dao;
import Util.DBUtil;
import bean.Dbean;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Dao {
public boolean add(Dbean ten) throws ClassNotFoundException, SQLException {
String sql = "insert into biao1(date,zi,zong,days,Mdays)values"
+ "('" + ten.getDate() + "','" + ten.getZi() + "','" + ten.getZong() + "','" + ten.getDays() + "','" + ten.getMdays() + "')";
Connection conn = DBUtil.getConnection();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if (a > 0)
f = true;
return f;
}
public Dbean getbytitle(String name) throws ClassNotFoundException, SQLException {
String sql = "select * from biao1 where date ='" + name + "'";
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
Dbean ten = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String title2 = rs.getString("date");
String zi2 = rs.getString("zi");
String person2 = rs.getString("zong");
String date2 = rs.getString("days");
String neirong2 = rs.getString("Mdays");
ten = new Dbean(title2, zi2, person2, date2, neirong2);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return ten;
}
public boolean delete(String name) throws SQLException, ClassNotFoundException {
String sql="delete from biao1 where date='" + name + "'";
Connection conn = DBUtil.getConnection();
Statement state = null;
int a = 0;
boolean f = false;
try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
public boolean name(String name) throws SQLException, ClassNotFoundException {
boolean flag = false;
String sql = "select date from biao1 where date = '" + name + "'";
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return flag;
}
public List<Dbean> search(String zi, String date) throws SQLException, ClassNotFoundException {
String sql = "select * from biao1 where ";
if (zi != "") {
sql += "zhuti like '%" +zi+ "%'";
}
if (date != "") {
sql += "time like '%" +date+ "%'";
}
List<Dbean> list = new ArrayList<>();
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
Dbean bean = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String title2 = rs.getString("title");
String zi2 = rs.getString("zi");
String person2 = rs.getString("person");
String date2=rs.getString("date");
String neirong2=rs.getString("neirong");
bean = new Dbean(title2, zi2 ,person2,date2,neirong2);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
public List<Dbean> list() throws SQLException, ClassNotFoundException {
String sql = "select * from biao1";
List<Dbean> list = new ArrayList<>();
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
Dbean bean = null;
int id=rs.getInt("id");
String title2 = rs.getString("date");
String zi2 = rs.getString("zi");
String person2 = rs.getString("zong");
String date2=rs.getString("days");
String neirong2=rs.getString("Mdays");
bean = new Dbean(title2, zi2 ,person2,date2,neirong2);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return list;
}
}
service:
package service;
import bean.Dbean;
import dao.Dao;
import java.sql.SQLException;
import java.util.List;
public class Service {
Dao tDao=new Dao();
public boolean add(Dbean ten) throws SQLException, ClassNotFoundException {
boolean f = false;
if(!tDao.name(ten.getDate()))
{
tDao.add(ten);
f=true;
}
return f;
}
public boolean del(String title) throws SQLException, ClassNotFoundException {
tDao.delete(title);
return true;
}
public Dbean getbytitle(String title) throws SQLException, ClassNotFoundException {
return tDao.getbytitle(title);
}
public List<Dbean> list() throws SQLException, ClassNotFoundException {
return tDao.list();
}
}
servlet:
package servlet;
import Util.DBUtil;
import service.Service;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AddServlet
*/
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Service service = new Service();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编译格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//接收数据
String date = request.getParameter("date");
String zi = request.getParameter("zi");
String zong = request.getParameter("zong");
String days = request.getParameter("days");
String Mdays = request.getParameter("Mdays");
PrintWriter write = response.getWriter();
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = DBUtil.getConnection();
String sql = "insert into biao1(date,zi,zong,days,Mdays) values(?,?,?,?,?)";
preparedStatement = null;
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, date);
preparedStatement.setString(2, zi);
preparedStatement.setString(3, zong);
preparedStatement.setString(4, date);
preparedStatement.setString(5, Mdays);
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
request.getRequestDispatcher("list.jsp").forward(request,response);
}
private boolean isNumeric(String str) {
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
}
——————————————————-——————
package servlet;
import Util.DBUtil;
import service.Service;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DelateServlet
*/
@WebServlet("/DelateServlet")
public class DelateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Service service =new Service();
/**
* @see HttpServlet#HttpServlet()
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编码格式设置成成UTF-8
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String date =request.getParameter("date");//接收删除的日期
PreparedStatement preparedStatement = null;
Connection connection = null;
boolean f=false;
try {
connection = DBUtil.getConnection();
String sql = "delete from biao1 where date='"+date+"'";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
request.getRequestDispatcher("list.jsp").forward(request,response);
}
public DelateServlet() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @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);
}
}
-——————————————————————————————
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FindServlet
*/
@WebServlet("/FindServlet")
public class FindServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FindServlet() {
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
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @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);
}
}
——————————————————————
package servlet;
import Util.DBUtil;
import service.Service;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UpdateServlet
*/
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
Service service=new Service();
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//编码格式设置成成UTF-8
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String date =request.getParameter("date");//接收数据
String zi =request.getParameter("zi");
String zong =request.getParameter("zong");
String days =request.getParameter("days");
String Mdays =request.getParameter("Mdays");
PreparedStatement preparedStatement = null;
Connection connection = null;
boolean f=false;
try {
connection = DBUtil.getConnection();
String sql = "update biao1 set date='"+date+"',zi='"+zi+"',zong='"+zong+"',days='"+days+"',Mdays='"+Mdays+"' where date='"+date+"'";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
request.getRequestDispatcher("list.jsp").forward(request,response);
}
public UpdateServlet() {
// 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
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @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);
}
}
jsp:
menu:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" http-equiv="Content-type" content="text/html">
<title>Mnue</title>
</head>
<body>
<form action="#" method="get">
<p style="text-align:center;font-size:20px" >
<input type="button" value="信息登记" onclick="location.href='add.jsp'" /><br>
<input type="button" value="浏览信息" onclick="location.href='list.jsp'" /><br>
<input type="button" value="删除信息" onclick="location.href='delate.jsp'" /><br>
<input type="button" value="更新信息" onclick="location.href='change.jsp'" /><br>
<input type="button" value="查询信息" onclick="location.href='find.jsp'" /><br>
<br>
</p>
</form>
</body>
</html>
add:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>add</title>
</head>
<body>
<form method="post" action="AddServlet">
<table align="center" border="1" width="600">
<tr>
<td>日期</td>
<td>
<input type="text" name="date" ><br>
</td>
</tr>
<tr>
<td>每日关键字</td>
<td>
<input type="text" name="zi" ><br>
</td>
</tr>
<tr>
<td>每日总结</td>
<td>
<input type="text" name="zong" ><br>
</td>
</tr>
<tr>
<td>坚持天数</td>
<td>
<input type="text" name="days" ><br>
</td>
</tr>
<tr>
<td>连续最长天数</td>
<td>
<input type="text" name="Mdays" ><br>
</td>
</tr>
<tr align="center">
<td colspan="2">
<button>提交</button>
</td>
</tr>
</table>
</form>
</body>
</html>
delate:
<%@ page import="Util.DBUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>delate</title>
</head>
<body>
<form action="DelateServlet" metheod="post">
<table align="center" border="2" width=0>
<h1 align="center">输入你选择删除的日期</h1>
<div>
<tr align="center">
<td>
<input type="text" name="date"></tr>
<td/>
</div >
<div align="center">
<button >提交</button>
</div>
</table>
</form>
</body>
</html>
change:
<%@ page import="Util.DBUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table align="center" border="1" width=0>
<tr>
<td align="center" width=0>日期</td>
<td align="center" width=0>每日关键字</td>
<td align="center" width=0>每日总结</td>
<td align="center" width=0>坚持天数</td>
<td align="center" width=0>连续最长天数</td>
</tr>
<%
Connection connection= DBUtil.getConnection();
PreparedStatement preparedStatement=null;
ResultSet in=null;
try{
// 按照添加时间排序::
preparedStatement=connection.prepareStatement("select * from biao1");
in = preparedStatement.executeQuery();
while(in.next()){
%>
<tr>
<td><%=in.getString(1)%></td>
<td><%=in.getString(2)%></td>
<td><%=in.getString(3)%></td>
<td><%=in.getString(4)%></td>
<td><%=in.getString(5)%></td>
</tr>
<%
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
connection.close();
preparedStatement.close();
in.close();
}
%>
<form action="UpdateServlet" metheod="post">
<table align="center" border="2" width=0>
<div>
<tr align="center">
<td><h1 align="center">输入你选择修改的日期</h1></td>
<td>
<input type="text" name="date"></tr>
<td/>
</div >
<tr>
<td>每日关键字</td>
<td>
<input type="text" name="zi" ><br>
</td>
</tr>
<tr>
<td>每日总结</td>
<td>
<input type="text" name="zong" ><br>
</td>
</tr>
<tr>
<td>坚持天数</td>
<td>
<input type="text" name="days" ><br>
</td>
</tr>
<tr>
<td>连续最长天数</td>
<td>
<input type="text" name="Mdays" ><br>
</td>
</tr>
</table>
<div align="center">
<button >提交</button>
</div>
</form>
</body>
</html>
find:
<%@ page import="Util.DBUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table align="center" border="1">
<form action="Finded.jsp" metheod="post">
<table align="center" border="1" width=0>
<div>
<tr align="center">
<td>输入你选择查询的日期</td>
<td>
<input type="text" name="date"></tr>
<td/>
</div >
</table>
<div align="center">
<button >提交</button>
</div>
</form>
</body>
</html>
Finded:
<%@ page import="Util.DBUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
<html>
<head>
<title>浏览</title>
</head>
<body>
<h2 align="center" >查询结果如下</h2><br>
<table align="center" border="1">
<tr>
<td align="center" width=10%>日期</td>
<td align="center" width=10%>每日关键字</td>
<td align="center" width=10%>每日总结</td>
<td align="center" width=10%>坚持天数</td>
<td align="center" width=10%>连续最长天数</td>
</tr>
<%
String date=request.getParameter("date");
Connection connection= DBUtil.getConnection();
PreparedStatement preparedStatement=null;
ResultSet in=null;
try{
// 按照添加时间排序::
preparedStatement=connection.prepareStatement("select * from biao1");
in = preparedStatement.executeQuery();
while(in.next()){
if(in.getString(1).equals(date)){
%>
<tr>
<td><%=in.getString(1)%></td>
<td><%=in.getString(2)%></td>
<td><%=in.getString(3)%></td>
<td><%=in.getString(4)%></td>
<td><%=in.getString(5)%></td>
</tr>
<%
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
connection.close();
preparedStatement.close();
in.close();
}
%>
<p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
<br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br>
</table>
</body>
</html>
list:
<%@ page import="Util.DBUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
<html>
<head>
<title>浏览</title>
</head>
<body>
<h2 align="center" >操作结果</h2><br>
<table align="center" border="1">
<tr>
<td align="center" width=10%>日期</td>
<td align="center" width=10%>每日关键字</td>
<td align="center" width=10%>每日总结</td>
<td align="center" width=10%>坚持天数</td>
<td align="center" width=10%>连续最长天数</td>
</tr>
<%
Connection connection= DBUtil.getConnection();
PreparedStatement preparedStatement=null;
ResultSet in=null;
try{
// 按照添加时间排序::
preparedStatement=connection.prepareStatement("select * from biao1");
in = preparedStatement.executeQuery();
while(in.next()){
%>
<tr>
<td><%=in.getString(1)%></td>
<td><%=in.getString(2)%></td>
<td><%=in.getString(3)%></td>
<td><%=in.getString(4)%></td>
<td><%=in.getString(5)%></td>
</tr>
<%
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
connection.close();
preparedStatement.close();
in.close();
}
%>
<p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
<br> <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br>
</table>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>copy</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
</web-app>
浙公网安备 33010602011771号