diff --git "a/31 \346\235\216\346\254\243/20230520 \344\275\234\344\270\232.md" "b/31 \346\235\216\346\254\243/20230520 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..87d15af61415c17e38a06a1ea6b4f96d4c4094fa --- /dev/null +++ "b/31 \346\235\216\346\254\243/20230520 \344\275\234\344\270\232.md" @@ -0,0 +1,187 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + import org.omg.CORBA.UserException; + + import java.sql.*; + public class Select { + public static void main(String[] args) { + 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 = "select *from student"; + //获取对象 + Statement st = conn.createStatement(); + //执行 + ResultSet re = st.executeQuery(sql); + //处理 + while (re.next()){ + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id + name + sex ); + } + //释放 + re.close(); + + */ + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + ``` + + ​ + + 2. 添加功能 + + ```java + import org.omg.CORBA.UserException; + + import java.sql.*; + public class Add { + public static void main(String[] args) { + 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(null,'六留',2)"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i>0){ + System.out.println("增加了" + i + "行"); + } else{ + System.out.println("错误"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + ``` + + + + 3. 修改功能 + + ```java + import org.omg.CORBA.UserException; + + import java.sql.*; + public class Update { + public static void main(String[] args) { + 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 sex='男' where id = 4 "; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i>0){ + System.out.println("修改了" + i + "行"); + } else{ + System.out.println("错误"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + ``` + + + + 4. 删除功能 + + ```java + import org.omg.CORBA.UserException; + + import java.sql.*; + public class Delete { + public static void main(String[] args) { + 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 = 4 "; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i>0){ + System.out.println("删除了" + i + "行"); + } else{ + System.out.println("错误"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + ``` + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + + 2. 能否实现将查询的结果,封装成java对象 + + + + + + + + +