diff --git "a/22 \345\274\240\351\276\231\350\205\276/20230518 JDBC\344\275\234\344\270\232.md" "b/22 \345\274\240\351\276\231\350\205\276/20230518 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..da4bafe93dc776cd3f32e614811f01707ae7de49 --- /dev/null +++ "b/22 \345\274\240\351\276\231\350\205\276/20230518 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,178 @@ +~~~ java +import java.sql.*; +import java.util.Scanner; + +public class JDBC { + // ### 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对象 + public static void main(String[] args) { + out: + for (; ; ) { + Scanner scanner = new Scanner(System.in); + System.out.println("输入你要的功能:\n" + + "\t1,查询功能\n" + + "\t2 添加功能\n" + + "\t3 修改功能\n" + + "\t4 删除功能\n" + + "\t5 退出"); + String choose = scanner.next(); + switch (choose) { + case "1": + System.out.println("你选择了查询功能"); + Inquire();//查询功能 + break; + case "2": + System.out.println("你选择了添加功能"); + Add(); //添加功能 + break; + case "3": + System.out.println("你选择了修改功能"); + Alter(); //修改功能 + break; + case "4": + System.out.println("你选择了删除功能"); + Delete(); //删除功能 + break; + case "5": + System.out.println("你选择了退出功能"); + break out; + default: + System.out.println("对不起,没有这个功能!"); + + + } + } + } + + public static void Add() { +// 添加功能 + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + Connection connection = DriverManager.getConnection(url, usename, password); + String Add = "insert into student values(1,'张三','男'),(2,'李四','女'),(3,'王五','男'),(4,'老六','男')"; + Statement statement = connection.createStatement(); + int i = statement.executeUpdate(Add); + if (i > 0) { + System.out.println("添加成功,添加了" + i + "行"); + } else { + System.out.println("添加失败"); + } + connection.close(); + statement.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + } + + public static void Delete() { + // 删除功能 + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + Connection connection = DriverManager.getConnection(url, usename, password); + String delete = "delete from student where id=4"; + Statement statement = connection.createStatement(); + int i = statement.executeUpdate(delete); + if (i > 0) { + System.out.println("删除成功,删除了" + i + "行"); + } else { + System.out.println("删除失败!"); + } + connection.close(); + statement.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public static void Alter() { + // 修改功能 + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + Connection connection = DriverManager.getConnection(url, usename, password); + String alter = "update student set name='李隐' where id=4"; + Statement statement = connection.createStatement(); + int i = statement.executeUpdate(alter); + if (i > 0) { + System.out.println("修改成功,修改了" + i + "行"); + } else { + System.out.println("修改失败!"); + } + connection.close(); + statement.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public static void Inquire() { + + try { +// 查询功能 +// 1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); +// 2.链接对象 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String usename = "root"; + String password = "root"; + Connection connection = DriverManager.getConnection(url, usename, password); +// 3.SQL的内容 + String sql = "select *from student"; +// 4.获取执行SQL的内容 + Statement statement = connection.createStatement(); +// 5.运行SQL + ResultSet resultSet = statement.executeQuery(sql); +// 6.处理结果 + while (resultSet.next()) { + int id = resultSet.getInt(1); + String name = resultSet.getString(2); + String sex = resultSet.getString(3); + System.out.println("ID:" + id + " 姓名:" + name + " 性别:" + sex); + } +// 7.释放资源 + connection.close(); + statement.close(); + resultSet.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} + + +~~~ + diff --git "a/22 \345\274\240\351\276\231\350\205\276/20230521 JDBC\345\267\245\345\205\267\345\214\205\344\275\234\344\270\232.md" "b/22 \345\274\240\351\276\231\350\205\276/20230521 JDBC\345\267\245\345\205\267\345\214\205\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..85fe8b166e8e12f800f8edea037796cc54abba64 --- /dev/null +++ "b/22 \345\274\240\351\276\231\350\205\276/20230521 JDBC\345\267\245\345\205\267\345\214\205\344\275\234\344\270\232.md" @@ -0,0 +1,194 @@ +~~~mysql +CREATE DATABASE student CHARACTER SET utf8; +USE student; +CREATE TABLE student( +id int, +name varchar(10), +sex varchar(1) +); +insert into student values(1,"张三","男"), +(2,"李四","女"), +(3,"王五","男"); +~~~ + + + +~~~ java +package zuoye; + +import java.sql.*; +import java.util.ArrayList; + +public class DBUtil { + // 1.用静态代码块,注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + // 2.封装主机,用户,密码 + private static final String url = "jdbc:mysql://localhost:3306/student?useSSL=false&allowPublicKeyRetrieval=true&useUnicode&characterEncoding"; + private static final String usename = "root"; + private static final String password = "36932211"; + + // 3.获取连接对象 + public static Connection getConn() { + Connection conn = null; + try { + conn = DriverManager.getConnection(url, usename, password); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + + // 4.定义通用查询方法 + + ArrayList list = new ArrayList<>(); + public static ArrayList Query(String sql, ArrayList list, String... keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement ps = conn.prepareStatement(sql); + ResultSet rs = ps.executeQuery(); + try { + Set(ps, keys); + while (rs.next()) { + Student stu = new Student(); + stu.setId(rs.getInt("id")); + stu.setName(rs.getString("name")); + stu.setSex(rs.getString("sex")); + list.add(stu); + } + return list; + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + CloseAll(conn, ps, rs); + } + } + + // 5.定义通用的update方法 + public static int update(String sql, String... keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement ps = conn.prepareStatement(sql); + try { + ps = conn.prepareStatement(sql); +// 向SQL设置参数 + Set(ps, keys); +// 执行SQL语句 + return ps.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + CloseAll(conn, ps, null); + } + } + + // 6. query和update都需要用到设置参数,所以设置一个设置参数的方法 +// 定义通用的设置参数方法 + public static void Set(PreparedStatement ps, String... keys) { + try { + if (keys != null) { + for (int i = 0; i < keys.length; i++) { + ps.setString(i + 1, keys[i]); + } + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + // 7.通用的释放资源方法 + public static void CloseAll(Connection conn, PreparedStatement ps, ResultSet res) throws SQLException { + + if (res != null) { + res.close(); + } + if (conn != null) { + conn.close(); + } + if (ps != null) { + ps.close(); + } + } + +} + +~~~ + + + +~~~ java +package zuoye; + +public class Student { + private int id; + private String name; + private String sex; + + Student() { + } + + 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 +package zuoye; +import java.sql.SQLException; +import java.util.ArrayList; + +public class StudentSystem { + + // 今天作业:使用新的执行SQL的对象,封装一个工具类, +// 然后用这个工具类查询上次+作业的数据库, +// 实现将查询结果封装成学生对象, +// 最后存入一个集合窗口,遍历这个集合的对象 + public static void main(String[] args) throws SQLException { + ArrayList list = new ArrayList<>(); + String sql = "select * from student"; + ArrayList liste = DBUtil.Query(sql,list); + System.out.println("id\t姓名\t性别"); + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getSex()); + } + } +} + + + +~~~ +