From 84d899480ce83a7e7db0c2e65c8251370262467b Mon Sep 17 00:00:00 2001 From: "100800@bin" <1761144610@qq.com> Date: Wed, 17 May 2023 23:30:07 +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 --- ...BC\344\275\234\344\270\232\357\274\232.md" | 224 ++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 "19 \345\275\255\345\213\207\346\226\214/JDBC\344\275\234\344\270\232\357\274\232.md" diff --git "a/19 \345\275\255\345\213\207\346\226\214/JDBC\344\275\234\344\270\232\357\274\232.md" "b/19 \345\275\255\345\213\207\346\226\214/JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000..4d13de5 --- /dev/null +++ "b/19 \345\275\255\345\213\207\346\226\214/JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,224 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + ResultSet res = null; + Statement sta = null; + Connection conn = 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"); + int id = res.getInt("id"); + String name = res.getString("name"); + String aye = res.getString("aye"); + System.out.println(id + name + aye); + } + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } finally { + try { + if(res!=null){ + res.close(); + + } + if(sta!=null){ + + sta.close(); + } + if(conn!=null){ + + conn.close(); + } + } catch (SQLException throwables) { + System.out.println("错误"); + } + ``` + + + + 2. 添加功能 + + ```java + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class ZY2 { + public static void main(String[] args) { + Statement smt = null; + Connection conn = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + // 语句 + String sql = "INSERT INTO student VALUES\n(4,'友利奈绪','女')"; + //执行 + smt = conn.createStatement(); + int i = smt.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语句错误"); + } 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 ZY3 { + public static void main(String[] args) { + Connection conn = null; + Statement smt = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + String sql = "UPDATE student SET NAME = '三月七' WHERE id = 2"; + smt = conn.createStatement(); + int i = smt.executeUpdate(sql); + 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 ZY4 { + 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=4"; + //4 获取执行 + stm = conn.createStatement(); + //5 执行sql语句 + int i = stm.executeUpdate(sql); + //6 处理返回结果 + if (i > 0) { + System.out.println("成功删除"); + System.out.println("删除成功"); + } else { + System.out.println("成功删除"); + System.out.println("删除失败"); + } + + } catch (ClassNotFoundException e) { + System.out.println("没有成功导入,请重试"); + } catch (SQLException e) { + System.out.println("错误"); + } finally { + //7 释放 + try { + stm.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + } + + ``` + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + + + + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file -- Gitee From 31ada634b45e4f11e1c8f884e78c3bb072f82c5c Mon Sep 17 00:00:00 2001 From: "100800@bin" <1761144610@qq.com> Date: Mon, 22 May 2023 23:04:29 +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 --- .../\345\260\201\350\243\205.md" | 205 ++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 "19 \345\275\255\345\213\207\346\226\214/\345\260\201\350\243\205.md" diff --git "a/19 \345\275\255\345\213\207\346\226\214/\345\260\201\350\243\205.md" "b/19 \345\275\255\345\213\207\346\226\214/\345\260\201\350\243\205.md" new file mode 100644 index 0000000..ec9c257 --- /dev/null +++ "b/19 \345\275\255\345\213\207\346\226\214/\345\260\201\350\243\205.md" @@ -0,0 +1,205 @@ +将id、name、sex封装起来,并放在集合里实现功能 + +```java +public class Test01{ + private int id; + private String name; + private String sex; + + public Test01() { + } + + public Test01(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; + } + + @Override + public String toString() { + return "编号:" + id + + ",姓名:" + name + + ",性别:" + sex ; + } +public class DBUtil { + private static final String url = "jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username = "root"; + private static final String password = "root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册异常"); + e.printStackTrace(); + } + } + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println("连接异常,检查url等"); + e.printStackTrace(); + } + return conn; + + } + public static ResultSet select(String select, Object ...values){ + Connection conn = getConn(); + ResultSet rs = null; + try { + PreparedStatement stmt = conn.prepareStatement(select); + for (int i = 0; i < values.length; i++) { + stmt.setObject((i+1),values[i]); + } + rs = stmt.executeQuery(); + } catch (SQLException e) { + System.out.println("SQL执行异常"); + e.printStackTrace(); + } + return rs; + } + public static int update(String sql,Object ...values){ + Connection conn = getConn(); + int num = 0; + try { + PreparedStatement stmt = conn.prepareStatement(sql); + for (int j = 0; j < values.length; j++) { + stmt.setObject((j+1),values[j]); + } + num = stmt.executeUpdate(); + } catch (SQLException e) { + System.out.println("SQL语句异常"); + e.printStackTrace(); + } + return num; + } +} +public class Test02{ + static Scanner sc = new Scanner(System.in); + static Connection conn = DBUtil.getConn(); + public static void main(String[] args) { + out: + while (true) { + System.out.print("=========操作序号列表========\n" + + "1.查询信息\n" + + "2.添加功能\n" + + "3.修改功能\n" + + "4.删除功能\n" + + "5.退出系统\n" + + "输入序号进行对应操作:"); + int i = 0; + try { + i = sc.nextInt(); + } catch (Exception e) { + System.out.println("非法字符!请按要求输入!"); + e.printStackTrace(); + } + switch (i){ + case 1: + selectTest01(); + break; + case 2: + addTest01(); + break; + case 3: + updateTest01(); + break; + case 4: + deleteTest01(); + break; + case 5: + try { + conn.close(); + } catch (SQLException e) { + System.out.println("SQL执行错误"); + e.printStackTrace(); + } + System.out.println("即将退出,期待下次登录"); + break out; + default: + System.out.println("请按要求进行操作!!"); + } + } + } + + private static void deleteTest01() { + System.out.print("请输入要删除的学生学号:"); + int id = sc.nextInt(); + int i = DBUtil.update("delete from student where id = ?",id); + if (i>0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + } + + private static void updateTest01() { + System.out.print("请输入要修改的学号:"); + int id = sc.nextInt(); + System.out.print("请输入要修改的姓名:"); + String name = sc.next(); + System.out.print("请输入要修改的性别:"); + String sex = sc.next(); + int i=DBUtil.update("update student set name =?,sex=? where id=? ",name,sex,id); + if (i>0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + } + + private static void addTest01() { + System.out.print("请输入学号:"); + int id = sc.nextInt(); + System.out.print("请输入姓名:"); + String name = sc.next(); + System.out.print("请输入性别男或女:"); + String sex = sc.next(); + int i = DBUtil.update("insert into student values (?,?,?)",id,name,sex); + if (i>0){ + System.out.println("添加成功!"); + } + } + + private static void selectTest01() { + ResultSet resultSet = DBUtil.select("select * from student"); + ArrayList list = new ArrayList<>(); + try { + while (resultSet.next()){ + list.add(new Test01(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + for (int i = 0; i < list.size(); i++) { + Test01 test01 = list.get(i); + System.out.println(test01); + } + } +} +``` \ No newline at end of file -- Gitee