From c52a9884a2ae4cfe987a0f31edb72b942c260694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=BB=A4=E9=B9=8F?= <1963831974@qq.com> Date: Fri, 19 May 2023 12:20:48 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\213\217\344\273\244\351\271\217 JDBC.md" | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 "12 \350\213\217\344\273\244\351\271\217/20230517_12 \350\213\217\344\273\244\351\271\217 JDBC.md" diff --git "a/12 \350\213\217\344\273\244\351\271\217/20230517_12 \350\213\217\344\273\244\351\271\217 JDBC.md" "b/12 \350\213\217\344\273\244\351\271\217/20230517_12 \350\213\217\344\273\244\351\271\217 JDBC.md" new file mode 100644 index 0000000..93b0c8f --- /dev/null +++ "b/12 \350\213\217\344\273\244\351\271\217/20230517_12 \350\213\217\344\273\244\351\271\217 JDBC.md" @@ -0,0 +1,117 @@ +## 工具类 + +```java +import java.sql.*; +import java.util.Scanner; + +public class student { + public static Statement enroll(){ + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + Connection conn = DriverManager.getConnection(url, "root", "root"); + Statement stmt = conn.createStatement(); + return stmt; + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static void select(Statement stmt){ + String sql = "select * from student"; + try { + ResultSet rs = stmt.executeQuery(sql); + while (rs.next()) { + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id+" "+name+" "+sex); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int insert(Scanner sc,Statement stmt){ + try { + System.out.println("请输入要添加的学号"); + int id = sc.nextInt(); + System.out.println("请输入要添加的姓名"); + String name = sc.next(); + System.out.println("请输入添加人的性别"); + String sex = sc.next(); + String stu = "("+id+",'"+name+"','"+sex+"')"; + String sql = "insert into student values"+stu; + int i = stmt.executeUpdate(sql); + return i; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int delete(Scanner sc,Statement stmt){ + try { + System.out.println("请输入要删除学生的学号"); + int id = sc.nextInt(); + String sql = "delete from student where id="+id; + int i = stmt.executeUpdate(sql); + return i; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int update(Scanner sc,Statement stmt){ + try { + System.out.println("请输入要修改学生的学号"); + int id = sc.nextInt(); + System.out.println("请输入修改后的学生姓名"); + String name = sc.next(); + System.out.println("请输入修改后的学生性别"); + String sex = sc.next(); + String sql = "update student set name='"+name+"',sex='"+sex+"'where id="+id; + int i = stmt.executeUpdate(sql); + return i; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +## 测试类 + +```java +import java.sql.Statement; +import java.util.Scanner; + +public class works { + public static void main(String[] args) { + student stu = new student(); + Scanner sc = new Scanner(System.in); + Statement stmt = stu.enroll(); + while (true) { + System.out.println("请选择你要使用的功能\n" + + "1.查询表数据 2.添加表数据 3.删除表数据 4.修改数据 5.退出系统"); + int num = sc.nextInt(); + switch (num) { + case 1: + stu.select(stmt); + break; + case 2: + System.out.println("添加成功"+stu.insert(sc,stmt)+"条数据"); + break; + case 3: + System.out.println("删除成功"+stu.delete(sc,stmt)+"条数据"); + break; + case 4: + System.out.println("修改成功"+stu.update(sc,stmt)+"条数据"); + break; + case 5: + System.exit(0); + default: + System.out.println("输入错误请重新输入"); + } + } + } +} +``` + -- Gitee From 1b9cbbd207bf9f95c99810f2f5b17d970d7d25da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=BB=A4=E9=B9=8F?= <1963831974@qq.com> Date: Sun, 21 May 2023 22:48:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\351\271\217 \345\260\201\350\243\205.md" | 175 ++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 "12 \350\213\217\344\273\244\351\271\217/20230520_12 \350\213\217\344\273\244\351\271\217 \345\260\201\350\243\205.md" diff --git "a/12 \350\213\217\344\273\244\351\271\217/20230520_12 \350\213\217\344\273\244\351\271\217 \345\260\201\350\243\205.md" "b/12 \350\213\217\344\273\244\351\271\217/20230520_12 \350\213\217\344\273\244\351\271\217 \345\260\201\350\243\205.md" new file mode 100644 index 0000000..8e720dd --- /dev/null +++ "b/12 \350\213\217\344\273\244\351\271\217/20230520_12 \350\213\217\344\273\244\351\271\217 \345\260\201\350\243\205.md" @@ -0,0 +1,175 @@ +## 工具类 + +```java +package Test; + +import java.sql.*; + +public class DBUtil { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static final String url = "jdbc:mysql://localhost:3306/student?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username = "root"; + private static final String password = "root"; + + public static Connection register() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, password); + return conn; + } + public static int Update(String sql,String... keys) throws SQLException { + Connection conn = register(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i+1),keys[i]); + } + int i = pst.executeUpdate(); + if (i>0){ + return i; + } + return 0; + } + public static ResultSet Query(String sql,String... keys) throws SQLException { + Connection conn = register(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i+1),keys[i]); + } + ResultSet rs = pst.executeQuery(); + return rs; + } + public static void close(Connection conn,PreparedStatement pst,ResultSet rs) throws SQLException { + if (rs!=null){ + rs.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } +} +``` + +## 管理系统 + +```java +package Test; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Scanner; + +public class StudentDB { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("----欢迎来到**学生管理系统----\n" + + "\t请选择你需要的功能\n" + + "\t1.查看所有学生\n" + + "\t2.添加学生信息\n" + + "\t3.删除学生信息\n" + + "\t4.修改学生信息\n" + + "\t5.退出管理系统\n"); + while (true) { + System.out.println("请选择你需要的功能"); + int num = sc.nextInt(); + try { + switch (num){ + case 1: + select(sc); + System.out.println("-----------------------"); + break; + case 2: + insert(sc); + System.out.println("-----------------------"); + break; + case 3: + delete(sc); + System.out.println("-----------------------"); + break; + case 4: + update(sc); + System.out.println("-----------------------"); + break; + case 5: + System.out.println("已成功退出**学生管理系统,请支付试用费共记200元,欢迎下次使用"); + System.exit(0); + default: + System.out.println("输入错误请重新输入"); + System.out.println("-----------------------"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + public static void select(Scanner sc) throws SQLException { + System.out.println("1.查看所有学生信息 2.查看指定学号学生信息"); + int num = sc.nextInt(); + String sql = null; + ResultSet rs = null; + switch (num) { + case 1: + sql = "select * from stu"; + rs = DBUtil.Query(sql); + break; + case 2: + sql = "select * from stu where id = ?"; + System.out.println("请输入查询学生的学号"); + String id = sc.next(); + rs = DBUtil.Query(sql,id); + break; + default: + System.out.println("输入错误请重新输入"); + break; + } + while (rs.next()) { + int id = rs.getInt("id"); + String name = rs.getString("name"); + int age = rs.getInt("age"); + String sex = rs.getString("sex"); + System.out.println("学号" + id + ",姓名" + name + ",年龄" + age + ",性别" + sex); + } + DBUtil.close(null, null, rs); + } + public static void insert(Scanner sc) throws SQLException { + String sql = "insert into stu values(?,?,?,?)"; + System.out.println("请输入添加学生的学号"); + String id = sc.next(); + System.out.println("请输入添加学生的姓名"); + String name = sc.next(); + System.out.println("请输入添加学生的年龄"); + String age = sc.next(); + System.out.println("请输入添加学生的性别"); + String sex = sc.next(); + int i = DBUtil.Update(sql, id, name, age, sex); + System.out.println("成功添加"+i+"条信息"); + } + public static void delete(Scanner sc) throws SQLException { + System.out.println("请输入要删除的学生学号"); + String id = sc.next(); + String sql = "delete from stu where id = ?"; + int i = DBUtil.Update(sql, id); + System.out.println("成功删除"+i+"条信息"); + } + public static void update(Scanner sc) throws SQLException { + System.out.println("请输入要修改信息的学生学号"); + String id = sc.next(); + String sql = "update stu set name =?,age =?,sex =? where id =?"; + System.out.println("请输入修改后的姓名"); + String name = sc.next(); + System.out.println("请输入修改后的年龄"); + String age = sc.next(); + System.out.println("请输入修改后的性别"); + String sex = sc.next(); + int i = DBUtil.Update(sql, name, age, sex, id); + System.out.println("成功修改"+i+"条信息"); + } +} +``` + -- Gitee