From eb3409fbe8bd01f229b666407801c8d882aba2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E6=A2=93=E9=91=AB?= <1813169067@qq.com> Date: Sun, 21 May 2023 23:55:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230520.md" | 181 ++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 "27\351\222\237\346\242\223\351\221\253/20230520.md" diff --git "a/27\351\222\237\346\242\223\351\221\253/20230520.md" "b/27\351\222\237\346\242\223\351\221\253/20230520.md" new file mode 100644 index 0000000..2f5400c --- /dev/null +++ "b/27\351\222\237\346\242\223\351\221\253/20230520.md" @@ -0,0 +1,181 @@ +建表 +create database student_db charset utf8; +use Student_db; +create table student (id int PRIMARY KEY AUTO_INCREMENT,`name` varchar(5),sex char(2)); +insert into student values + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); +工具类 +import java.sql.*; + +public class Tool { + /* + 使用新的执行SQL的对象,封装一个工具类, + 然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象, + 最后存入一个集合窗口,遍历之个集合的对象 + */ + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding"; + private static String username = "root"; + private static String password = "shy666nb"; + public static Connection co() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, password); + return conn; + } + public static ResultSet query(String sql,String ...keys) throws SQLException { + PreparedStatement pr = co().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pr.setString((i+1),keys[i]); + } + ResultSet re = pr.executeQuery(); + return re; + } + public static int update(String sql,String ...keys) throws SQLException { + Connection conn = co(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i+1),keys[i]); + } + int i = pst.executeUpdate(); + return i; + } + public static void close(Connection conn,PreparedStatement pst,ResultSet rst) throws SQLException { + if (rst!=null){ + rst.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } + +} +测试类 +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Scanner; + +public class Test { + public static void main(String[] args) throws SQLException { + System.out.println("欢迎进入系统!" + + "\n1.查询全部信息" + + "\n2.查询个别信息" + + "\n3.添加信息" + + "\n4.修改信息" + + "\n5.删除信息" + + "\n6.退出系统"); + Scanner sc = new Scanner(System.in); + while (true) { + System.out.println("请输入要选择的功能选项:"); + String i = sc.nextLine(); + switch (i) { + case "1": + System.out.println("您选择了查询全部信息"); + allSelect(); + break; + case "2": + System.out.println("您选择了查询个别信息"); + select(); + break; + case "3": + System.out.println("您选择了添加信息"); + add(); + break; + case "4": + System.out.println("您选择了修改信息"); + update(); + break; + case "5": + System.out.println("您选择了删除信息"); + delete(); + break; + case "6": + System.out.println("您选择了退出系统"); + return; + default: + System.out.println("请输入正确的功能选项!"); + break; + } + } + } + private static void allSelect() throws SQLException { + String sql = "select * from student"; + ResultSet rs = Tool.query(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); + } + Tool.close(null,null,rs); + } + private static void select() throws SQLException { + Scanner sc = new Scanner(System.in); + System.out.print("请输入编号:"); + String userid=sc.nextLine(); + System.out.print("请输入姓名:"); + String username=sc.nextLine(); + String sql = "select * from student where id=? and name=?"; + String[] keys= {userid,username}; + ResultSet rs = Tool.query(sql,keys); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id+","+name+","+sex); + } + Tool.close(null,null,rs); + } + private static void add() throws SQLException { + Scanner sc = new Scanner(System.in); + System.out.print("请输入姓名:"); + String username=sc.nextLine(); + System.out.print("请输入性别:"); + String usersex=sc.nextLine(); + String sql2= "insert into student values (null,?,?)"; + int i = Tool.update(sql2,username,usersex); + if (i>0){ + System.out.println("添加成功"); + }else{ + System.out.println("添加失败了"); + } + } + private static void update() throws SQLException { + Scanner sc = new Scanner(System.in); + System.out.print("请输入修改后姓名:"); + String username=sc.nextLine(); + System.out.print("请输入修改前编号:"); + String userid=sc.nextLine(); + String sql2= "update student set name=? where id=?"; + int i = Tool.update(sql2,username,userid); + if (i>0){ + System.out.println("修改成功"); + }else{ + System.out.println("修改失败了"); + } + } + private static void delete() throws SQLException { + Scanner sc = new Scanner(System.in); + System.out.print("请输入编号:"); + String userid = sc.nextLine(); + System.out.print("请输入姓名:"); + String username = sc.nextLine(); + String sql2 = "delete from student where id=? and name=?"; + int i = Tool.update(sql2,userid,username); + if (i > 0) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败了"); + } + } +} + -- Gitee