From 76a6c0a6864aaed17737e158a61c458c2c179ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BF=8A=E5=93=B2?= <2361967167@qq.com> Date: Tue, 23 May 2023 21:57:03 +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 --- .../20230520 \344\275\234\344\270\232.md" | 115 ----------- .../20230523\344\275\234\344\270\232.md" | 195 ++++++++++++++++++ 2 files changed, 195 insertions(+), 115 deletions(-) delete mode 100644 "24 \351\231\210\344\277\212\345\223\262/20230520 \344\275\234\344\270\232.md" create mode 100644 "24 \351\231\210\344\277\212\345\223\262/20230523\344\275\234\344\270\232.md" diff --git "a/24 \351\231\210\344\277\212\345\223\262/20230520 \344\275\234\344\270\232.md" "b/24 \351\231\210\344\277\212\345\223\262/20230520 \344\275\234\344\270\232.md" deleted file mode 100644 index 30348f6..0000000 --- "a/24 \351\231\210\344\277\212\345\223\262/20230520 \344\275\234\344\270\232.md" +++ /dev/null @@ -1,115 +0,0 @@ -```java -//JDBC工具类 -import java.sql.*; - -public class Jdbc { - //注册 - static - { - try { - Class.forName("com.mysql.jdbc.Driver"); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } - } - //连接 - private Connection conn() { - try { - String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEchoing=utf8"; - Connection conn = DriverManager.getConnection(url, "root", "sxxddytdn"); - return conn; - } catch (SQLException e) { - throw new RuntimeException(e); - } - } - - //查询 - ResultSet select(String sql){ - try { - PreparedStatement pst = conn().prepareStatement(sql); - ResultSet re = pst.executeQuery(); - return re; - } catch (SQLException e) { - throw new RuntimeException(e); - } - } -} - -``` - -```java -//学生对象 -public class Student { - private int id; - private String name; - private String sex; - - public Student() { - } - - public Student(int id, String name, String sex) { - this.id = id; - this.name = name; - this.sex = sex; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getSex() { - return sex; - } - - public void setSex(String sex) { - this.sex = sex; - } -} -``` - -```java -//测试类 -import java.sql.*; -import java.util.ArrayList; - -public class Test { - public static void main(String[] args) { - Jdbc jdbc = new Jdbc(); - ResultSet re = jdbc.select("select * from student"); - ArrayList stu = new ArrayList<>(); - try { - while (re.next()) { - int id = re.getInt(1); - String name = re.getString(2); - String sex = re.getString(3); - Student s = new Student(id, name, sex); - stu.add(s); - } - re.close(); - } catch (SQLException e) { - throw new RuntimeException(e); - } - - System.out.println("ID\t\t姓名\t\t性别"); - for (int i = 0; i < stu.size(); i++) { - Student s = stu.get(i); - System.out.println(s.getId()+"\t\t"+s.getName()+"\t\t"+s.getSex()); - } - } -} -``` - - - diff --git "a/24 \351\231\210\344\277\212\345\223\262/20230523\344\275\234\344\270\232.md" "b/24 \351\231\210\344\277\212\345\223\262/20230523\344\275\234\344\270\232.md" new file mode 100644 index 0000000..6da1d27 --- /dev/null +++ "b/24 \351\231\210\344\277\212\345\223\262/20230523\344\275\234\344\270\232.md" @@ -0,0 +1,195 @@ +````java +# 作业, + * 1 数据库相关的操作,使用封装的工具类 + * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作 + * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败 + * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作? + +``` + +工具类 + +```java +import java.sql.*; +public class DbJbdc { + private static final String url="jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String user="root"; + private static final String pass="root"; + public static Connection link() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.cj.jdbc.Driver"); + Connection conn = DriverManager.getConnection(url, user, pass); + return conn; + } + public static int currencyUpdate(String sql,String ...flop) throws SQLException, ClassNotFoundException { + Connection conn = link(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setString((i+1),flop[i]); + } + int i = prs.executeUpdate(); + return i; + } + public static int delete(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static int modify(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static int add(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static ResultSet Query(String sql,String ...flop) throws SQLException, ClassNotFoundException { + Connection conn = link(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setString((i+1),flop[i]); + } + ResultSet rs = prs.executeQuery(); + return rs; + } +} +``` + +```java +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.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +@WebServlet("/seek") +public class Seek extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + try { + String sql ="select * from student"; + ResultSet rs = DbJbdc.Query(sql); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + resp.getWriter().write(id+" "+name+" "+sex+"
"); + } + } catch (SQLException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +----------------------------------------------------------- +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.IOException; +import java.sql.SQLException; +@WebServlet("/add") +public class Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + try { + String sql ="insert into student values (?,?,?)"; + int add = DbJbdc.add(sql, "0", "小明", "男"); + if (add>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +------------------------------------------------------------ +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.IOException; +import java.sql.SQLException; +@WebServlet("/delete") +public class Delete extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + try { + String sql ="delete from student where id=?"; + int add = DbJbdc.add(sql, "1"); + if (add>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +------------------------------------------------------------ +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.IOException; +import java.sql.SQLException; +@WebServlet("/modify") +public class Modify extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + try { + String sql ="update student set sex=? where id=?"; + int add = DbJbdc.add(sql, "女","2"); + if (add>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +```` + -- Gitee