表依旧是上上次的表~
然后是目录结构
然后……贴代码!
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"%>标签:凑齐,preparedStatement,name,改查,public,connection,增删,id,String From: https://www.cnblogs.com/yansans/p/16824661.html
<%@ 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>