diff --git "a/36 \350\265\265\345\220\257\346\201\222/20223.5.21.txt" "b/36 \350\265\265\345\220\257\346\201\222/20223.5.21.txt" new file mode 100644 index 0000000000000000000000000000000000000000..e04b6ec2ae510d1a1c30baac43ebe02eab288955 --- /dev/null +++ "b/36 \350\265\265\345\220\257\346\201\222/20223.5.21.txt" @@ -0,0 +1,182 @@ +try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url,username,password); + String sql = "select * from student"; + Statement st = conn.createStatement(); + ResultSet re = st.executeQuery(sql); + System.out.println("编号\t\t姓名\t\t性别"); + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id+"\t\t"+name+"\t\t"+sex); + } + re.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + 添加功能 + + ```java + 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,'A','男')"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("添加了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + 修改功能 + + ```java + 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 name = 'B' where name = 'A'"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("修改了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + 删除功能 + + ```java + 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 = 1"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("删除了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 + + ```java + import java.sql.*; + + public class Jdbc { + + //注册+连接 + Connection conn() { + 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); + Statement st = conn.createStatement(); + return conn; + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //添加 + void add(String sql){ + Connection conn = conn(); + try { + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("添加了"+i+"行"); + st.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //删除 + void delete(String sql){ + Connection conn = conn(); + try { + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("删除了"+i+"行"); + st.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //修改 + void update(String sql){ + Connection conn = conn(); + try { + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("修改了"+i+"行"); + st.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //查询 + void select(String sql){ + Connection conn = conn(); + try { + Statement st = conn.createStatement(); + ResultSet re = st.executeQuery(sql); + System.out.println("编号\t\t姓名\t\t性别"); + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id+"\t\t"+name+"\t\t"+sex); + } + re.close(); + st.close(); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + } \ No newline at end of file diff --git "a/36 \350\265\265\345\220\257\346\201\222/2023.5.20.txt" "b/36 \350\265\265\345\220\257\346\201\222/2023.5.20.txt" new file mode 100644 index 0000000000000000000000000000000000000000..ae85ade2aaf4d6ffccc2b0b8660d60ff91587447 --- /dev/null +++ "b/36 \350\265\265\345\220\257\346\201\222/2023.5.20.txt" @@ -0,0 +1,90 @@ +ResultSet re = null; + try { + //1 加载MySQL驱动类 + Class.forName("com.mysql.cj.jdbc.Driver"); + //2 创建Mysql连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + Connection conn = DriverManager.getConnection(url, "root", "200311"); + //3 创建sql语句对象 + String sql = "select * from student"; + //4 创建执行sqk语句的对象 + Statement st = conn.createStatement(); + //5 执行sql语句 + re = st.executeQuery(sql); + //6 执行返回结果 + while (re.next()) { + stu stu = new stu(); + stu.setId(re.getInt(1)); + stu.setName(re.getString(2)); + stu.setSex(re.getString(3)); + list.add(stu); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包异常"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("sql语句异常"); + e.printStackTrace(); + } + + +添加 +try { + Class.forName("com.mysql.cj.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + Connection conn = DriverManager.getConnection(url, "root", "200311"); + Statement st = conn.createStatement(); + int i = st.executeUpdate("insert into student values (4,'王四','女')"); + if (i > 0) { + System.out.println("添加成功"); + } else { + System.out.println("添加失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包异常"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("sql语句异常"); + e.printStackTrace(); + } + +修改 +try { + Class.forName("com.mysql.cj.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + Connection conn = DriverManager.getConnection(url, "root", "200311"); + Statement st = conn.createStatement(); + int i = st.executeUpdate("update student set name='四四' where id=2"); + if (i > 0) { + System.out.println("修改成功"); + } else { + System.out.println("修改失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包异常"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("sql语句异常"); + e.printStackTrace(); + } + + +删除 +try { + Class.forName("com.mysql.cj.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false"; + Connection conn = DriverManager.getConnection(url, "root", "200311"); + Statement st = conn.createStatement(); + int i = st.executeUpdate("delete from student where id=4"); + if (i > 0) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包异常"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("sql语句异常"); + e.printStackTrace(); + } \ No newline at end of file