From 3ae01da9102ad9b23f7a168ebe19f2bb5b4b0c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=86=99=E7=A5=A5?= <1817589159@qq.com> Date: Wed, 17 May 2023 21:47:04 +0800 Subject: [PATCH 1/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 --- .../20230517 \344\275\234\344\270\232.md" | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 "15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" diff --git "a/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" "b/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..5d00ea9 --- /dev/null +++ "b/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" @@ -0,0 +1,204 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 查询功能,查询student中所有数据 + + ```java + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "sxxddytdn"; + Connection conn = DriverManager.getConnection(url,username,password); + String sql = "select * from student"; + Statement st = conn.createStatement(); + ResultSet re = st.executeQuery(sql); + System.out.println("编号\t\t姓名\t\t性别"); + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id+"\t\t"+name+"\t\t"+sex); + } + re.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + 添加功能 + + ```java + 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 conn = DriverManager.getConnection(url,username,password); + String sql = "insert into student values(4,'A','男')"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("添加了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + 修改功能 + + ```java + 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 conn = DriverManager.getConnection(url,username,password); + String sql = "update student set name = 'B' where name = 'A'"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("修改了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + 删除功能 + + ```java + 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 conn = DriverManager.getConnection(url,username,password); + String sql = "delete from student where id = 1"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("删除了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 + + ```java + import java.sql.*; + + public class Jdbc { + + //注册+连接 + Connection conn() { + 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 conn = DriverManager.getConnection(url, username, password); + Statement st = conn.createStatement(); + return conn; + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //添加 + void add(String sql){ + Connection conn = conn(); + try { + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("添加了"+i+"行"); + st.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //删除 + void delete(String sql){ + Connection conn = conn(); + try { + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("删除了"+i+"行"); + st.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //修改 + void update(String sql){ + Connection conn = conn(); + try { + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("修改了"+i+"行"); + st.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //查询 + void select(String sql){ + Connection conn = conn(); + try { + Statement st = conn.createStatement(); + ResultSet re = st.executeQuery(sql); + System.out.println("编号\t\t姓名\t\t性别"); + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id+"\t\t"+name+"\t\t"+sex); + } + re.close(); + st.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + } + ``` + + \ No newline at end of file -- Gitee From 30ea97f23ac1d8361806b9bc68089b925c330326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=86=99=E7=A5=A5?= <1817589159@qq.com> Date: Wed, 17 May 2023 22:01:24 +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 --- .../20230517 \344\275\234\344\270\232.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" "b/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" index 5d00ea9..6e2e44e 100644 --- "a/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" +++ "b/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" @@ -21,7 +21,7 @@ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEncoding=utf8"; String username = "root"; - String password = "sxxddytdn"; + String password = "root"; Connection conn = DriverManager.getConnection(url,username,password); String sql = "select * from student"; Statement st = conn.createStatement(); -- Gitee