From b9e8e3c65f630ec7a27fbbdea428d9b0883b27b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=AD=E9=98=B3=E4=B8=BD?= <12071439+jieyangli@user.noreply.gitee.com> Date: Wed, 24 May 2023 16:08:37 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 揭阳丽 <12071439+jieyangli@user.noreply.gitee.com> --- ...71\346\237\245\344\275\234\344\270\232.md" | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 "37 \346\217\255\351\230\263\344\270\275/20230520 \345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" diff --git "a/37 \346\217\255\351\230\263\344\270\275/20230520 \345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" "b/37 \346\217\255\351\230\263\344\270\275/20230520 \345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" new file mode 100644 index 0000000..f0704c0 --- /dev/null +++ "b/37 \346\217\255\351\230\263\344\270\275/20230520 \345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" @@ -0,0 +1,166 @@ +```mysql +create database student_db charset utf8; +use student_db; +create table studetn{ + id int, + name varchar(5), + sex varchar(2) + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + select * from student; +``` + +增删改查, + +```java +import java.sql.*; + public class hhh { + private static final String url="jdbc:mysql:///test?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String password="root"; + static PreparedStatement ps=null; + static Connection conn=null; + static ResultSet re=null; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动有问题"); + e.printStackTrace(); + } + } + public static Connection conn() { + try { + conn = DriverManager.getConnection(url,username,password); + } catch (SQLException e) { + System.out.println("路径,用户名,密码错误请检查"); + } + return conn; + } + public static ResultSet select(String sql,String ...values){ + conn(); + ResultSet re = null; + try { + ps = conn().prepareStatement(sql); + for (int i = 0; i < values.length; i++) { + ps.setObject((i+1),values[i]); + } + re=ps.executeQuery(); + } catch (SQLException e) { + System.out.println("sql语句有问题"); + e.printStackTrace(); + } + return re; + } + public static int update(String sql,String ...values){ + conn(); + int i=0; + try { + ps= conn().prepareStatement(sql); + for (int a = 0; a < values.length; a++) { + ps.setObject((a+1),values[a]); + } + i=ps.executeUpdate(); + } catch (SQLException e) { + System.out.println("sql语句有问题"); + e.printStackTrace(); + } + return i; + } + public static void close(){ + try { + ps.close(); + conn.close(); + re.close(); + } catch (SQLException e) { + System.out.println("无法关闭"); + e.printStackTrace(); + } + } + } + + + +``` + +Student类 + +```java +public class Student { + int id; + String name; + 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) { + Connection conn=h.conn(); + ArrayList list=new ArrayList<>(); + String sql="select * from student"; + ResultSet re=h.select(sql); + Student stu=new Student(); + try { + while (re.next()){ + int id= re.getInt(1); + String name=re.getString(2); + String sex= re.getString(3); + stu.setId(id); + stu.setName(name); + stu.setSex(sex); + list.add(stu); + } + } catch (SQLException e) { + System.out.println("wuy1"); + } + System.out.println(list); + + } +} + +``` + -- Gitee From 0c4fe7a8aee89bd625fa64e9a393127e32c362a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=AD=E9=98=B3=E4=B8=BD?= <12071439+jieyangli@user.noreply.gitee.com> Date: Wed, 24 May 2023 16:09:15 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 揭阳丽 <12071439+jieyangli@user.noreply.gitee.com> --- .../20230524\344\275\234\344\270\232.md" | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 "37 \346\217\255\351\230\263\344\270\275/20230524\344\275\234\344\270\232.md" diff --git "a/37 \346\217\255\351\230\263\344\270\275/20230524\344\275\234\344\270\232.md" "b/37 \346\217\255\351\230\263\344\270\275/20230524\344\275\234\344\270\232.md" new file mode 100644 index 0000000..c4a8c92 --- /dev/null +++ "b/37 \346\217\255\351\230\263\344\270\275/20230524\344\275\234\344\270\232.md" @@ -0,0 +1,204 @@ +```markdown +# 作业, + * 1 数据库相关的操作,使用封装的工具类 + * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作 + * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败 + * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作? + +``` + +数据库: + +```mysql +-- 创建数据库 +CREATE DATABASE student_db charset utf8; +-- 使用数据库 +use student_db; +-- 创建表格 +CREATE table student ( +id int, +name VARCHAR(12), +sex VARCHAR(12) +); +-- 插入数据 +INSERT into student VALUES +(1,'张三','男'), +(2,'李四','女'), +(3,'王五','男'); +-- 查询所有 +SELECT * from student; +``` + +工具类: + +```java +package servlet; +import java.sql.*; +public class DBUtil { + // Math System + // 数据库的主机名,端口号,数据库名,关闭SSL,采用Unicode字符集,并以Utf8为编码 + private static final String url = "jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8";// localhost:3306是默认的主机和端口号,可以省略掉 + private static final String user = "root"; // 用户名 + private static final String password = "root";// 密码 + + // 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册异常"); + e.printStackTrace();// 开发调试阶段,为了排查具体错误,要把详细的异常信息打印 + } + } + + // 获取连接对象 + public static Connection getConn() { + Connection conn = null;// alt+回车 + try { + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + System.out.println("获取连接对象失败"); + e.printStackTrace(); + } + return conn; + } + + // 一个通用查询方法,接收SQL语句,返回结果集 + public static ResultSet query(String sql, String... keys) { + // String sql = "update student set id=? , name =? , sex =? where id=?"; + // 形参前加... 就表示其是可变参数,就是数组 + // 获取连接对象 + ResultSet rs = null; + try { + Connection conn = getConn(); + // 建立预编译SQL的对象 + PreparedStatement pst = conn.prepareStatement(sql);// 将?号转成变量 + // 给每个变量赋值 + // 遍历可变参数 + for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + } + // 执行SQL + rs = pst.executeQuery(); + } catch (SQLException e) { + System.out.println("执行查询异常"); + e.printStackTrace(); + } + return rs; + } + + // ,一个通用更新的方法(删除,添加,修改), + public static int update(String sql, String... keys) { + // String sql = "update student set id=? , name =? , sex =? where id=?"; + // 形参前加... 就表示其是可变参数,就是数组 + // 获取连接对象 + int num = 0; + try { + Connection conn = getConn(); + // 建立预编译SQL的对象 + PreparedStatement pst = conn.prepareStatement(sql);// 将?号转成变量 + // 给每个变量赋值 + // 遍历可变参数 + for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + } + // 执行SQL + num = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return num; + + // + // + // 一个通用的关闭资源的方法 + + } +} +``` + +servlet + +```java +package servlet; + +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.io.PrintWriter; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet("/stu") +public class StudentServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8");//设置网页格式 + resp.setCharacterEncoding("utf-8");//设置网页编码 + resp.getWriter().write("你要响应的内容在这里"); + //通过响应对象创建一个PrintWriter对象 + PrintWriter out = resp.getWriter(); + //调用写的方法 + out.write("你发送了一个请求,我收到了!"); + //下面开始使用数据库的工具类,读取数据库所有学生的信息,并显示到浏览器 + //添加功能 + String sql2="insert into student values(4,'赵六','男')"; + int i=DBUtil.update(sql2); + + if (i>0){ + out.write("添加成功!"); + }else { + out.write("添加失败!"); + } + +//删除功能 +// String sql3="delete from student where id=1"; +// int i = DBUtil.update(sql3); +// if (i>0){ +// out.write("删除成功!"); +// }else { +// out.write("删除失败!"); +// } + +//修改功能 +// String sql4="update student set sex='女' where id=3"; +// int i = DBUtil.update(sql4); +// if (i>0){ +// out.write("修改成功!"); +// }else { +// out.write("修改失败!"); +// } + + + //查询功能 + String sql ="select * from student"; + ResultSet rs = DBUtil.query(sql); + out.write(""); + out.write(""); + try { + while (rs.next()){ + int id = rs.getInt(1); + String name = rs.getString(2); + String sex = rs.getString(3); + out.write("");//将信息写到浏览器 + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + out.write("
编号姓名性别
"+id+""+name+""+sex+"
"); + + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} + +``` + -- Gitee From 6489f7b69f1f4b370f7330e52aad3607cda07fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=AD=E9=98=B3=E4=B8=BD?= <12071439+jieyangli@user.noreply.gitee.com> Date: Wed, 24 May 2023 16:09:47 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=202023?= =?UTF-8?q?0520=20=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5=E4=BD=9C=E4=B8=9A.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\346\237\245\344\275\234\344\270\232.md" | 166 ------------------ 1 file changed, 166 deletions(-) delete mode 100644 "20230520 \345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" diff --git "a/20230520 \345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" "b/20230520 \345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" deleted file mode 100644 index 472c2ea..0000000 --- "a/20230520 \345\242\236\345\210\240\346\224\271\346\237\245\344\275\234\344\270\232.md" +++ /dev/null @@ -1,166 +0,0 @@ -```mysql -create database student_db charset utf8; -use student_db; -create table studetn{ - id int, - name varchar(5), - sex varchar(2) - ); - insert into student values - (1,'张三','男'), - (2,'李四','女'), - (3,'王五','男'); - select * from student; -``` - -增删改查 - -```java -import java.sql.*; - public class hhh { - private static final String url="jdbc:mysql:///test?useSSL=false&useUnicode=true&characterEncoding=utf8"; - private static final String username="root"; - private static final String password="root"; - static PreparedStatement ps=null; - static Connection conn=null; - static ResultSet re=null; - static { - try { - Class.forName("com.mysql.jdbc.Driver"); - } catch (ClassNotFoundException e) { - System.out.println("驱动有问题"); - e.printStackTrace(); - } - } - public static Connection conn() { - try { - conn = DriverManager.getConnection(url,username,password); - } catch (SQLException e) { - System.out.println("路径,用户名,密码错误请检查"); - } - return conn; - } - public static ResultSet select(String sql,String ...values){ - conn(); - ResultSet re = null; - try { - ps = conn().prepareStatement(sql); - for (int i = 0; i < values.length; i++) { - ps.setObject((i+1),values[i]); - } - re=ps.executeQuery(); - } catch (SQLException e) { - System.out.println("sql语句有问题"); - e.printStackTrace(); - } - return re; - } - public static int update(String sql,String ...values){ - conn(); - int i=0; - try { - ps= conn().prepareStatement(sql); - for (int a = 0; a < values.length; a++) { - ps.setObject((a+1),values[a]); - } - i=ps.executeUpdate(); - } catch (SQLException e) { - System.out.println("sql语句有问题"); - e.printStackTrace(); - } - return i; - } - public static void close(){ - try { - ps.close(); - conn.close(); - re.close(); - } catch (SQLException e) { - System.out.println("无法关闭"); - e.printStackTrace(); - } - } - } - - - -``` - -Student类 - -```java -public class Student { - int id; - String name; - 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) { - Connection conn=h.conn(); - ArrayList list=new ArrayList<>(); - String sql="select * from student"; - ResultSet re=h.select(sql); - Student stu=new Student(); - try { - while (re.next()){ - int id= re.getInt(1); - String name=re.getString(2); - String sex= re.getString(3); - stu.setId(id); - stu.setName(name); - stu.setSex(sex); - list.add(stu); - } - } catch (SQLException e) { - System.out.println("wuy1"); - } - System.out.println(list); - - } -} - -``` - -- Gitee