Servlet+纯java+MySQL实现课程信息的增删改查
Dbutil:
package com.zh.util; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException; public class DButil {
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:mysql://localhost:3306/teacher_class?serverTimezone=UTC";
conn=DriverManager.getConnection(url, "root", "hao19990507.");
}catch (Exception e){
e.printStackTrace();
}
return conn;
} public static void close(Statement sta,Connection con){
if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs,Statement sta,Connection con){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if (sta != null) {
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
bean:
package com.zh.entity; public class bean {
private String place;
private String teacher_name;
private String class_name;
public String get_place() {
return place;
}
public void set_place(String place) {
this.place=place;
}
public String get_teacher_name() {
return teacher_name;
}
public void set_teacher_name(String teacher_name) {
this.teacher_name=teacher_name;
}
public String get_class_name() {
return class_name;
}
public void set_class_name(String class_name) {
this.class_name=class_name;
}
public bean() {}; public bean(String place,String teacher_name,String class_name) {
this.place=place;
this.teacher_name=teacher_name;
this.class_name=class_name;
}
}
data_dao:
package com.zh.dao;
import java.util.*;
import java.sql.*;
import com.zh.entity.*;
import com.zh.util.*;
import java.sql.*;
public class data_dao {
/*增加
*
*/
public void add(bean Bean) {
String sql="insert into class_message values('"+Bean.get_place()+"','"+Bean.get_teacher_name()+"','"+Bean.get_class_name()+"')";
Connection conn = DButil.getConnection();
Statement state = null;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
/*删除
*
*/
public void deleteByteacher_name(String teacher_name) {
boolean f=false;
String sql="delete from class_message where teacher_name='"+teacher_name+"'";
Connection conn=DButil.getConnection();
Statement state = null; try { state = conn.createStatement();
state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} }
/* 修改
*
*/ public void updata(bean b) {
String sql="updata class_message set place='"+b.get_place()+"', teacher_name='"+b.get_teacher_name()+"',class_name='"+b.get_class_name()+"' where teacher_name='"+b.get_teacher_name()+"'"; Connection conn=DButil.getConnection();
Statement state = null;
try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} }
/**
* 验证课程名称是否唯一
*/
public List<bean> isOnly_class_name(String class_name) {
List list=new ArrayList<bean>();
String sql="select name from class_message where class_name='"+class_name+"'";
Connection conn=DButil.getConnection();
Statement state=null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while(rs.next()) {
String class_name1=rs.getString("class_name");
String teacher_name=rs.getString("teacher_name");
String place=rs.getString("place");
bean b=new bean(place,teacher_name,class_name1);
list.add(b);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
} /**
* 通过teacher_name获得一个bean对象
*/
public bean findByteacher_name(String teacher_name) {
Connection conn=null;
Statement state=null; try {
conn=DButil.getConnection();
String sql="select*from class_message where teacher_name='"+teacher_name+"'"; state=conn.createStatement();
ResultSet rs=state.executeQuery(sql);
bean u=new bean();;
while(rs.next()) {
String t_name=rs.getString("teacher_name");
String c_name=rs.getString("class_name");
String place=rs.getString("place");
u.set_class_name(c_name);
u.set_place(place);
u.set_teacher_name(t_name);
return u;
}
}catch (Exception e) {
e.printStackTrace();
} return null;
}
/*
* 显示全部
*/
public List<bean> findAll(){
Connection conn=DButil.getConnection();
List<bean> list=new ArrayList<bean>();
bean b=null;
try {
Statement sta=conn.createStatement();
String sql="select * from class_message";
ResultSet rs=sta.executeQuery(sql);
while(rs.next()) {
b=new bean();
String t_name,c_name,place;
t_name=rs.getString("teacher_name");
c_name=rs.getString("c_name");
place=rs.getString("place");
b.set_class_name(c_name);
b.set_place(place);
b.set_teacher_name(t_name);
list.add(b);
}
}catch (Exception e) {
e.printStackTrace();
}
for(int i=0;i<list.size();i++) {
bean br=(bean)list.get(i);
System.out.println(br.get_teacher_name());
}
return list;
}
}
Userservice:
package com.zh.service;
import java.util.*;
import com.zh.dao.*;
import com.zh.entity.*;
public class Userservice {
public boolean insertUser(String place,String teacher_name,String class_name) {
data_dao dao=new data_dao();
bean b=new bean();
b.set_class_name(class_name);
b.set_place(place);
b.set_teacher_name(teacher_name);
try {
dao.add(b);
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean deleteUser(String teacher_name) {
data_dao dao=new data_dao();
bean b=dao.findByteacher_name(teacher_name);
if(b!=null) {
dao.deleteByteacher_name(teacher_name);
return true;
}else
return false;
}
public boolean updataByteacher_name(String place,String class_name,String teacher_name) {
data_dao dao=new data_dao();
bean b=new bean(place,class_name,teacher_name);
try {
dao.updata(b);
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean isOnly_classname(String class_name) {
data_dao dao=new data_dao();
try {
List list=dao.isOnly_class_name(class_name);
if(list.size()>1)
return true;
else
return false;
}catch (Exception e) {
e.printStackTrace();
return false;
}
} public List<bean> findAll_user(){
data_dao dao=new data_dao();
List list=dao.findAll();
return list;
}
}
DeleteServlet:
package com.zh.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;
import com.zh.service.*;
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet { protected void service(HttpServletRequest arg0,HttpServletResponse arg1)
throws ServletException ,IOException{
arg1.setContentType("text/html");
arg0.setCharacterEncoding("GBK");
arg1.setCharacterEncoding("GBK");
String name=arg0.getParameter("teacher_name");
Userservice service =new Userservice();
service.deleteUser(name);
try {
arg0.getRequestDispatcher("/query").forward(arg0, arg1);
}catch (Exception e) {
e.printStackTrace();
}
} }
InsertServlet:
package com.zh.servlet; import java.io.IOException;
import java.io.PrintWriter; 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 com.zh.service.*;
@WebServlet("/InsertServlet")
public class InsertServlet extends HttpServlet {
protected void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException {
res.setContentType("text/html");
req.setCharacterEncoding("GBK");
res.setCharacterEncoding("GBK");
String place=req.getParameter("place");
String teacher_name=req.getParameter("teacher_name");
String class_name=req.getParameter("class_name");
PrintWriter pw=res.getWriter();
Userservice service=new Userservice();
try {
service.insertUser(place, teacher_name, class_name);
pw.println("新增信息成功!");
req.getRequestDispatcher("/query").forward(req, res);;
}catch (Exception e) {
e.printStackTrace();
req.getRequestDispatcher("/insert.html").forward(req, res);
}
} }
QueryServlet:
package com.zh.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;
import java.io.PrintWriter;
import com.zh.entity.*;
import com.zh.service.*;
import java.util.*;
@WebServlet("/QueryServlet")
public class QueryServlet extends HttpServlet {
protected void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException {
res.setContentType("text/html");
req.setCharacterEncoding("GBK");
res.setCharacterEncoding("GBK");
PrintWriter pw=res.getWriter();
Userservice service=new Userservice();
List list=service.findAll_user();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<table border='1' bordercolor='red' align='center'");
pw.println("<tr>");
pw.println("<td>");
pw.println("地点");
pw.println("</td>");
pw.println("<td>");
pw.println("教师名");
pw.println("</td>");
pw.println("<td>");
pw.println("课程名");
pw.println("</td>");
pw.println("</tr>");
for(int i=0;i<list.size();i++) {
bean b=(bean) list.get(i);
pw.println("<tr>");
pw.println("<td>");
pw.println(b.get_place());
pw.println("</td>");
pw.println("<td>");
pw.println(b.get_teacher_name());
pw.println("</td>");
pw.println("<td>");
pw.println(b.get_class_name());
pw.println("/<td>");
pw.println("<a href='/Web_operation1/delete?teacher_name="+b.get_teacher_name()+"'> 删 除 </a>||<a href='/Web_operation1/updata.html'>修改信息</a>" );
pw.println("</td>");
pw.println("</tr>");
}
pw.println("</table>");
pw.println("</body>");
pw.println("</html>");
pw.flush();
pw.close();
}
}
UpdateServlet:
package com.zh.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;
import java.io.PrintWriter;
import com.zh.service.*; @WebServlet("/UpdataServlet")
public class UpdateServlet extends HttpServlet {
protected void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException {
res.setContentType("text/html");
req.setCharacterEncoding("GBK");
res.setCharacterEncoding("GBK");
PrintWriter pw=res.getWriter();
Userservice service=new Userservice();
String place=req.getParameter("place");
String teacher_name=req.getParameter("teacher_name");
String class_name=req.getParameter("class_name");
try {
if (service.updataByteacher_name(place, class_name, teacher_name)) {
res.sendRedirect("/Web_operation1/query");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
insert.html:
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="UTF-8">
<title>Insert </title>
</head>
<body>
<center>
<form action="/Web_operation1/insert"medthod="post">
<table border ="1" bordercolor="red"align="center">
<tr>
<td>
上课地点:
</td>
<td>
<input type="text" name="place"/>
</td> </tr>
<tr>
<td>
任课教师:
</td>
<td>
<input type="text" name="teacher_name"/>
</td>
</tr>
<tr>
<td>
课程名称:
</td>
<td>
<input type="text" name="class_name"/>;
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
update.html:
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="UTF-8">
<title>update</title>
</head>
<body>
<center>
<form action="/Web_operation1/update" method="post">
<table border="1"bordercolor="red" align="center">
<tr>
<td> 上课地点:</td>
<td>
<input type="text" name="place">
</td>
</tr>
<tr>
<td> 任课教师:</td>
<td>
<input type="text" name="teacher_name">
</td>
</tr>
<tr>
<td> 课程名称:</td>
<td>
<input type="text"name="class_name">
</td>
</tr>
<tr>
<td colspan="2"align="center">
<input type="button" value="确认">
</td>
</tr>
</table>
</form>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>Web_operation1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>query</servlet-name>
<servlet-class>com.zh.servlet.QueryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>query</servlet-name>
<url-pattern>/query</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>com.zh.servlet.DeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/delete</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>update</servlet-name>
<servlet-class>com.zh.servlet.UpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>update</servlet-name>
<url-pattern>/update</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>insert</servlet-name>
<servlet-class>com.zh.servlet.InsertServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>insert</servlet-name>
<url-pattern>/insert</url-pattern>
</servlet-mapping> </web-app>
Servlet+纯java+MySQL实现课程信息的增删改查的更多相关文章
- Java通过JDBC进行简单的增删改查(以MySQL为例)
Java通过JDBC进行简单的增删改查(以MySQL为例) 目录: 前言:什么是JDBC 一.准备工作(一):MySQL安装配置和基础学习 二.准备工作(二):下载数据库对应的jar包并导入 三.JD ...
- AD 域服务简介(三)- Java 对 AD 域用户的增删改查操作
博客地址:http://www.moonxy.com 关于AD 域服务器搭建及其使用,请参阅:AD 域服务简介(一) - 基于 LDAP 的 AD 域服务器搭建及其使用 Java 获取 AD 域用户, ...
- MYSQL - database 以及 table 的增删改查
MYSQL - database 以及 table 的增删改查 MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这 ...
- 使用JDBC分别利用Statement和PreparedStatement来对MySQL数据库进行简单的增删改查以及SQL注入的原理
一.MySQL数据库的下载及安装 https://www.mysql.com/ 点击DOWNLOADS,拉到页面底部,找到MySQL Community(GPL)Downloads,点击 选择下图中的 ...
- MySQL数据库之表的增删改查
目录 MySQL数据库之表的增删改查 1 引言 2 创建表 3 删除表 4 修改表 5 查看表 6 复制表 MySQL数据库之表的增删改查 1 引言 1.MySQL数据库中,数据库database就是 ...
- Node教程——Node+MongoDB案例实现用户信息的增删改查
想要获取源代码的同学可以留言,我不做git上传了,案例太简单 没必要 综合演练 用户信息的增删改查 需求:你需要实现这样的结果 点击添加可以添加用户,点击删除可以删除点击修改可以修改 代码分析: 1. ...
- Java Maven:spring boot + Mybatis连接MySQL,通用mapper的增删改查,映射实现多表查询
1. MySQL自带库test添加表user.role 角色表role 用户表user 2. 添加依赖,配置属性 相关依赖:百度即可,此处略 application.properties spring ...
- Java连接MySQL数据库,并进行增删改查
1.具体的代码实现 import java.sql.*; public class DatabaseService { /** * Create Connection * * @param dbtyp ...
- nodejs+express+mysql实现restful风格的增删改查示例
首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...
随机推荐
- 一种隐蔽性较高的Java ConcurrentModificationException异常场景
前言 在使用Iterator遍历容器类的过程中,如果对容器的内容进行增加和删除,就会出现ConcurrentModificationException异常.该异常的分析和解决方案详见博文<Jav ...
- Windows 平台下局域网劫持测试工具 – EvilFoca
简介 安全测试工具可能含有攻击性,请谨慎适用于安全教学及学习用途,禁止非法利用! EvilFoca是Windows环境下基于.NET FrameWork的一款轻量级的劫持测试工具.与BackTrack ...
- 爬虫IP代理中的http与https
之前使用代理IP,构造的proxies一直都是http模式 proxies={"http": "http://{}".format(ip)} 但是今天遇到的网站 ...
- ELK之安装searchguard后默认管理员用户admin修改
安装完elasticsearch之后会有一个默认的用户admin密码也为admin,该用户无法删除无法编辑修改密码,用于生产时安全性较差,需要修改默认密码或者删除该admin用户 使用工具生产加密密码 ...
- stm32专属于菜鸟的学习方法
1.首先我们先看看与STM32相关的文档 我们假定大家已经对STM32的书籍或者文档有一定的理解.如不理解,请立即阅读STM32的文档,以获取最基本的知识点. 如果你手上拥有ST官方主推的STM32神 ...
- sqlserver开启远程访问
1.通过本地连接数据库,选择数据库——右键——属性 2.在连接选项勾选“允许远程连接到此服务器” 3.打开sqlserver配置管理器 4.到sqlserver网络配置——XXX的协议——TCP/IP ...
- react中进入某个详情页URL路劲参数Id获取问题
<Route path={`${match.url}/detail/:id`} component={AppManageAddDetail} /> const { match:{param ...
- 图->连通性->关节点和重连通分量
文字描述 相关定义:假若在删去顶点v以及和v相关联的各边之后,将图的一个连通分量分割成两个或两个以上的连通分量,则称顶点v为该图的一个关节点.一个没有关节点的连通图称为重连通图. 在重连通图上,任意一 ...
- c++代码检测工具
cppcheck是一款静态代码检查工具,可以检查如内存泄漏等代码错误,使用起来比较简单,即提供GUI界面操作,也可以与VS开发工具结合使用. 1.安装 一般会提供免安装版,安装完成后将cppcheck ...
- 10.9-uC/OS-III任务管理
1.OSTaskCreate () 要使用 uC/OS 的任务必须先声明任务控制块和创建任务,调用 OSTaskCreate () 函数可以创建一个任务. 2.OSTaskSuspend () OST ...