diff --git "a/2244310237 \346\217\255\351\230\263\344\270\275/20230517 JDBC\344\275\234\344\270\232\357\274\232.md" "b/2244310237 \346\217\255\351\230\263\344\270\275/20230517 JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..d73038f3b79210a0d0ae58f0bbe2bf675223e153 --- /dev/null +++ "b/2244310237 \346\217\255\351\230\263\344\270\275/20230517 JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,226 @@ +### 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对象 + + + +```mysql +-- 创建数据库 +CREATE DATABASE student_db charset utf8; +-- 使用数据库 +use student_db; +-- 创建表格 +CREATE table student ( +id int, +name VARCHAR(12), +sex VARCHAR(12) +); +-- 插入数据 +INSERT into student VALUES +(1,'张三','男'), +(2,'李四','女'), +(3,'王五','男'); +-- 查询所有 +SELECT * from student; +``` + +查询功能,查询student中所有数据 + +```java +package Topic1; + +import java.sql.*; + +public class A1 { + public static void main(String[] args) { + Connection a = null; + Statement b = null; + ResultSet c = 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"; + a = DriverManager.getConnection(url, username, password); + String sql = "select * from student"; + b = a.createStatement(); + c = b.executeQuery(sql); + while (c.next()){ + int id = c.getInt("id"); + String name =c.getString("name"); + String sex = c.getString("sex"); + System.out.println(id+" "+name+" "+sex); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查"); + } catch (SQLException e) { + System.out.println("sql语句执行错误!"); + } + finally{ + try { + c.close(); + b.close(); + a.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} +``` + +添加功能 + +```java +package Topic1; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class A2 { + public static void main(String[] args) { + Connection a = null; + Statement b = 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"; + a = DriverManager.getConnection(url, username, password); + String sql="insert into student values(4,'赵六','男')"; + //获取 + b = a.createStatement(); + //添加 + int i = b.executeUpdate(sql); + if(i>0){ + System.out.println("添加成功"); + }else{ + System.out.println("添加失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查"); + } catch (SQLException e) { + System.out.println("sql语句执行错误!"); + } + try { + a.close(); + b.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +修改功能 + +```java +package Topic1; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +public class A3 { + public static void main(String[] args) { + Connection a = null; + Statement b = 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"; + a = DriverManager.getConnection(url, username, password); + String sql="update student set sex='女' where name='赵六'"; + //获取 + b = a.createStatement(); + //修改 + int i = b.executeUpdate(sql); + if(i>0){ + System.out.println("修改成功"); + }else{ + System.out.println("修改失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查"); + } catch (SQLException e) { + System.out.println("sql语句执行错误!"); + } + try { + a.close(); + b.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +删除功能 + +```java +package Topic1; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +public class A4 { + public static void main(String[] args) { + Connection a = null; + Statement b = 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"; + a = DriverManager.getConnection(url, username, password); + String sql="delete from student where name='王五'"; + //获取 + b = a.createStatement(); + //删除 + int i = b.executeUpdate(sql); + if(i>0){ + System.out.println("删除成功"); + }else{ + System.out.println("删除失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查"); + } catch (SQLException e) { + System.out.println("sql语句执行错误!"); + } + try { + a.close(); + b.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` +