首页 > 其他分享 >终于凑齐增删改查四个宝喽

终于凑齐增删改查四个宝喽

时间:2022-10-25 14:11:34浏览次数:54  
标签:凑齐 preparedStatement name 改查 public connection 增删 id String

表依旧是上上次的表~

然后是目录结构

 

 

 然后……贴代码!

package Bean;

public class Bean {

String id;
String name;
public Bean() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Bean(String id, String name) {
super();
this.id = id;
this.name = name;
}

}




package DBUtil;

import java.sql.*;

public class DBUtil {
private static Connection connection;
private static String url="jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
private static String user="root";//用户名
private static String pass="mysql26331";//密码

static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
connection= DriverManager.getConnection(url,user,pass);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection, Statement statement, ResultSet resultSet){
try {
if (connection!=null) {
connection.close();
}
if (statement!=null){
statement.close();
}
if (resultSet!=null){
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement, Connection connection) {
// TODO Auto-generated method stub
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedstatement, Connection connection, ResultSet rs) {
// TODO Auto-generated method stub
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
preparedstatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}




package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Bean.Bean;
import DBUtil.DBUtil;

public class Dao {

//增删改是差不多的操作,查询相对特殊一点

public static void add(String id, String name) {
// TODO Auto-generated method stub
Connection connection=null;
PreparedStatement preparedStatement=null;
connection= DBUtil.getConnection();
String sql="insert into inf(id,name) values (?,?)";
try{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,id);//就是把id替代sql的第一个问号,id由前端传过来
preparedStatement.setString(2,name);
System.out.print("信息传输成功");
int flag = preparedStatement.executeUpdate();
if(flag != 0) {
System.out.print("信息添加成功");
} else {
System.out.print("信息添加失败");
}
preparedStatement.close();
connection.close();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(preparedStatement,connection);
}
}

public static void delete(String id, String name) {
// TODO Auto-generated method stub
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection= DBUtil.getConnection();
//这里一定要注意!!!这里和查找操作不同,from前边没有*
String sql="delete from inf where id=?";
preparedStatement=connection.prepareStatement(sql);
//寻找到需要删除的数据,执行删除指令
preparedStatement.setString(1,id);
int flag = preparedStatement.executeUpdate();
if(flag != 0) {
System.out.print("信息删除成功");
} else {
System.out.print("信息删除失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(connection,preparedStatement,null);
}
}

public static void change(String id, String name) {
// TODO Auto-generated method stub
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection= DBUtil.getConnection();
//这里也算是一个小小的踩坑点吧,通过id查找,set后边就不要添加id=?了,不然下边也要对应得进行修改
//sql中有几个?就要写几个preparedStatement.setString
String sql="update inf set name=? where id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setString(2,id);

int flag = preparedStatement.executeUpdate();
if(flag != 0) {
System.out.print("信息修改成功");
} else {
System.out.print("信息修改失败");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(connection,preparedStatement,null);
}
}

public static Bean found(String id) {
// TODO Auto-generated method stub
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
Bean bb = null;
try {
connection= DBUtil.getConnection();
String sql="select * from inf where id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,id);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String id2=resultSet.getString(1);
String name=resultSet.getString(2);
bb = new Bean(id2, name);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(connection,preparedStatement,resultSet);
}
return bb;
}

}




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;

import Dao.Dao;

/**
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
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());
//防止乱码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");

//判断前端传来的标记,以此执行相对应的增删改查操作
String method=request.getParameter("method");
//获取前端传来的数据
if("add".equals(method)) {
//获取前端传来的数据
String id=request.getParameter("id");
String name=request.getParameter("name");
//调用add方法将数据添加进数据库
Dao.add(id,name);
} else if("delete".equals(method)) {
//获取前端传来的数据
String id=request.getParameter("id");
String name=request.getParameter("name");
//调用delete方法删除数据库相关信息
Dao.delete(id,name);
} else if("change".equals(method)) {
//获取前端传来的数据
String id=request.getParameter("id");
String name=request.getParameter("name");
//调用change方法修改数据库相关信息
Dao.change(id,name);
} else if("found".equals(method)) {
//获取前端传来的数据
String id=request.getParameter("id");
//调用found方法查询数据库相关信息
request.setAttribute("Bean",Dao.found(id));
request.getRequestDispatcher("show.jsp").forward(request,response);
}

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}

}




<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>主界面</title>
<body>
<center>
<p style="height: 20px; border: 1px solid red;text-align: center;">主界面</p>

<button type="submit" value="add" onclick="window.location.href='add.jsp'">增加信息</button>
<br><br>
<button type="submit" value="delete" onclick="window.location.href='delete.jsp'">删除信息</button>
<br><br>
<button type="submit" value="change" onclick="window.location.href='change.jsp'">修改信息</button>
<br><br>
<button type="submit" value="found" onclick="window.location.href='found.jsp'">查询信息</button>
<br><br>
</center>
</body>
</html>




<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>添加</title>
</head>
<body>
<center>
<form action="Servlet" method="post">

<p style="height: 20px; border: 1px solid red;text-align: center;">添加</p>

<p>请输入id:<input type="text" name="id"><p/>
<p>请输入姓名:<input type="text" name="name"><p/>

<input type="hidden" name="method" value="add"/>
<input type="submit" name="submit" value="添加">

</form>

</center>
</body>
</html>




<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>删除</title>
</head>
<body>
<center>
<form action="Servlet" method="post">

<p style="height: 20px; border: 1px solid red;text-align: center;">删除</p>

<p>请输入id:<input type="text" name="id"><p/>
<p>请输入姓名:<input type="text" name="name"><p/>

<input type="hidden" name="method" value="delete"/>
<input type="submit" name="submit" value="删除">

</form>

</center>
</body>
</html>




<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>修改</title>
</head>
<body>
<center>
<form action="Servlet" method="post">

<p style="height: 20px; border: 1px solid red;text-align: center;">修改</p>

<p>请输入id:<input type="text" name="id"><p/>
<p>请输入姓名:<input type="text" name="name"><p/>

<input type="hidden" name="method" value="change"/>
<input type="submit" name="submit" value="修改">

</form>

</center>
</body>
</html>




<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>查找</title>
</head>
<body>
<center>
<form action="Servlet" method="post">

<p style="height: 20px; border: 1px solid red;text-align: center;">查询</p>
<%--
通过学号查询
--%>
<p>请输入id:<input type="text" name="id"><p/>

<p>请输入姓名:<input type="text" name="name"><p/>

<input type="hidden" name="method" value="found"/>
<input type="submit" name="submit" value="查询">

</form>

</center>
</body>
</html>




<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>展示</title>
</head>
<body>
<center>
<form action="Servlet" method="post">
id:${Bean.id}<br/>
姓名:${Bean.name}<br/>
<input type="hidden" name="method" value="change"/>
<%--
<br><br>
<button type="submit" value="change" onclick="window.location.href='change.jsp'">修改</button>
<br><br>
--%>

</form>
</center>
</body>
</html>

标签:凑齐,preparedStatement,name,改查,public,connection,增删,id,String
From: https://www.cnblogs.com/yansans/p/16824661.html

相关文章

  • day17 MySQL的安装 & 数据库基本语法——增删改查
    day17MySQL登录数据库mysql-hlocalhost-P3307-uroot-p查看所有数据库showdatabases;退出数据库exit;//现有表格usesitu;//使用哪个数据库createtable......
  • json模拟数据,实现增删该查
    operation.jsconst{rejects}=require('assert')constfs=require('fs')const{resolve}=require('path')/*查询数据*/constqueryData=({......
  • Elasticsearch-----介绍与增删改查操作
    一、介绍:1、概念:是功能非常强大的全文搜索引擎,目的是为了能快速的查询数据2、核心概念:接近实时(NRT):Elasticsearch是一个接近实时的搜索平台。从索引一个文档直到这个文档能......
  • mysql-增删改查
    mysql-增删改查####mysqlctrl+l清屏ctrl+c终止[linux]servicemysqlstart启动mysqlservicemysqlstop停止mysqlservicemysqlrestart重启mysql......
  • firebath数据库常见操作——增删改查使用总结
    firebath数据库常见操作——增删改查使用总结问题背景之前在看一个GitHub上的海外项目用到了Google的实时在线数据库firebath,查看官方文档提供的demo和api介绍感觉相对还......
  • JDBC的增删改查基本操作
    packageJDBC_1;//演示jdbc所使用的预编译importjava.sql.*;publicclassjdbc_2{privatestaticConnectioncoon=null;static{try{Cl......
  • Entity Framework教程-增删改查(Data Create、Delete、Update、Select)
    更新记录转载请注明出处:2022年10月19日发布。2022年10月10日从笔记迁移到博客。增删改查增删改说明对实体进行增删改操作,会改变实体的EntityState属性插入(Inse......
  • [答疑]反正最后都会有增删改查用例,为什么不直接写出来?
    小马乖乖2021-8-1311:03书上说不要增删改查用例,我总觉得既然系统保存数据,肯定会有对数据增删改查的用例,为什么不列出来呢?UMLChina潘加宇首先,系统里有某个数据,不代表一定需......
  • Mybatis 增删改查 基础小进阶
    有部分相同不需要修改可以点击--->简单的Mybatis项目1.配置Maven依赖2.配置环境文件mybatis-config.xml3.编写MybatisUtils工具类4.编写实体类User5.实体类UserMapper......
  • 基础09:数据处理之增删改
    一、插入数据1.1方式1:VALUES的方式添加使用这种语法一次只能向表中插入一条数据。情况1:为表的所有字段按默认顺序插入数据--语法格式如下:INSERTINTO表名VALU......