From 6364d0e8806011c6ca370921e0b1fe1ba2dff472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=88=90=E9=B8=BF?= <3157164764@qq,com> Date: Sun, 11 Jun 2023 21:04:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230610 JDBC\344\275\234\344\270\232.md" | 473 ++++++++++++++++++ 1 file changed, 473 insertions(+) create mode 100644 "16 \346\236\227\346\210\220\351\270\277/20230610 JDBC\344\275\234\344\270\232.md" diff --git "a/16 \346\236\227\346\210\220\351\270\277/20230610 JDBC\344\275\234\344\270\232.md" "b/16 \346\236\227\346\210\220\351\270\277/20230610 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..e2b0319 --- /dev/null +++ "b/16 \346\236\227\346\210\220\351\270\277/20230610 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,473 @@ +```java +package bean; + +public class Dept { + private int dId; + private String dName; + + public Dept() { + } + + public Dept(int dId, String dName) { + this.dId = dId; + this.dName = dName; + } + + public int getdId() { + return dId; + } + + public void setdId(int dId) { + this.dId = dId; + } + + public String getdName() { + return dName; + } + + public void setdName(String dName) { + this.dName = dName; + } + + @Override + public String toString() { + return "Dept{" + + "dId=" + dId + + ", dName='" + dName + '\'' + + '}'; + } +} + +package bean; + +public class Emp { + private int eId; + private String eName; + private String sex; + private int age; + private String tel; + private String pass; + private int dId; + + public Emp() { + } + + public Emp(int eId, String eName, String sex, int age, String tel, String pass, int dId) { + this.eId = eId; + this.eName = eName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.pass = pass; + this.dId = dId; + } + + public int geteId() { + return eId; + } + + public void seteId(int eId) { + this.eId = eId; + } + + public String geteName() { + return eName; + } + + public void seteName(String eName) { + this.eName = eName; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getTel() { + return tel; + } + + public void setTel(String tel) { + this.tel = tel; + } + + public String getPass() { + return pass; + } + + public void setPass(String pass) { + this.pass = pass; + } + + public int getdId() { + return dId; + } + + public void setdId(int dId) { + this.dId = dId; + } + + @Override + public String toString() { + return "Emp{" + + "eId=" + eId + + ", eName='" + eName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", pass='" + pass + '\'' + + ", dId=" + dId + + '}'; + } +} + +``` + +```java +package util; + +import java.sql.*; + +public class DBUtil { //characterEncoding=utf8 + private static final String url="jdbc:mysql:///companyManager?characterEncoding=utf8"; + private static final String user="root"; + private static final String pow="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getConn () throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pow); + return conn; + } + public static ResultSet getQuery(String sql,Object ...flop) throws SQLException { + Connection conn = getConn(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setObject((i+1),flop[i]); + } + ResultSet rs = prs.executeQuery(); + return rs; + } + public static int getUpdate (String sql,Object ...flop) throws SQLException { + Connection conn = getConn(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setObject((i+1),flop[i]); + } + int i = prs.executeUpdate(); + return i; + } + public static void close(Connection conn,PreparedStatement prs,ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (prs!=null){ + prs.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java +package servlet; + +import bean.Emp; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/list") +public class list extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from emp e,Dept d where d.DeptID=e.DepId"; + ResultSet rs = null; + try { + rs = DBUtil.getQuery(sql); + while (rs.next()) { + int eId = rs.getInt(1); + String name = rs.getString(2); + String sex = rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String pass = rs.getString(6); + int id = rs.getInt(7); + list.add(new Emp(eId, name, sex, age, tel, pass, id)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null, null, rs); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String name = request.getParameter("name"); + String sql = "select * from emp e,Dept d where d.DeptID=e.DepId and e.EmpName like ?"; + ResultSet rs = null; + try { + rs = DBUtil.getQuery(sql, "%"+name+"%"); + while (rs.next()) { + int eId = rs.getInt(1); + String eName = rs.getString(2); + String sex = rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String pass = rs.getString(6); + int id = rs.getInt(7); + list.add(new Emp(eId,eName,sex,age,tel,pass,id)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null, null, rs); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response); + } +} + +package servlet; + +import bean.Dept; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/add") +public class add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from dept"; + ResultSet rs = null; + try { + rs = DBUtil.getQuery(sql); + while (rs.next()) { + int eId = rs.getInt(1); + String eName = rs.getString(2); + list.add(new Dept(eId, eName)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null, null, rs); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + if (sex.equals("male")){ + sex="男"; + } else if (sex.equals("female")) { + sex="女"; + } + int age = Integer.parseInt(request.getParameter("age")); + String tel = request.getParameter("tel"); + String pass = request.getParameter("pass"); + String dId = request.getParameter("dId"); + String sql ="insert into emp values (?,?,?,?,?,?,?)"; + int i = 0; + try { + i = DBUtil.getUpdate(sql, 0, name, sex, age, tel, pass, dId); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (i>0){ + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + }else { + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} + +``` + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 15:29 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 首页 + + + +
+ + + + +
姓名:
+
+添加员工 + + + + + + + + +<%-- --%> + + + + + + + + + --%> +<%-- --%> +<%-- --%> + +
编号姓名性别年龄电话所属部门操作
${bar.eId}${bar.eName}${bar.sex}${bar.age}${bar.tel} + 开发部 + + + 测试部 + + + UI部 + +<%-- 修改删除
+ + + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:29 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加员工 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
姓名
性别男 + 女 +
年龄
电话
密码
所属部门 + +
+
+ + + +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 17:11 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示 + + +${msg} +
+返回首页 + + + +``` + -- Gitee