From 4aec2755f18fc864ecddd782ae65e9f6f0b4a7f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E4=BA=9A=E8=BE=89?= <3192667214@qq.com> Date: Wed, 17 May 2023 13:25:02 +0000 Subject: [PATCH] =?UTF-8?q?41=20=E5=91=A8=E4=BA=9A=E8=BE=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周亚辉 <3192667214@qq.com> --- .../20230517 JDBC\344\275\234\344\270\232.md" | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 "41 \345\221\250\344\272\232\350\276\211/20230517 JDBC\344\275\234\344\270\232.md" diff --git "a/41 \345\221\250\344\272\232\350\276\211/20230517 JDBC\344\275\234\344\270\232.md" "b/41 \345\221\250\344\272\232\350\276\211/20230517 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..5f5d46e --- /dev/null +++ "b/41 \345\221\250\344\272\232\350\276\211/20230517 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,151 @@ + + +# 作业 + +## mysql + +```mysql +CREATE DATABASE student_db CHARSET utf8; +use student_db; +CREATE TABLE student( +id int, +name VARCHAR(20), +sex enum('男','女')); +INSERT into student VALUES( +1,'张三','男'), +(2,'李四','女'), +(3,'王五','男'); +``` + +## java + +```java +import java.sql.*; +import java.util.Scanner; + +public class test { + static Connection conn = null; + static Statement sta = null; + static ResultSet res = null; + + static Scanner sc = new Scanner(System.in); + + public static void main(String[] args) { + try { + OUT: + while (true) { + Class.forName("com.mysql.jdbc.Driver"); + conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + sta = conn.createStatement(); + System.out.println("1. 查询功能\n2. 添加功能\n3. 修改功能\n4. 删除功能\n退出"); + System.out.println("输入"); + int i = sc.nextInt(); + switch (i) { + case 1: + select(sta); + break; + case 2: + add(sta); + break; + case 3: + update(sta); + break; + case 4: + delete(sta); + break; + case 5: + break OUT; + } + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + try { + sta.close(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + private static void delete(Statement sta) { + try { + int a = sta.executeUpdate("delete from student where id=3"); + if (a > 0) { + System.out.println("ok"); + } else { + System.out.println("no"); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + + } + } + + private static void update(Statement sta) { + try { + int a = sta.executeUpdate("update student set name='张二' where id=1"); + if (a > 0) { + System.out.println("ok"); + } else { + System.out.println("no"); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + + } + } + + private static void add(Statement sta) { + try { + int a = sta.executeUpdate("INSERT into student VALUES(1,'小明','男')"); + if (a > 0) { + System.out.println("ok"); + } else { + System.out.println("no"); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + + } + } + + static void select(Statement sta) { + try { + res = sta.executeQuery("select * from student;"); + while (res.next()) { + System.out.println(res.getInt("id") + res.getString("name") + res.getString("sex")); + } + } catch (SQLException e) { + + } finally { + try { + res.close(); + } catch (SQLException e) { + + } + } + } +} + +class stu { + int id; + String name; + String sex; + + @Override + public String toString() { + return "id=" + id + + " name='" + name + + " sex='" + sex; + } +} + +``` + -- Gitee