From 3b3f3dcd02aba868264fb6ec51701ef8baf8f3f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=BE=90=E6=B0=B8=E6=B7=B3?= <2678158018@qq.com>
Date: Wed, 24 May 2023 23:17:24 +0800
Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C=E4=B8=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...56\345\272\223\344\275\234\344\270\232.md" | 198 ++++++++++++++++++
1 file changed, 198 insertions(+)
create mode 100644 "18 \345\276\220\346\260\270\346\267\263/20230524 jsp+servlet web\346\223\215\344\275\234\346\225\260\346\215\256\345\272\223\344\275\234\344\270\232.md"
diff --git "a/18 \345\276\220\346\260\270\346\267\263/20230524 jsp+servlet web\346\223\215\344\275\234\346\225\260\346\215\256\345\272\223\344\275\234\344\270\232.md" "b/18 \345\276\220\346\260\270\346\267\263/20230524 jsp+servlet web\346\223\215\344\275\234\346\225\260\346\215\256\345\272\223\344\275\234\344\270\232.md"
new file mode 100644
index 0000000..2dfb8fd
--- /dev/null
+++ "b/18 \345\276\220\346\260\270\346\267\263/20230524 jsp+servlet web\346\223\215\344\275\234\346\225\260\346\215\256\345\272\223\344\275\234\344\270\232.md"
@@ -0,0 +1,198 @@
+```java
+web类
+import until.Text;
+
+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("/Text")
+
+public class SS extends HttpServlet {
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ System.out.println("这是我数出语句");
+ resp.setContentType("Text/html;charset=utf-8");
+ PrintWriter out = resp.getWriter();
+ out.write("你接受到了一条消息");
+ out.write("
");
+ out.write("| 编号 | 姓名 | 性别 |
");
+ //查询
+ String sql="select * from studnet";
+ ResultSet qure = Text.qure(sql);
+ try {
+ while (qure.next()){
+ int id = qure.getInt("id");
+ String name = qure.getString("name");
+ String sex = qure.getString("sex");
+ out.write("| "+id+" | "+""+name+" | "+""+sex+" |
");
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ out.write("
");
+ //增添
+// String sql1="insert into studnet value(10,'牛蚊疯','妖魔鬼怪大傻叉')";
+// try {
+// int insert = Text.insert(sql1);
+// out.write("添加成功!");
+// } catch (Exception e) {
+// out.write("添加失败!");
+// }
+ //修改
+// String sql2="update studnet set name='刘文峰' where id=10";
+// try {
+// int update = Text.insert(sql2);
+// out.write("修改成功!");
+// } catch (Exception e) {
+// out.write("修改失败!");
+// }
+ //删除
+// String sql3="delete from studnet where id=1";
+// try {
+// int delete = Text.delete(sql3);
+// out.write("删除成功");
+// } catch (Exception e) {
+// out.write("删除成功");
+// }
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ super.doPost(req, resp);
+ }
+}
+```
+
+```java
+操作数据库类
+package HomeWork.HomeWork0524;
+
+
+import java.sql.*;
+
+public class Text {
+ private static final String url="jdbc:mysql:///aaa?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) {
+ System.out.println("云心");
+ }
+ }
+ public static Connection getcon() {
+ Connection con = null;
+ try {
+ con = DriverManager.getConnection(url, username, password);
+ } catch (SQLException e) {
+ System.out.println("连接异常'");
+ }
+ return con;
+ }
+ public static Connection conget(){
+ Connection con = null;
+ try {
+ con = DriverManager.getConnection(url, username, password);
+ } catch (SQLException e) {
+ System.out.println("连接异常");
+ }
+ return con;
+ }
+ public static ResultSet qure(String sql, String...keys) {//查询
+ ResultSet res = null;
+ try {
+ Connection con = conget();
+ PreparedStatement pre = con.prepareStatement(sql);
+ for (int i = 0; i < keys.length; i++) {
+ pre.setString((i+1),keys[i]);
+ }
+ res = pre.executeQuery();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ return res;
+ }
+ public static int insert(String sql,String...keys) {//添加
+ int num = 0;
+ PreparedStatement pre = null;
+ try {
+ Connection con = conget();
+ pre = con.prepareStatement(sql);
+ for (int i = 0; i < keys.length; i++) {
+ pre.setString((i + 1), keys[i]);
+ }
+ num = pre.executeUpdate();
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ try {
+ pre.close();
+ conget().close();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return num;
+ }
+ public static int update(String sql,String...keys) {//修改
+ int num = 0;
+ PreparedStatement pre = null;
+ try {
+ Connection con = conget();
+ pre = con.prepareStatement(sql);
+ for (int i = 0; i < keys.length; i++) {
+ pre.setString((i + 1), keys[i]);
+ }
+ num = pre.executeUpdate();
+ pre.close();
+ conget().close();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ try {
+ pre.close();
+ conget().close();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return num;
+ }
+ public static int delete(String sql,String...keys) {//删除
+ int num = 0;
+ PreparedStatement pre = null;
+ try {
+ Connection con = conget();
+ pre = con.prepareStatement(sql);
+ for (int i = 0; i < keys.length; i++) {
+ pre.setString((i + 1), keys[i]);
+ }
+ num = pre.executeUpdate();
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ try {
+ pre.close();
+ conget().close();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ return num;
+ }
+}
+
+```
+
--
Gitee