From b1c530b0a28d5a8d9e0f18da7c73960a375ca3d5 Mon Sep 17 00:00:00 2001 From: xcchengguo <2395432766@qq.com> Date: Wed, 17 May 2023 16:31:45 +0800 Subject: [PATCH 1/2] 111 --- .../\350\257\225\344\270\200\350\257\225.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "18 \350\260\242\345\256\270/\350\257\225\344\270\200\350\257\225.md" diff --git "a/18 \350\260\242\345\256\270/\350\257\225\344\270\200\350\257\225.md" "b/18 \350\260\242\345\256\270/\350\257\225\344\270\200\350\257\225.md" new file mode 100644 index 0000000..e69de29 -- Gitee From bace6de6900940bdb44b7940e606002ffd44ed93 Mon Sep 17 00:00:00 2001 From: xcchengguo <2395432766@qq.com> Date: Sat, 20 May 2023 10:43:13 +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 --- .../20230520 \344\275\234\344\270\232.md" | 156 ++++++++++++++++++ .../\350\257\225\344\270\200\350\257\225.md" | 0 2 files changed, 156 insertions(+) create mode 100644 "18 \350\260\242\345\256\270/20230520 \344\275\234\344\270\232.md" delete mode 100644 "18 \350\260\242\345\256\270/\350\257\225\344\270\200\350\257\225.md" diff --git "a/18 \350\260\242\345\256\270/20230520 \344\275\234\344\270\232.md" "b/18 \350\260\242\345\256\270/20230520 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..0381853 --- /dev/null +++ "b/18 \350\260\242\345\256\270/20230520 \344\275\234\344\270\232.md" @@ -0,0 +1,156 @@ +~~~java +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + ```java + import java.sql.*; + + public class Select { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false", "root", "root"); + String sql = "select * from student"; + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql); + while (rs.next()){ + int stuNo = rs.getInt("stuNo"); + String stuName = rs.getString("stuName"); + String stuSex = rs.getString("stuSex"); + System.out.println(stuNo +" "+ "姓名:" + stuName +" "+ "性别:" + stuSex); + } + rs.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + import java.sql.*; + + public class Add { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql = "insert into student values (4,'小蓝','男'),(5,'小红','男')"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >0){ + System.out.println("添加成功"+i+"行"); + }else { + System.out.println("添加失败"); + System.out.println("添加失败"+i+"行"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class Update { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql = "update student set stuName='小绿' where stuNo=3"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >0){ + System.out.println("添加成功"+i+"行"); + }else { + System.out.println("添加失败"); + System.out.println("添加失败"+i+"行"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class Delete { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql = "delete from student where stuNo=4"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >0){ + System.out.println("成功删除"+i+"行"); + }else { + System.out.println("删除失败"); + System.out.println("删除失败"+i+"行"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + create database Ok; + create database student_db charset utf8; + use student_db; + create table student( + stuNo int, + stuName varchar(20), + stuSex varchar(20) + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + + + ``` + + +~~~ + diff --git "a/18 \350\260\242\345\256\270/\350\257\225\344\270\200\350\257\225.md" "b/18 \350\260\242\345\256\270/\350\257\225\344\270\200\350\257\225.md" deleted file mode 100644 index e69de29..0000000 -- Gitee