From 53c2eee964576830206d78209590f70381e3a6c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BF=B5=E5=BF=B5?= <2324752546@qq.com> Date: Sat, 20 May 2023 11:45:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230517 JDBC\344\275\234\344\270\232.md" | 197 ++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 "47 \346\235\216\345\277\265\345\277\265/20230517 JDBC\344\275\234\344\270\232.md" diff --git "a/47 \346\235\216\345\277\265\345\277\265/20230517 JDBC\344\275\234\344\270\232.md" "b/47 \346\235\216\345\277\265\345\277\265/20230517 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..f8f2449 --- /dev/null +++ "b/47 \346\235\216\345\277\265\345\277\265/20230517 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,197 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + + ~~~ mysql + CREATE DATABASE student_db CHARSET utf8; + USE student_db; + CREATE TABLE student ( + id int(11) NOT NULL, + name varchar(255) , + sex varchar(255) + ); + Drop TABLE student; + INSERT into student VALUES + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); + ~~~ + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + 2. 添加功能 + 3. 修改功能 + 4. 删除功能 + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 + +~~~ java +package D1; + +import java.sql.*; +import java.util.Scanner; + +public class JDBC { + public static void main(String[] args) { + out: + while (true) { + Scanner scanner = new Scanner(System.in); + System.out.println("输入你要的功能:\n" + + "\t1,查询功能\n" + + "\t2 添加功能\n" + + "\t3 修改功能\n" + + "\t4 删除功能\n" + + "\t5 退出"); + String choose = scanner.next(); + switch (choose) { + case "1": + System.out.println("你选择了查询功能"); + Inquire();//查询功能 + break; + case "2": + System.out.println("你选择了添加功能"); + Add(); //添加功能 + break; + case "3": + System.out.println("你选择了修改功能"); + Alter(); //修改功能 + break; + case "4": + System.out.println("你选择了删除功能"); + Delete(); //删除功能 + break; + case "5": + System.out.println("你选择了退出功能"); + break out; + default: + System.out.println("对不起,没有这个功能!"); + } + } + } + + public static void Add() { +// 添加功能 + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection connection = DriverManager.getConnection(url, username, password); + String Add = "insert into student values(4,'赵六','男')"; + Statement statement = connection.createStatement(); + int i = statement.executeUpdate(Add); + if (i > 0) { + System.out.println("添加成功,添加了" + i + "行"); + } else { + System.out.println("添加失败"); + } + connection.close(); + statement.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public static void Delete() { + // 删除功能 + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + Connection connection = DriverManager.getConnection(url, usename, password); + String delete = "delete from student where id=4"; + Statement statement = connection.createStatement(); + int i = statement.executeUpdate(delete); + if (i > 0) { + System.out.println("删除成功,删除了" + i + "行"); + } else { + System.out.println("删除失败!"); + } + connection.close(); + statement.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public static void Alter() { + // 修改功能 + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + Connection connection = DriverManager.getConnection(url, usename, password); + String alter = "update student set name='老六' where id=4"; + Statement statement = connection.createStatement(); + int i = statement.executeUpdate(alter); + if (i > 0) { + System.out.println("修改成功,修改了" + i + "行"); + } else { + System.out.println("修改失败!"); + } + connection.close(); + statement.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public static void Inquire() { + + try { +// 查询功能 +// 1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); +// 2.链接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + Connection connection = DriverManager.getConnection(url, usename, password); +// 3.SQL的内容 + String sql = "select *from student"; +// 4.获取执行SQL的内容 + Statement statement = connection.createStatement(); +// 5.运行SQL + ResultSet re = statement.executeQuery(sql);//只针对查询语句 +// 6.处理结果 + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println("ID:" + id + " 姓名:" + name + " 性别:" + sex); + } +// 7.释放资源 + connection.close(); + statement.close(); + re.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +~~~ + -- Gitee From cecceb35c19f0bd2e4cb11fe93d22442885036e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BF=B5=E5=BF=B5?= <2324752546@qq.com> Date: Sun, 21 May 2023 21:14:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230520 \344\275\234\344\270\232.md" | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 "47 \346\235\216\345\277\265\345\277\265/20230520 \344\275\234\344\270\232.md" diff --git "a/47 \346\235\216\345\277\265\345\277\265/20230520 \344\275\234\344\270\232.md" "b/47 \346\235\216\345\277\265\345\277\265/20230520 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..86df2d7 --- /dev/null +++ "b/47 \346\235\216\345\277\265\345\277\265/20230520 \344\275\234\344\270\232.md" @@ -0,0 +1,107 @@ +## 作业: + +~~~ Java +//JDBC工具类 +import java.sql.*; + +public class Jdbc { + //注册+连接 + private Connection conn() { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEchoing=utf8"; + Connection conn = DriverManager.getConnection(url, "root", "root"); + return conn; + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //查询 + ResultSet select(String sql){ + try { + PreparedStatement pst = conn().prepareStatement(sql); + ResultSet re = pst.executeQuery(); + return re; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +```java +//学生对象 +public class Student { + private int id; + private String name; + private String sex; + + public Student() { + } + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } +} +``` + +```java +//测试类 +import java.sql.*; +import java.util.ArrayList; + +public class Test { + public static void main(String[] args) { + Jdbc jdbc = new Jdbc(); + ResultSet re = jdbc.select("select * from student"); + ArrayList stu = new ArrayList<>(); + try { + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + Student s = new Student(id, name, sex); + stu.add(s); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + System.out.println("ID\t\t姓名\t\t性别"); + for (int i = 0; i < stu.size(); i++) { + Student s = stu.get(i); + System.out.println(s.getId()+"\t\t"+s.getName()+"\t\t"+s.getSex()); + } + } +} +~~~ + -- Gitee