From 6fa8ac65f64ae66394d7138741f96f3d3e24be6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E4=BA=A8=E4=BC=9F?= <529310475@qq.com> Date: Wed, 17 May 2023 23:16:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?0517=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0517JDBC\344\275\234\344\270\232.md" | 204 ++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 "11 \351\202\271\344\272\250\344\274\237/0517JDBC\344\275\234\344\270\232.md" diff --git "a/11 \351\202\271\344\272\250\344\274\237/0517JDBC\344\275\234\344\270\232.md" "b/11 \351\202\271\344\272\250\344\274\237/0517JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000..2a8a016 --- /dev/null +++ "b/11 \351\202\271\344\272\250\344\274\237/0517JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,204 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. CREATE DATABASE student_db CHARSET utf8; + USE student_db; + CREATE TABLE student( + id int, + name VARCHAR(10), + sex VARCHAR(10) + ); + INSERT INTO student + VALUES + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +6. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. + + 3. ``` + import java.sql.*; + + public class Test02 { + public static void main(String[] args) { + ResultSet res = null; + Connection conn = null; + Statement smt = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String usename="root"; + String password="root"; + conn = DriverManager.getConnection(url,usename,password); + String sql = "select * from student"; + smt = conn.createStatement(); + res = smt.executeQuery(sql); + while (res.next()){ + int id = res.getInt("id"); + String sex = res.getString("sex"); + String name = res.getString("name"); + System.out.println(id+"--"+name+"--"+sex); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException s) { + System.out.println("sql语句错误"); + s.printStackTrace(); + } + try { + if (res != null){ + res.close(); + } + if (smt != null){ + smt.close(); + } + if (conn != null){ + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放错误"); + e.printStackTrace(); + } + } + + // public static void main(String[] args) { + // Class.forName("com.mysql.jdbc.Driver"); + // String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + // String usename="root";//数据库用户名 + // String password="root";//数据库密码 + // String sql = "select * from studen"; + // Connection conn = DriverManager.getConnection(url, usename, password); + // Statement re =conn.createStatement(); + // } + } + ``` + + 4. 添加功能 + + 5. + + 6. ``` + Insert + public class Insert { + 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 (4,'王六','女')"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("添加成功"); + }else { + System.out.println("添加失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } + ``` + + 7. 修改功能 + + 8. + + 9. ``` + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + 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 name = '王六'"; + Statement sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } + ``` + + 10. 删除功能 + + 11. + + 12. ``` + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + 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 sta = conn.createStatement(); + int i = sta.executeUpdate(sql); + if(i > 0){ + System.out.println("删除成功"); + }else { + System.out.println("删除失败"); + } + sta.close(); + conn.close(); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + } catch (SQLException e) { + System.out.println("sql语句错误"); + e.printStackTrace(); + } + } + } + ``` + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file -- Gitee From 6b8288bf8ed50afd52a79f3d933d64ea6c8c35b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E4=BA=A8=E4=BC=9F?= <529310475@qq.com> Date: Mon, 22 May 2023 23:12:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?0522=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0522\344\275\234\344\270\232.md" | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 "11 \351\202\271\344\272\250\344\274\237/0522\344\275\234\344\270\232.md" diff --git "a/11 \351\202\271\344\272\250\344\274\237/0522\344\275\234\344\270\232.md" "b/11 \351\202\271\344\272\250\344\274\237/0522\344\275\234\344\270\232.md" new file mode 100644 index 0000000..bf7b42c --- /dev/null +++ "b/11 \351\202\271\344\272\250\344\274\237/0522\344\275\234\344\270\232.md" @@ -0,0 +1,158 @@ +```` +import java.sql.*; + +public class Inquire { + public static void main(String[] args) { + //1.注册驱动 + ResultSet rst = null; + Statement smt = null; + Connection conn = null; + try { + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + conn = DriverManager.getConnection(url, username, password); +// 3.编写SQL语句 + String sql = "SELECT * from student"; + //4.获取执行SQl的对象 + smt = conn.createStatement(); + //5.执行SQL + rst = smt.executeQuery(sql); +// 6.处理返回结果 + 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语句错误!"); + } + finally { + //7.释放资源 + try { + if (rst!=null){ + rst.close(); + } + if (smt!=null){ + smt.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + } +} + +``` + +```java +//添加 +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Add { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql = "insert into student values(4,'小六','男')"; + Statement se = conn.createStatement(); + int i = se.executeUpdate(sql); + if (i>0){ + System.out.println("插入成功"); + }else { + System.out.println("失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("你输入的驱动包错误,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } + } +} + +``` + +```java +//修改 +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Amend { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql = "update student set name='张三' where id=1 "; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i>0){ + System.out.println("成功修改了"+i+"行"); + }else { + System.out.println("修改失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("你输入的驱动包错误,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } + } +} + +``` + +```java +//删除 +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Delete { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接 + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url, username, password); + String sql ="delete from student where id=3"; + Statement ts = conn.createStatement(); + int i =ts.executeUpdate(sql); + if (i>0){ + System.out.println("删除成功!"); + System.out.println("删除了"+i+"行"); + }else { + System.out.println("删除失败"); + } + } catch (ClassNotFoundException e) { + System.out.println("你输入的驱动包错误,请检查!"); + } catch (SQLException e) { + System.out.println("SQL语句错误!"); + } + } +} + +``` +```` \ No newline at end of file -- Gitee