diff --git "a/33 \347\206\212\346\231\257\345\263\260/20230520JDBC\344\275\234\344\270\232\357\274\232.md" "b/33 \347\206\212\346\231\257\345\263\260/20230520JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..aa97d9ee04e043e755fb2b977a34221a7b90d9f2 --- /dev/null +++ "b/33 \347\206\212\346\231\257\345\263\260/20230520JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,308 @@ +### 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 default charset utf8; +use student_db; +create table student ( + id int not null, + name varchar(10) not null, + gender varchar(1) not null +); +insert into student values +(1,'张三', '男'), +(2,'李四', '女'), +(3,'王五', '男'); +``` + + + +```java +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(); + } +``` + +````java +添加 + 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(); + } +```` + +``` +封装类 + +```java +public class stu { + private int id; + private String name; + private String sex; + + public stu() { + } + + public stu(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 +import java.sql.*; +import java.util.ArrayList; + +public class Jdbc{ + //1. 查询功能,查询student中所有数据 + ArrayList list = new ArrayList<>(); + void Find(ArrayList list) { + 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(); + } + } + //2. 添加功能 + void Add() { + 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(); + } + + } + //3. 修改功能 + void Amend() { + 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(); + } + } + //4. 删除功能 + void Delete(){ + 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(); + } + } +} +``` + +测试 + +```java +import java.sql.SQLException; +import java.util.ArrayList; + +public class Text { + public static void main(String[] args) throws SQLException { + ArrayList list = new ArrayList<>(); + Jdbc jdbc = new Jdbc(); + jdbc.Find(list); + for (int i = 0; i < list.size(); i++) { + stu stu = list.get(i); + System.out.println(stu.getId()+" "+stu.getName()+" "+stu.getSex()); + } + } +} +```` + diff --git "a/33 \347\206\212\346\231\257\345\263\260/20230521JDBC\344\275\234\344\270\232.md" "b/33 \347\206\212\346\231\257\345\263\260/20230521JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..99e65c14004c2c37332283beffc43801390daf2e --- /dev/null +++ "b/33 \347\206\212\346\231\257\345\263\260/20230521JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,134 @@ +### 使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历之个集合的对象 + +```java +public class Studentdx { + private int id; + private String name; + private String sex; + + public Studentdx() { + } + + public Studentdx(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; + } +} + ---------------------------------------------------------- +import java.sql.*; +import java.util.ArrayList; + +public class Student { + private static final String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String user="root"; + private static final String pass ="200311"; + + static { + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + + } + + public static Connection link() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pass); + return conn; + } + //查询 + ArrayList list = new ArrayList<>(); + public static ArrayList Query(String sql,ArrayList list,String ...flop) throws SQLException { + Connection conn = link(); + PreparedStatement ps = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + ps.setString((i+1),flop[i]); + } + ResultSet rs = ps.executeQuery(); + while (rs.next()){ + Studentdx stu = new Studentdx(); + stu.setId(rs.getInt("id")); + stu.setName(rs.getString("name")); + stu.setSex(rs.getString("sex")); + list.add(stu); + } + return list; + } + //添加,删除,修改 + public static int update(String sql,String ...flop) throws SQLException { + Connection conn = link(); + PreparedStatement ps = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + ps.setString((i+1),flop[i]); + } + int i = ps.executeUpdate(); + return i; + } + //释放 + public static void release(Connection conn,PreparedStatement ps,ResultSet rs) throws SQLException { + if (rs!=null){ + rs.close(); + } + if (ps!=null){ + ps.close(); + } + if (conn!=null){ + conn.close(); + } + } + public static void release(Connection conn,PreparedStatement ps) throws SQLException { + if (ps!=null){ + ps.close(); + } + if (conn!=null){ + conn.close(); + } + } +} +------------------------------------------------------------ +import java.sql.SQLException; +import java.util.ArrayList; + +public class Text2 { + public static void main(String[] args) throws SQLException { + //今天作业:使用新的执行SQL的对象,封装一个工具类, + // 然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象, + // 最后存入一个集合窗口,遍历之个集合的对象 + ArrayList list = new ArrayList<>(); + String sql = "select * from student"; + ArrayList liste = Student.Query(sql, list); + System.out.println("id\t姓名\t性别"); + for (int i = 0; i < liste.size(); i++) { + Studentdx stu = liste.get(i); + System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getSex()); + } + } +} +``` +