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/4] =?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/4] =?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 From 1f9ec9f1e6e1f2a7dd1339fa0b24c1690967cd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=86=99=E7=A5=A5?= <1817589159@qq.com> Date: Sun, 21 May 2023 18:54:50 +0800 Subject: [PATCH 3/4] =?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" | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 "15 \346\262\210\347\206\231\347\245\245/20230520 \344\275\234\344\270\232.md" diff --git "a/15 \346\262\210\347\206\231\347\245\245/20230520 \344\275\234\344\270\232.md" "b/15 \346\262\210\347\206\231\347\245\245/20230520 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..f5bfacd --- /dev/null +++ "b/15 \346\262\210\347\206\231\347\245\245/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 From 20fe194d6f1e4b8f9166ab47bf4c75d8c8d77438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=86=99=E7=A5=A5?= <1817589159@qq.com> Date: Sun, 21 May 2023 22:06:26 +0800 Subject: [PATCH 4/4] =?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" | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git "a/15 \346\262\210\347\206\231\347\245\245/20230520 \344\275\234\344\270\232.md" "b/15 \346\262\210\347\206\231\347\245\245/20230520 \344\275\234\344\270\232.md" index f5bfacd..30348f6 100644 --- "a/15 \346\262\210\347\206\231\347\245\245/20230520 \344\275\234\344\270\232.md" +++ "b/15 \346\262\210\347\206\231\347\245\245/20230520 \344\275\234\344\270\232.md" @@ -3,15 +3,21 @@ import java.sql.*; public class Jdbc { - //注册+连接 - private Connection conn() { + //注册 + static + { 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); + } + } + //连接 + private Connection conn() { + try { + String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEchoing=utf8"; + Connection conn = DriverManager.getConnection(url, "root", "sxxddytdn"); + return conn; } catch (SQLException e) { throw new RuntimeException(e); } @@ -28,6 +34,7 @@ public class Jdbc { } } } + ``` ```java @@ -90,6 +97,7 @@ public class Test { Student s = new Student(id, name, sex); stu.add(s); } + re.close(); } catch (SQLException e) { throw new RuntimeException(e); } -- Gitee