From cc7b35554f5134a292d6b209b4379ad117df5136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AC=A3?= <3188923799@qq.com> Date: Mon, 22 May 2023 00:49:58 +0800 Subject: [PATCH 1/2] tree --- .../20230521 \344\275\234\344\270\232.md" | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 "31 \346\235\216\346\254\243/20230521 \344\275\234\344\270\232.md" diff --git "a/31 \346\235\216\346\254\243/20230521 \344\275\234\344\270\232.md" "b/31 \346\235\216\346\254\243/20230521 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..c403197 --- /dev/null +++ "b/31 \346\235\216\346\254\243/20230521 \344\275\234\344\270\232.md" @@ -0,0 +1,117 @@ + 使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库, + 实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历之个集合的对象 + +```java +import org.omg.CORBA.UserException; + +import java.sql.*; +import java.util.ArrayList; + +public class MysqlTest { + static String url ="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + static String username = "root"; + static String password = "1234"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getConn() throws SQLException { + //2 获取连接的对象conn + Connection conn = DriverManager.getConnection(url,username,password);// Alt+回车 + return conn; + } + public static ResultSet Query(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pr = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pr.setString((i+1),keys[i]); + } + ResultSet re = pr.executeQuery(); + return re; + } + public static void Close(Connection conn,PreparedStatement pst,ResultSet rst) throws SQLException { + if (rst!=null){ + rst.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } + +} +class Student{ + private int id; + private String name; + private String sex; + + public Student() { + + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } + + 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; + } +} +class Test{ + public static void main(String[] args) throws SQLException { + /* + 使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库, + 实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历之个集合的对象 + */ + ArrayList list = new ArrayList<>(); + String sql ="select * from student"; + ResultSet resultSet = MysqlTest.Query(sql); + while (resultSet.next()){ + Student st = new Student(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3) ); + list.add(st); + } + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + MysqlTest.Close(null,null,resultSet); + } +} + +``` + -- Gitee From 860487e51f6d34925b4e3180b326ffc803380b1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AC=A3?= <3188923799@qq.com> Date: Tue, 23 May 2023 22:37:34 +0800 Subject: [PATCH 2/2] tree --- .../20230523 \344\275\234\344\270\232.md" | 226 ++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 "31 \346\235\216\346\254\243/20230523 \344\275\234\344\270\232.md" diff --git "a/31 \346\235\216\346\254\243/20230523 \344\275\234\344\270\232.md" "b/31 \346\235\216\346\254\243/20230523 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..e7bcc72 --- /dev/null +++ "b/31 \346\235\216\346\254\243/20230523 \344\275\234\344\270\232.md" @@ -0,0 +1,226 @@ +```markdown +# 作业, + * 1 数据库相关的操作,使用封装的工具类 + * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作 + * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败 + * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作? + +``` + +```java +package servle; +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; + + /* + # 作业, + * 1 数据库相关的操作,使用封装的工具类 + * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作 + * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败 + * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作? + + */ + @WebServlet("/select") + public class SelectServlet 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 re = DBUtil.query(sql); + System.out.println("编号" + "" + "姓名" + "" + "性别"); + while (re.next()){ + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id+name+sex); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + } + + + +``` + +```java +package servle; + + + import java.sql.*; + + public class DBUtil { + private static final String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String password = "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, username, password); + return conn; + } + public static ResultSet query(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pr = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pr.setString((i+1),keys[i]); + } + ResultSet re = pr.executeQuery(); + return re; + } + public static int update(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pr = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pr.setString((i + 1), keys[i]); + } + int i = pr.executeUpdate(); + return i; + } + public static void close(Connection conn,PreparedStatement pst,ResultSet rst) throws SQLException { + if (rst != null) { + rst.close(); + } + if (pst != null) { + pst.close(); + } + if (conn != null) { + conn.close(); + } + } + + } + + + +``` + +```java +package servle; + +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("/insert") +public class InsertServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + String sql= "insert into student values (0,'cc',1)"; + int i = DBUtil.update(sql); + if (i>0){ + System.out.println("添加成功"); + }else{ + System.out.println("添加失败了"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} + +``` + +```java +package servle; + +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("/update") +public class Update extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + String sql= "update student set name=aa where id=4"; + int i = DBUtil.update(sql); + if (i>0){ + System.out.println("修改成功"); + }else{ + System.out.println("修改失败了"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} + +``` + +```java +package servle; + +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 DeleteServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + String sql= "delete from student set name='aa' where id=4"; + int i = DBUtil.update(sql); + if (i>0){ + System.out.println("删除成功"); + }else{ + System.out.println("删除失败了"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} + + +``` + -- Gitee