diff --git "a/14 \345\256\213\345\256\217\346\211\254/20230522\345\205\263\344\272\216servlet\347\232\204\344\275\234\344\270\232.md" "b/14 \345\256\213\345\256\217\346\211\254/20230522\345\205\263\344\272\216servlet\347\232\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..5cafc96367f6b44f478cabea3b2e518992fe6828 --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20230522\345\205\263\344\272\216servlet\347\232\204\344\275\234\344\270\232.md" @@ -0,0 +1,182 @@ +```java +import java.sql.*; +public class Jbdc { + private static final String url="jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String psw="root"; + public static Connection link() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.cj.jdbc.Driver"); + Connection conn = DriverManager.getConnection(url, username, psw); + return conn; + } + public static int currencyUpdate(String sql,String ...keys) throws SQLException, ClassNotFoundException { + Connection conn = link(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setString((i+1),keys[i]); + } + int i = prs.executeUpdate(); + return i; + } + public static int delete(String sql,String ...keys) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, keys); + return i; + } + public static int modify(String sql,String ...keys) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, keys); + return i; + } + public static int add(String sql,String ...keys) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, keys); + return i; + } + public static ResultSet Query(String sql,String ...keys) throws SQLException, ClassNotFoundException { + Connection conn = link(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setString((i+1),keys[i]); + } + ResultSet rs = prs.executeQuery(); + return rs; + } +} +//查找 +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("/select") +public class select 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 = Jbdc.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 = Jbdc.add(sql, "4", "小叶", "男"); + 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 = Jbdc.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); + } +} +//删除 +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 = Jbdc.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); + } +} +``` +