From 19e4f5bac659d01fcd9f27f24aef0f2ecb90ff59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=A9=E4=B8=9C?= <3187628460@qq.com> Date: Sat, 20 May 2023 10:40:35 +0800 Subject: [PATCH 1/5] =?UTF-8?q?jdbc=E5=85=A5=E9=97=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\232\204jdbc\345\237\272\347\241\200.md" | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 "23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" diff --git "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" new file mode 100644 index 0000000..f895e7b --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" @@ -0,0 +1,187 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```java + create database student_db charset utf8; + use student_db; + create table student ( + num int, + name varchar(20), + sex char + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + ``` + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + 5. ```java + import java.sql.*; + import java.util.Scanner; + + public class p1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + // 创建3个类 + Connection con = null; + Statement state = null; + ResultSet res = null; + int resultSet = 0; + String url = "jdbc:Mysql://localhost:3306/student?uesSSL=false&useUnicode=true&characterEncoding=utf8"; + String name = "root"; + String password = "root"; + try { + //添加驱动 + Class.forName("com.mysql.jdbc.Driver"); + // 创建连接 + con = DriverManager.getConnection(url,name, password); + // 创建 Statement 对象 + state = con.createStatement(); + // 写执行语句 + choice:while (true){ + int choice = sc.nextInt(); + int num = sc.nextInt(); + switch (choice){ + case 1: + System.out.println("查询"); + select(res,state); + break; + case 2: + System.out.println("删除"); + System.out.println("请输入要删除的id"); + delete(resultSet,state,sc.nextInt()); + break; + case 3: + System.out.println("修改"); + System.out.println("请输入要修改的id"); + update(res,resultSet,state,sc); + case 4: + System.out.println("添加"); + Add(res,resultSet,state,sc); + break; + default: + break choice; + + } + } + } + catch(SQLException e){ + e.printStackTrace(); + } + catch(ClassNotFoundException e){ + e.printStackTrace(); + } + try { + if (res != null){ + res.close(); + } + if (con != null){ + con.close(); + } + if (state !=null){ + state.close(); + } + } + catch(SQLException e){ + e.printStackTrace(); + } + } + public static void select(ResultSet res, Statement state) throws SQLException{ + res = state.executeQuery("select * from student"); + while (res.next()){ + System.out.println(res.getInt("id") + "\t" + res.getString("name")+res.getString("sex")); + } + } + public static void delete(int res,Statement state,int num) throws SQLException{ + res = state.executeUpdate("Delete from student where id ="+num); + System.out.println("删除了"+res+"行"); + } + public static void update(ResultSet result,int res,Statement state,Scanner sc) throws SQLException{ + int id = sc.nextInt(); + result= state.executeQuery("select * from student"); + while (result.next()){ + if (result.getInt("id") != id){ + System.out.println("查无此人"); + return; + } + } + System.out.println("请输入要修改的姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + res = state.executeUpdate("update student " + + "set name = "+ "\'"+name+"\'"+"\\, " + + "sex ="+"\'"+sex+"\' where id ="+id); + if (sex >0){ + System.out.println("修改成功"+res+"行已被修改"); + } + else { + System.out.println("修改失败"); + } + } + public static void Add + (ResultSet result,int res,Statement state,Scanner sc) throws SQLException{ + System.out.println("请输入id"); + int id = sc.nextInt(); + result= state.executeQuery("select * from student"); + while (result.next()){ + if (result.getInt("id") == id){ + System.out.println("该id已存在请换一个"); + return; + } + } + System.out.println("请输入姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + res = state.executeUpdate("insert into student values ("+id+"\\,"+"\'"+name+"\' \\, \'"+sex+"\'"); + if (res >0){ + System.out.println("添加成功"); + } + else { + System.out.println("添加失败"); + } + } + } + + + //1. 编写java 4个类,分别实现以下功能 + // 1. 查询功能,查询student中所有数据 + // 2. 添加功能 + // 3. 修改功能 + // 4. 删除功能 + // 2. 扩展题【预习题】 + // + // 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + // 2. 能否实现将查询的结果,封装成java对象 + + + ``` + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file -- Gitee From 4bb8bc12bfd3f12af1b14ab8c61e089bcb2c0f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=A9=E4=B8=9C?= <3187628460@qq.com> Date: Sat, 20 May 2023 09:30:24 +0000 Subject: [PATCH 2/5] =?UTF-8?q?update=2023=20=E9=BB=84=E6=B5=A9=E4=B8=9C/2?= =?UTF-8?q?0235017=20java=E7=9A=84jdbc=E5=9F=BA=E7=A1=80.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄浩东 <3187628460@qq.com> --- ...47\232\204jdbc\345\237\272\347\241\200.md" | 258 +++++++++--------- 1 file changed, 131 insertions(+), 127 deletions(-) diff --git "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" index f895e7b..0c9ac6f 100644 --- "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" +++ "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" @@ -37,133 +37,137 @@ 4. 删除功能 5. ```java - import java.sql.*; - import java.util.Scanner; - - public class p1 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - // 创建3个类 - Connection con = null; - Statement state = null; - ResultSet res = null; - int resultSet = 0; - String url = "jdbc:Mysql://localhost:3306/student?uesSSL=false&useUnicode=true&characterEncoding=utf8"; - String name = "root"; - String password = "root"; - try { - //添加驱动 - Class.forName("com.mysql.jdbc.Driver"); - // 创建连接 - con = DriverManager.getConnection(url,name, password); - // 创建 Statement 对象 - state = con.createStatement(); - // 写执行语句 - choice:while (true){ - int choice = sc.nextInt(); - int num = sc.nextInt(); - switch (choice){ - case 1: - System.out.println("查询"); - select(res,state); - break; - case 2: - System.out.println("删除"); - System.out.println("请输入要删除的id"); - delete(resultSet,state,sc.nextInt()); - break; - case 3: - System.out.println("修改"); - System.out.println("请输入要修改的id"); - update(res,resultSet,state,sc); - case 4: - System.out.println("添加"); - Add(res,resultSet,state,sc); - break; - default: - break choice; - - } - } - } - catch(SQLException e){ - e.printStackTrace(); - } - catch(ClassNotFoundException e){ - e.printStackTrace(); - } - try { - if (res != null){ - res.close(); - } - if (con != null){ - con.close(); - } - if (state !=null){ - state.close(); - } - } - catch(SQLException e){ - e.printStackTrace(); - } - } - public static void select(ResultSet res, Statement state) throws SQLException{ - res = state.executeQuery("select * from student"); - while (res.next()){ - System.out.println(res.getInt("id") + "\t" + res.getString("name")+res.getString("sex")); - } - } - public static void delete(int res,Statement state,int num) throws SQLException{ - res = state.executeUpdate("Delete from student where id ="+num); - System.out.println("删除了"+res+"行"); - } - public static void update(ResultSet result,int res,Statement state,Scanner sc) throws SQLException{ - int id = sc.nextInt(); - result= state.executeQuery("select * from student"); - while (result.next()){ - if (result.getInt("id") != id){ - System.out.println("查无此人"); - return; - } - } - System.out.println("请输入要修改的姓名"); - String name = sc.next(); - System.out.println("请输入性别"); - char sex = sc.next().charAt(0); - res = state.executeUpdate("update student " + - "set name = "+ "\'"+name+"\'"+"\\, " + - "sex ="+"\'"+sex+"\' where id ="+id); - if (sex >0){ - System.out.println("修改成功"+res+"行已被修改"); - } - else { - System.out.println("修改失败"); - } - } - public static void Add - (ResultSet result,int res,Statement state,Scanner sc) throws SQLException{ - System.out.println("请输入id"); - int id = sc.nextInt(); - result= state.executeQuery("select * from student"); - while (result.next()){ - if (result.getInt("id") == id){ - System.out.println("该id已存在请换一个"); - return; - } - } - System.out.println("请输入姓名"); - String name = sc.next(); - System.out.println("请输入性别"); - char sex = sc.next().charAt(0); - res = state.executeUpdate("insert into student values ("+id+"\\,"+"\'"+name+"\' \\, \'"+sex+"\'"); - if (res >0){ - System.out.println("添加成功"); - } - else { - System.out.println("添加失败"); - } - } - } + import java.sql.*; +import java.util.Scanner; + +public class p1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + // 创建3个类 + Connection con = null; + Statement state = null ; + int resultSet = 0; + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String name = "root"; + String password = "root"; + try { + //添加驱动 + Class.forName("com.mysql.jdbc.Driver"); + // 创建连接 + con = DriverManager.getConnection(url,name, password); + // 创建 Statement 对象 + state = con.createStatement(); + // 写执行语句 + choice:while (true){ + int choice = sc.nextInt(); + switch (choice){ + case 1: + System.out.println("查询"); + select(state); + break; + case 2: + System.out.println("删除"); + System.out.println("请输入要删除的id"); + delete(state,sc.nextInt()); + break; + case 3: + System.out.println("修改"); + System.out.println("请输入要修改的id"); + update(state,sc); + break ; + case 4: + System.out.println("添加"); + Add(state,sc); + break; + default: + break choice; + + } + } + } + catch(SQLException e){ + e.printStackTrace(); + } + catch(ClassNotFoundException e){ + e.printStackTrace(); + } + try { + state.close(); + con.close(); + } + catch(SQLException e){ + e.printStackTrace(); + } + } + public static void select( Statement state) throws SQLException{ + ResultSet res = state.executeQuery("select * from student"); + while (res.next()){ + System.out.println(res.getInt("num") + "\t" + res.getString("name")+"\t"+res.getString("sex")); + } + res.close(); + } + public static void delete(Statement state,int num) throws SQLException{ + int res = state.executeUpdate("Delete from student where num ="+num); + System.out.println("删除了"+res+"行"); + } + public static void update(Statement state,Scanner sc) throws SQLException{ + int res; + int num = 0; + int all = 0; + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + all++; + if (result.getInt("num") != id){ + num++; + } + if (num == all){ + System.out.println("查无此人"); + result.close(); + return; + } + } + result.close(); + System.out.println("请输入要修改的姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + res = state.executeUpdate("update student " + + "set name = "+ "\'"+name+"\', " + + "sex ="+"\'"+sex+"\' where num ="+id); + + if (res >0){ + System.out.println("修改成功"+res+"行已被修改"); + } + else { + System.out.println("修改失败"); + } + } + public static void Add + (Statement state,Scanner sc) throws SQLException{ + System.out.println("请输入id"); + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + if (result.getInt("num") == id){ + System.out.println("该id已存在请换一个"); + return; + } + } + System.out.println("请输入姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + int res = state.executeUpdate(String.format("insert into student values ("+ id+","+ "\'"+name+"\'"+","+"\'"+ sex+"\'"+")")); + if (res >0){ + System.out.println("添加成功"); + } + else { + System.out.println("添加失败"); + } + } +} + //1. 编写java 4个类,分别实现以下功能 -- Gitee From 1605563fe8b543bcad10594debe0461449781141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=A9=E4=B8=9C?= <3187628460@qq.com> Date: Sat, 20 May 2023 09:34:59 +0000 Subject: [PATCH 3/5] =?UTF-8?q?update=2023=20=E9=BB=84=E6=B5=A9=E4=B8=9C/2?= =?UTF-8?q?0235017=20java=E7=9A=84jdbc=E5=9F=BA=E7=A1=80.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄浩东 <3187628460@qq.com> --- ...5017 java\347\232\204jdbc\345\237\272\347\241\200.md" | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" index 0c9ac6f..b9fce7d 100644 --- "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" +++ "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" @@ -37,9 +37,6 @@ 4. 删除功能 5. ```java - import java.sql.*; -import java.util.Scanner; - public class p1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); @@ -98,11 +95,7 @@ public class p1 { catch(SQLException e){ e.printStackTrace(); } - } - public static void select( Statement state) throws SQLException{ - ResultSet res = state.executeQuery("select * from student"); - while (res.next()){ - System.out.println(res.getInt("num") + "\t" + res.getString("name")+"\t"+res.getString("sex")); + } public static void select( S.getInt("num") + "\t" + res.getString("name")+"\t"+res.getString("sex")); } res.close(); } -- Gitee From 218d098872e7f579f3c77c9d467c31f166824e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=A9=E4=B8=9C?= <3187628460@qq.com> Date: Sat, 20 May 2023 09:35:23 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2023?= =?UTF-8?q?=20=E9=BB=84=E6=B5=A9=E4=B8=9C/20235017=20java=E7=9A=84jdbc?= =?UTF-8?q?=E5=9F=BA=E7=A1=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\232\204jdbc\345\237\272\347\241\200.md" | 184 ------------------ 1 file changed, 184 deletions(-) delete mode 100644 "23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" diff --git "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" deleted file mode 100644 index b9fce7d..0000000 --- "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" +++ /dev/null @@ -1,184 +0,0 @@ -### JDBC作业: - -1. MySQL中创建一个数据库student_db - -2. 库中创建student表 - -3. 表中数据如下 - -4. | 编号 | 姓名 | 性别 | - | ---- | ---- | ---- | - | 1 | 张三 | 男 | - | 2 | 李四 | 女 | - | 3 | 王五 | 男 | - - ```java - create database student_db charset utf8; - use student_db; - create table student ( - num int, - name varchar(20), - sex char - ); - insert into student values - (1,'张三','男'), - (2,'李四','女'), - (3,'王五','男'); - ``` - -5. 编写java 4个类,分别实现以下功能 - - 1. 查询功能,查询student中所有数据 - - 2. 添加功能 - - 3. 修改功能 - - 4. 删除功能 - - 5. ```java -public class p1 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - // 创建3个类 - Connection con = null; - Statement state = null ; - int resultSet = 0; - String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; - String name = "root"; - String password = "root"; - try { - //添加驱动 - Class.forName("com.mysql.jdbc.Driver"); - // 创建连接 - con = DriverManager.getConnection(url,name, password); - // 创建 Statement 对象 - state = con.createStatement(); - // 写执行语句 - choice:while (true){ - int choice = sc.nextInt(); - switch (choice){ - case 1: - System.out.println("查询"); - select(state); - break; - case 2: - System.out.println("删除"); - System.out.println("请输入要删除的id"); - delete(state,sc.nextInt()); - break; - case 3: - System.out.println("修改"); - System.out.println("请输入要修改的id"); - update(state,sc); - break ; - case 4: - System.out.println("添加"); - Add(state,sc); - break; - default: - break choice; - - } - } - } - catch(SQLException e){ - e.printStackTrace(); - } - catch(ClassNotFoundException e){ - e.printStackTrace(); - } - try { - state.close(); - con.close(); - } - catch(SQLException e){ - e.printStackTrace(); - } - } public static void select( S.getInt("num") + "\t" + res.getString("name")+"\t"+res.getString("sex")); - } - res.close(); - } - public static void delete(Statement state,int num) throws SQLException{ - int res = state.executeUpdate("Delete from student where num ="+num); - System.out.println("删除了"+res+"行"); - } - public static void update(Statement state,Scanner sc) throws SQLException{ - int res; - int num = 0; - int all = 0; - int id = sc.nextInt(); - ResultSet result= state.executeQuery("select * from student"); - while (result.next()){ - all++; - if (result.getInt("num") != id){ - num++; - } - if (num == all){ - System.out.println("查无此人"); - result.close(); - return; - } - } - result.close(); - System.out.println("请输入要修改的姓名"); - String name = sc.next(); - System.out.println("请输入性别"); - char sex = sc.next().charAt(0); - res = state.executeUpdate("update student " + - "set name = "+ "\'"+name+"\', " + - "sex ="+"\'"+sex+"\' where num ="+id); - - if (res >0){ - System.out.println("修改成功"+res+"行已被修改"); - } - else { - System.out.println("修改失败"); - } - } - public static void Add - (Statement state,Scanner sc) throws SQLException{ - System.out.println("请输入id"); - int id = sc.nextInt(); - ResultSet result= state.executeQuery("select * from student"); - while (result.next()){ - if (result.getInt("num") == id){ - System.out.println("该id已存在请换一个"); - return; - } - } - System.out.println("请输入姓名"); - String name = sc.next(); - System.out.println("请输入性别"); - char sex = sc.next().charAt(0); - int res = state.executeUpdate(String.format("insert into student values ("+ id+","+ "\'"+name+"\'"+","+"\'"+ sex+"\'"+")")); - if (res >0){ - System.out.println("添加成功"); - } - else { - System.out.println("添加失败"); - } - } -} - - - - //1. 编写java 4个类,分别实现以下功能 - // 1. 查询功能,查询student中所有数据 - // 2. 添加功能 - // 3. 修改功能 - // 4. 删除功能 - // 2. 扩展题【预习题】 - // - // 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - // 2. 能否实现将查询的结果,封装成java对象 - - - ``` - - - -6. 扩展题【预习题】 - - 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 - 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file -- Gitee From 4756835b83539508b1286c01f8f86e7bdb2d1580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=A9=E4=B8=9C?= <3187628460@qq.com> Date: Sun, 21 May 2023 21:09:48 +0800 Subject: [PATCH 5/5] =?UTF-8?q?jdbc=E5=B7=A5=E5=85=B7=E7=B1=BB=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...67\347\261\273\344\275\234\344\270\232.md" | 121 ++++++++++++ ...47\232\204jdbc\345\237\272\347\241\200.md" | 184 ++++++++++++++++++ 2 files changed, 305 insertions(+) create mode 100644 "23 \351\273\204\346\265\251\344\270\234/20230520 jdbc\345\267\245\345\205\267\347\261\273\344\275\234\344\270\232.md" create mode 100644 "23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" diff --git "a/23 \351\273\204\346\265\251\344\270\234/20230520 jdbc\345\267\245\345\205\267\347\261\273\344\275\234\344\270\232.md" "b/23 \351\273\204\346\265\251\344\270\234/20230520 jdbc\345\267\245\345\205\267\347\261\273\344\275\234\344\270\232.md" new file mode 100644 index 0000000..b9e68f6 --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20230520 jdbc\345\267\245\345\205\267\347\261\273\344\275\234\344\270\232.md" @@ -0,0 +1,121 @@ +使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历之个集合的对象 + +```java +//工具类 +import java.sql.*; +import java.util.ArrayList; +import java.util.Arrays; + +public class tool{ + private static final String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String name = "root"; + static { + try{ + Class.forName("com.mysql.jdbc.Driver"); + } + catch (ClassNotFoundException e){ + e.printStackTrace(); + } + } + public static Connection getCon() throws SQLException + { + Connection con = DriverManager.getConnection(url,name,name); + return con; + } + public static ArrayList Select(String sql,String ...key) throws SQLException{ + ArrayList info = new ArrayList<>(); + Connection con = getCon(); + PreparedStatement state = con.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + state.setString(i,key[i]); + } + ResultSet res = state.executeQuery(); + while (res.next()){ + int num = res.getInt("num"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(num,name,sex)); + } + return info; + } + public static ArrayList Select(String sql) throws SQLException{ + ArrayList info = new ArrayList<>(); + Connection con = getCon(); + PreparedStatement state = con.prepareStatement(sql); + ResultSet res = state.executeQuery(); + while (res.next()){ + int num = res.getInt("num"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(num,name,sex)); + } + return info; + } + + +} +//学生类 + +public class Student { + public Student(){} + public Student(int num,String name,String sex){ + this.setName(name); + this.setSex(sex); + this.setNum(num); + } + private int num; + private String name; + private String sex; + public String toString (){ + return "num = "+ getNum() +"\t name = "+ getName() +"\t sex = "+ getSex(); + } + + public int getNum() { + return num; + } + + public void setNum(int num) { + this.num = num; + } + + 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; + } +} +//实现类 +import java.sql.*; +import java.util.ArrayList; +import java.util.Scanner; + +import static java.lang.Class.forName; + +public class p1 { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + String sql = "select * from student"; + try { + list = tool.Select(sql); + } + catch (SQLException e){ + e.printStackTrace(); + } + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } + } + + +``` \ No newline at end of file diff --git "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" new file mode 100644 index 0000000..c417bd4 --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" @@ -0,0 +1,184 @@ +6. 1. JDBC作业: + + 1. MySQL中创建一个数据库student_db + + 2. 库中创建student表 + + 3. 表中数据如下 + + 4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```java + create database student_db charset utf8; + use student_db; + create table student ( + num int, + name varchar(20), + sex char + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + ``` + + 5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + 5. ```java + import java.sql.*; + import java.util.Scanner; + + import static java.lang.Class.forName; + + public class p1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + // 创建3个类 + Connection con = null; + Statement state = null ; + int resultSet = 0; + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String name = "root"; + String password = "root"; + try { + //添加驱动 + forName("com.mysql.jdbc.Driver"); + // 创建连接 + con = DriverManager.getConnection(url,name, password); + // 创建 Statement 对象 + state = con.createStatement(); + // 写执行语句 + choice:while (true){ + int choice = sc.nextInt(); + switch (choice){ + case 1: + System.out.println("查询"); + select(state); + break; + case 2: + System.out.println("删除"); + System.out.println("请输入要删除的id"); + delete(state,sc.nextInt()); + break; + case 3: + System.out.println("修改"); + System.out.println("请输入要修改的id"); + update(state,sc); + break ; + case 4: + System.out.println("添加"); + Add(state,sc); + break; + default: + break choice; + + } + } + } + catch(SQLException e){ + e.printStackTrace(); + } + catch(ClassNotFoundException e){ + e.printStackTrace(); + } + try { + if (state != null) { + state.close(); + } + if (con !=null) { + con.close(); + } + } + catch(SQLException e){ + e.printStackTrace(); + } + } + public static void select( Statement state) throws SQLException{ + ResultSet res = state.executeQuery("select * from student"); + while (res.next()){ + System.out.println(res.getInt("num") + "\t" + res.getString("name")+"\t"+res.getString("sex")); + } + res.close(); + } + public static void delete(Statement state,int num) throws SQLException{ + int res = state.executeUpdate("Delete from student where num ="+num); + System.out.println("删除了"+res+"行"); + } + public static void update(Statement state,Scanner sc) throws SQLException{ + int res; + int num = 0; + int all = 0; + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + all++; + if (result.getInt("num") != id){ + num++; + } + if (num == all){ + System.out.println("查无此人"); + result.close(); + return; + } + } + result.close(); + System.out.println("请输入要修改的姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + res = state.executeUpdate("update student " + + "set name = "+ "\'"+name+"\', " + + "sex ="+"\'"+sex+"\' where num ="+id); + + if (res >0){ + System.out.println("修改成功"+res+"行已被修改"); + } + else { + System.out.println("修改失败"); + } + } + public static void Add + (Statement state,Scanner sc) throws SQLException{ + System.out.println("请输入id"); + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + if (result.getInt("num") == id){ + System.out.println("该id已存在请换一个"); + return; + } + } + System.out.println("请输入姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + int res = state.executeUpdate(String.format("insert into student values ("+ id+","+ "\'"+name+"\'"+","+"\'"+ sex+"\'"+")")); + if (res >0){ + System.out.println("添加成功"); + } + else { + System.out.println("添加失败"); + } + } + } + + ``` + + + + 6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file -- Gitee