diff --git "a/07 \345\206\257\345\273\272\347\250\213/20230522 \344\275\234\344\270\232.txt" "b/07 \345\206\257\345\273\272\347\250\213/20230522 \344\275\234\344\270\232.txt" new file mode 100644 index 0000000000000000000000000000000000000000..fde9f9c4c0622b3db15c6881568c33d611958140 --- /dev/null +++ "b/07 \345\206\257\345\273\272\347\250\213/20230522 \344\275\234\344\270\232.txt" @@ -0,0 +1,267 @@ +# 作业, + * 1 数据库相关的操作,使用封装的工具类 + + + + ```java + package web; + + public class Student { + public Student(){} + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + private int id; + private String name; + private String 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; + } + public String toString(){ + return "id= "+id+" name= "+name+" sex= "+sex; + } + } + + ``` + + + + ```java + // 工具类 + package web; + + import java.sql.*; + + public class tool { + private static final String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=ture&characterEncoding=utf8"; + private static final String name = "name"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, name, name); + return conn; + } + + + + public static ResultSet select(String sql) throws SQLException { + Connection conn = getConn(); + PreparedStatement state = conn.prepareStatement(sql); + ResultSet res = state.executeQuery(); + close(conn, state, res); + return res; + } + + public static int DDL(String sql) throws SQLException { + Connection conn = getConn(); + PreparedStatement state = conn.prepareStatement(sql); + int res = state.executeUpdate(); + close(conn, state); + return res; + } + + public static void close(Connection con, PreparedStatement state, ResultSet res) throws SQLException { + if (res != null) { + res.close(); + } + if (state != null) { + state.close(); + } + if (con != null) { + con.close(); + } + + } + + public static void close(Connection con, PreparedStatement state) throws SQLException { + if (state != null) { + state.close(); + } + if (con != null){ + con.close(); + } + } + } + + ``` + + + + * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作 + + * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败 + + + + ```java + package web; + + import javax.jws.WebService; + 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; + import java.util.ArrayList; + + + @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("欢迎使用"); + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + } + @WebServlet("Select") + class StudentSelect extends HttpServlet{ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "Select * from student"; + ArrayList info = new ArrayList<>(); + try { + ResultSet res = tool.select(sql); + while (res.next()){ + int id = res.getInt("id"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(id,name,sex)); + } + for (Student student : info) { + resp.getWriter().write(student.toString()+"\n"); + } + + } + catch (SQLException e){ + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req,resp); + } + } + @WebServlet("/add") + class StudentAdd extends HttpServlet{ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "insert into student values(4,'小花','女')"; + try{ + int res = tool.DDL(sql); + if (res<0){ + resp.getWriter().write("失败"); + } + else { + resp.getWriter().write("成功"); + } + + } + catch (SQLException e){ + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + } + @WebServlet("/update") + class StudentUpdate extends HttpServlet{ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "Update student set name = '小红' where name = '小花' "; + try{ + int res = tool.DDL(sql); + if (res > 0){ + resp.getWriter().write("成功"); + } + if (res < 0){ + resp.getWriter().write("失败"); + } + + } + catch(SQLException e){ + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + } + @WebServlet("/delete") + class StudentDelete extends HttpServlet{ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "Delete from student where name = '小红' "; + try{ + int res = tool.DDL(sql); + if (res > 0){ + resp.getWriter().write("成功"); + } + if (res < 0){ + resp.getWriter().write("失败"); + } + + } + catch(SQLException e){ + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + } + + + ``` + + * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作? +\ No newline at end of file \ No newline at end of file