From ed89c0c16c2f97834248a5c39f066f5d105f1cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E8=B4=B5=E9=9B=AF?= <2046174331@qq.com> Date: Thu, 18 May 2023 12:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230516 JDBC\344\275\234\344\270\232.md" | 244 ++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 "10 \346\270\251\350\264\265\351\233\257/20230516 JDBC\344\275\234\344\270\232.md" diff --git "a/10 \346\270\251\350\264\265\351\233\257/20230516 JDBC\344\275\234\344\270\232.md" "b/10 \346\270\251\350\264\265\351\233\257/20230516 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..128a556 --- /dev/null +++ "b/10 \346\270\251\350\264\265\351\233\257/20230516 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,244 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + 2. 添加功能 + 3. 修改功能 + 4. 删除功能 + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 + +1. 查讯类 + +```java +import java.net.ConnectException; +import java.sql.*; + +public class Test { + public static void main(String[] args) { + extracted(); + } + + private static void extracted() { + ResultSet rst = null; + Statement smt = null; + Connection conn = null; + try { + //0 导包 + //1 注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2 获取连接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + //3 编写SQL语句 + String sql = "select * from student"; + //4 获取执行 SQL 对象 + smt = conn.createStatement(); + //5 执行SQL + rst = smt.executeQuery(sql); + while (rst.next()) { + int id = rst.getInt("id"); + String name = rst.getString("name"); + String sex = rst.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 { + // 7 释放资源 + rst.close(); + smt.close(); + conn.close(); + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + } +} + +``` + +2. 插入类 + +```java +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) { + Statement smt = null; + Connection conn = null; + try { + //0 导包 + //1 注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2 获取连接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + //3 SQL 语句 + String sql = "INSERT INTO student VALUES\n(3,'强强','男')"; + //4 获取执行 + smt = conn.createStatement(); + //5 执行sql + int i = smt.executeUpdate(sql); + //6 处理返回问题 + if (i > 0) { + System.out.println("插入成功"); + } else { + System.out.println("没有插入数据哦!请检查"); + } + + } catch (ClassNotFoundException e) { + System.out.println("没有成功导入包,请正确导入"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + } finally { + //7 释放 + try { + smt.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + +3. 修改类 + +~~~java +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) { + Connection conn = null; + Statement smt = null; + try { + //0 导包 + //1 注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2 获取连接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + //3 编写SQL语句 + String sql = "UPDATE student SET NAME = '李小青' WHERE id = 2"; + //4 获取执行 SQL 对象 + smt = conn.createStatement(); + //5 执行SQL + int i = smt.executeUpdate(sql); + //6 处理返回结果 + if(i>0){ + System.out.println("成功修改了"+i+"行"); + }else { + System.out.println("成功修改了"+i+"行"); + System.out.println("没有改变"); + } + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + }finally { + // 7 释放资源 + try { + smt.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + + } +} + +~~~ + +4. 删除类 + +```java +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) { + Statement stm = null; + Connection conn = null; + try { + //0 导包 + //1 注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2 获取连接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + //3 编写SQL语句 + String sql = "DELETE FROM student WHERE id=3"; + //4 获取执行 + stm = conn.createStatement(); + //5 执行sql语句 + int i = stm.executeUpdate(sql); + //6 处理返回结果 + if (i > 0) { + System.out.println("成功删除" + i + "行"); + System.out.println("删除成功"); + } else { + System.out.println("成功删除" + i + "行"); + System.out.println("删除失败"); + } + + } catch (ClassNotFoundException e) { + System.out.println("没有成功导入包,请正确导入"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + } finally { + //7 释放 + try { + stm.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} + +``` + -- Gitee