From 8b24a5fe1d61b56a44cd5cffa963c5109a93bb96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E8=88=9C?= <1991510644@qq.com> Date: Wed, 17 May 2023 22:06:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230517 JDBC\344\275\234\344\270\232.md" | 196 ++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 "51 \347\250\213\350\210\234/20230517 JDBC\344\275\234\344\270\232.md" diff --git "a/51 \347\250\213\350\210\234/20230517 JDBC\344\275\234\344\270\232.md" "b/51 \347\250\213\350\210\234/20230517 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..de0f3a3 --- /dev/null +++ "b/51 \347\250\213\350\210\234/20230517 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,196 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```mysql + create database student_db charset utf8; + use student_db; + create table student ( + id int, + name varchar(10), + sex char + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + + select * from student; + ``` + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```javascript + import java.sql.*; + + public class Select { + public static void main(String[] args) { + Connection conn = null; + Statement sta = null; + ResultSet res = null; + 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"; + conn = DriverManager.getConnection(url, username, password); + String sql = "select * from student"; + sta = conn.createStatement(); + res = sta.executeQuery(sql); + while (res.next()){ + int id = res.getInt("id"); + String name = res.getString("name"); + String sex = res.getString("sex"); + System.out.println(id+"--"+name+"--"+sex); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + }finally { + try { + if (res != null){ + res.close(); + } + if (sta != null){ + sta.close(); + } + if (conn != null){ + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放错误"); + e.printStackTrace(); + } + } + } + } + ``` + + 2. 添加功能 + + ```javascript + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class Insert { + public static void main(String[] args) { + 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,'王六','女')"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("添加成功"); + }else { + System.out.println("添加失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } + ``` + + 3. 修改功能 + + ```javascript + 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"); + 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 sex = '男' where name = '王六'"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } + ``` + + 4. 删除功能 + + ```javascript + 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"); + 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 = 4"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } + ``` + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file -- Gitee From eb7a383fa40b8985ac6e22d02e4dc0bcf3dc7090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E8=88=9C?= <1991510644@qq.com> Date: Mon, 22 May 2023 21:44:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\243\205\344\275\234\344\270\232.md" | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 "51 \347\250\213\350\210\234/20230521 \345\260\201\350\243\205\344\275\234\344\270\232.md" diff --git "a/51 \347\250\213\350\210\234/20230521 \345\260\201\350\243\205\344\275\234\344\270\232.md" "b/51 \347\250\213\350\210\234/20230521 \345\260\201\350\243\205\344\275\234\344\270\232.md" new file mode 100644 index 0000000..5bb705c --- /dev/null +++ "b/51 \347\250\213\350\210\234/20230521 \345\260\201\350\243\205\344\275\234\344\270\232.md" @@ -0,0 +1,124 @@ +```java +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Scanner; +/** + * @author zhc + */ +public class test { + public static void main(String[] args) { + //获取连接 + Driver.conn(); + //注入SQL语句 + Driver.query("select * from student"); + //关闭连接 + Driver.close(); + //查看集合 + Driver.show(); + } +} +package java与数据库的连接.封装; + +import java.sql.*; +import java.util.ArrayList; + +/** + * @author zhc + */ +public class Driver { + private static final String Driver = "com.mysql.jdbc.Driver"; + private static final String URL = "jdbc:mysql://localhost:3306/student_db?useSSL=false&unicode=true&characterEncoding=utf8"; + private static final String user = "root"; + private static final String paw = "root"; + int id; + String name; + String sex; + + static void show() { + for (int i = 0; i < list.size(); i++) { + Driver driver = list.get(i); + int id = driver.id; + String name = driver.name; + String sex = driver.sex; + System.out.println("ID=" + id + ",name=" + name + ",sex=" + sex); + } + } + + //注册驱动 + static { + System.out.println("注册驱动中..."); + try { + Class.forName(Driver); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册失败"); + } + } + + public static Connection conn() { + System.out.println("连接数据库中..."); + Connection con = null; + try { + con = DriverManager.getConnection(URL, user, paw); + } catch (SQLException e) { + System.out.println("数据库连接失败"); + } + return con; + } + + static ArrayList list = new ArrayList<>(); + + public static void query(String sql, String... keys) { + System.out.println("查询中..."); + ResultSet rs = null; + int j = 0; + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + } + rs = pst.executeQuery(sql); + while (rs.next()) { + int id = rs.getInt(1); + String name = rs.getString(2); + String sex = rs.getString(3); + System.out.printf("ID=%d,name=%s,sex=%s\n", id, name, sex); + Driver driver = new Driver(); + driver.id = id; + driver.name = name; + driver.sex = sex; + list.add(j++, driver); + } + } catch (SQLException e) { + System.out.println("查询失败"); + } + } + + public static void update(String sql, String... keys) { + System.out.println("更新中..."); + try { + PreparedStatement pst = conn().prepareStatement(sql, keys); + int i = pst.executeUpdate(sql); + if (i > 0) { + System.out.printf("变化了%d条\n", i); + } else { + System.out.printf("更新失败.变化%d条\n", i); + } + } catch (SQLException e) { + System.out.println("更新失败"); + } + } + + public static void close() { + System.out.println("释放资源中..."); + try { + conn().close(); + } catch (SQLException e) { + System.out.println("释放资源失败"); + } + System.out.println("释放资源成功"); + } + +} +``` + -- Gitee