From 9f7085932835a5fde2e30384e61f0aed2e73d84b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E4=BA=A8=E8=80=80?= <2640788668@qq.com> Date: Tue, 23 May 2023 00:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JDBC\344\275\234\344\270\232.md" | 184 +++++++++++++++ .../JDBC\344\275\234\344\270\2322.md" | 222 ++++++++++++++++++ 2 files changed, 406 insertions(+) create mode 100644 "21 \345\215\242\344\272\250\350\200\200/JDBC\344\275\234\344\270\232.md" create mode 100644 "21 \345\215\242\344\272\250\350\200\200/JDBC\344\275\234\344\270\2322.md" diff --git "a/21 \345\215\242\344\272\250\350\200\200/JDBC\344\275\234\344\270\232.md" "b/21 \345\215\242\344\272\250\350\200\200/JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..854b9a1 --- /dev/null +++ "b/21 \345\215\242\344\272\250\350\200\200/JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,184 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 查询功能,查询student中所有数据 + + ```java + import java.sql.*; + + public class Sqlselect { + public static void main(String[] args) { + Connection conn = null; + Statement stmt = null; + ResultSet rs = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf-8"; + String user = "root"; + String password = "root"; + // 创建连接 + conn = DriverManager.getConnection(url, user, password); + + // 创建Statement对象 + stmt = conn.createStatement(); + + // 执行查询操作 + rs = stmt.executeQuery("SELECT * FROM student"); + + // 遍历结果集 + while (rs.next()) { + System.out.println(rs.getInt("NO") + "\t" + rs.getString("name") + "\t" + rs.getString("sex")); + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + // 释放资源 + try { + if (rs != null) { + rs.close(); + } + if (stmt != null) { + stmt.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } + ``` + + + + 添加功能 + + ```java + import java.sql.*; + + public class Sqlinsert { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf-8"; + String user = "root"; + String password = "root"; + // 创建连接 + Connection conn = DriverManager.getConnection(url, user, password); + + // 创建Statement对象 + Statement stmt = conn.createStatement(); + + String sql="insert into student values(5,'小明','男')"; + + // 执行查询操作 + int rs = stmt.executeUpdate(sql); + System.out.println("添加了"+rs+"条数据"); + stmt.close(); + conn.close(); + } catch (ClassNotFoundException | SQLException e) { + throw new RuntimeException(e); + } + } + } + ``` + + + + 修改功能 + + ```java + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class Sqlupdate { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf-8"; + String user = "root"; + String password = "root"; + // 创建连接 + Connection conn = DriverManager.getConnection(url, user, password); + + // 创建Statement对象 + Statement stmt = conn.createStatement(); + + String sql="update student set NO=4 where name='小明'"; + int rs=stmt.executeUpdate(sql); + System.out.println("成功修改数据"); + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("包异常"); + } catch (SQLException e) { + System.out.println("修改错误"); + } + } + } + ``` + + + + 删除功能 + + ```java + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class Sqldelete { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf-8"; + String user = "root"; + String password = "root"; + // 创建连接 + Connection conn = DriverManager.getConnection(url, user, password); + + // 创建Statement对象 + Statement stmt = conn.createStatement(); + + String sql="delete from student where NO=4"; + int rs= stmt.executeUpdate(sql); + if(rs>0){ + System.out.println("删除了"+rs+"条数据"); + } + stmt.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + System.out.println("删除失败"); + } + } + } + ``` + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file diff --git "a/21 \345\215\242\344\272\250\350\200\200/JDBC\344\275\234\344\270\2322.md" "b/21 \345\215\242\344\272\250\350\200\200/JDBC\344\275\234\344\270\2322.md" new file mode 100644 index 0000000..6434d52 --- /dev/null +++ "b/21 \345\215\242\344\272\250\350\200\200/JDBC\344\275\234\344\270\2322.md" @@ -0,0 +1,222 @@ +### 作业 + +```java +package Demo2; + +import com.mysql.jdbc.Driver; + +import java.sql.*; +import java.util.Stack; + +public class Tool { + private static final String url="jdbc:mysql:///student_db?useSSL=false&characterEncoding=utf-8"; + private static final String user="root"; + private static final String password="root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (Exception e) { + System.out.println("驱动错误"); + } + } + public static Connection getConn() { + Connection conn; + try { + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + public static ResultSet query(String sql,String... keys) { + ResultSet rs; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + } + rs = pst.executeQuery(); + + + } catch (SQLException e) { + throw new RuntimeException(e); + } + return rs; + } + public static int update(String sql,String... keys) { + int count; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + /*for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + }*/ + count = pst.executeUpdate(); + if (count >0) { + System.out.println("修改了"+count+"条数据"); + }else + System.out.println("数据未改变"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return count; + } +} +``` + +// Use类 + +```java +package Demo2; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Use { + + void select() { + try { + System.out.println("查询"); + Scanner sc = new Scanner(System.in); + String a= sc.nextLine(); + ResultSet rs = Tool.query(a); + + while (rs.next()) { + int id = rs.getInt(1); + String name = rs.getString(2); + String sex = rs.getString(3); + System.out.println("id:" + id + "\t" + "名字:" + name + "\t" + "性别:" + sex); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + void insert() { + try { + System.out.println("添加"); + Scanner sc = new Scanner(System.in); + String a= sc.nextLine(); + int add = Tool.update(a); + System.out.println("成功添加"); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + void update() { + try { + System.out.println("修改"); + Scanner sc = new Scanner(System.in); + String a= sc.nextLine(); + int update = Tool.update(a); + System.out.println("修改成功"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + void delete(){ + try { + System.out.println("删除"); + Scanner sc = new Scanner(System.in); + String a= sc.nextLine(); + int update = Tool.update(a); + System.out.println("删除成功"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} +``` + +// Test类 + +```java +package Demo2; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Test { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请对数据库进行操作"); + Use use = new Use(); + int num= sc.nextInt(); + + out: + while (true) { + switch (num){ + case 1: + use.update(); + case 2: + use.select(); + break; + case 3: + use.insert(); + break; + case 4: + use.delete(); + break; + default: + break out; + } + } + + } +} +``` + +// Student类 + +```java +package Demo2; + +import java.awt.event.PaintEvent; + +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; + } +} + +``` + -- Gitee