diff --git "a/03 \350\265\226\345\277\203\345\246\215/20230516 JDBC\344\275\234\344\270\232\357\274\232.md" "b/03 \350\265\226\345\277\203\345\246\215/20230516 JDBC\344\275\234\344\270\232\357\274\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..cfcbc6ef4e4f5f34acad1ef81c5abec8827e6171 --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20230516 JDBC\344\275\234\344\270\232\357\274\232.md" @@ -0,0 +1,175 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + public class Select { + public static void main(String[] args) { + ResultSet rst = null; + Statement smt = null; + Connection conn = null; + try { + Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String user = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, user, password); + String sql = "select * from student"; + smt = conn.createStatement(); + rst = smt.executeQuery(sql); + while (rst.next()) { + int id = rst.getInt("id"); + String name = rst.getString("name"); + String sex = rst.getString("sex"); + System.out.println(id + ":" + name + ":" + sex); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("sql语句执行错误!"); + e.printStackTrace(); + } finally { + try { + rst.close(); + smt.close(); + conn.close(); + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + } + } + ``` + + 2. 添加功能 + + ``` + public class Insert { + public static void main(String[] args) { + Statement st = null; + Connection conn = null; + try { + Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&Unicode=true&characterEncoding=utf8"; + String user = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, user, password); + String sql = "insert into student values(4,'娜娜','女')"; + st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("成功插入" + i + "行"); + } else { + System.out.println("插入失败,插入" + i + "行"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("sql执行语句错误!"); + e.printStackTrace(); + } finally { + try { + st.close(); + conn.close(); + } catch (SQLException e) { + System.out.println("释放资源失败"); + } + } + } + } + ``` + + 3. 修改功能 + + ```java + public class Update { + public static void main(String[] args) { + Statement st = null; + Connection conn = null; + try { + Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String user = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, user, password); + String sql = "update student set name='NANA' where id=4"; + st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("成功修改" + i + "行"); + } else { + System.out.println("修改失败,修改" + i + "行"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("sql语句执行错误!"); + e.printStackTrace(); + } finally { + try { + st.close(); + conn.close(); + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + } + } + ``` + + 4. 删除功能 + + ```java + public class Delete { + public static void main(String[] args) { + Statement st = null; + Connection conn = null; + try { + Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); + String sql = "delete from Student where id=4"; + st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("删除成功,删除了" + i + "行"); + } else { + System.out.println("删除失败,删除了" + i + "行"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动包没有正确导入,请检查!"); + } catch (SQLException e) { + System.out.println("sql语句执行错误!"); + e.printStackTrace(); + } finally { + try { + st.close(); + conn.close(); + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + } + } + ``` + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file