diff --git "a/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" "b/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..6e2e44e627dc80d3b86abb1421b6104b41fbad6b --- /dev/null +++ "b/15 \346\262\210\347\206\231\347\245\245/20230517 \344\275\234\344\270\232.md" @@ -0,0 +1,204 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 查询功能,查询student中所有数据 + + ```java + 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