From c2acdb5ebf097ac104472c423a7c5658427ed6fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=81=E6=B6=9B?= <1841582040@qq.com> Date: Wed, 17 May 2023 18:22:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?JDBC=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230517 JDBC.md" | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 "57 \351\273\204\346\265\201\346\266\233/20230517 JDBC.md" diff --git "a/57 \351\273\204\346\265\201\346\266\233/20230517 JDBC.md" "b/57 \351\273\204\346\265\201\346\266\233/20230517 JDBC.md" new file mode 100644 index 0000000..7f54374 --- /dev/null +++ "b/57 \351\273\204\346\265\201\346\266\233/20230517 JDBC.md" @@ -0,0 +1,208 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```mysql + create database student_db charset utf8; + use student_db; + create table student ( + id int, + name varchar(10), + sex char + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + + select * from student; + ``` + + + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + import java.sql.*; + + public class Select { + public static void main(String[] args) { + Connection conn = null; + Statement sta = null; + ResultSet res = null; + 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"; + conn = DriverManager.getConnection(url, username, password); + String sql = "select * from student"; + sta = conn.createStatement(); + res = sta.executeQuery(sql); + while (res.next()){ + int id = res.getInt("id"); + String name = res.getString("name"); + String sex = res.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 { + if (res != null){ + res.close(); + } + if (sta != null){ + sta.close(); + } + if (conn != null){ + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放错误"); + e.printStackTrace(); + } + } + } + } + + ``` + + + + 2. 添加功能 + + ```java + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + 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(); + } + } + } + + + ``` + + + + 3. 修改功能 + + ```java + 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(); + } + } + } + + ``` + + + + 4. 删除功能 + + ```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"); + 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 6547944675a7d200e9a0c2fc0d84f116318d6a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=81=E6=B6=9B?= <1841582040@qq.com> Date: Mon, 22 May 2023 21:08:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...26\350\257\221\345\260\201\350\243\205.md" | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 "57 \351\273\204\346\265\201\346\266\233/20230521 \351\242\204\347\274\226\350\257\221\345\260\201\350\243\205.md" diff --git "a/57 \351\273\204\346\265\201\346\266\233/20230521 \351\242\204\347\274\226\350\257\221\345\260\201\350\243\205.md" "b/57 \351\273\204\346\265\201\346\266\233/20230521 \351\242\204\347\274\226\350\257\221\345\260\201\350\243\205.md" new file mode 100644 index 0000000..3d0dfca --- /dev/null +++ "b/57 \351\273\204\346\265\201\346\266\233/20230521 \351\242\204\347\274\226\350\257\221\345\260\201\350\243\205.md" @@ -0,0 +1,202 @@ +```java +import java.sql.*; + +public class FZ { + // 获取连接对象 + private static final String url = "jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username = "root"; + private static final String password = "root"; + + // 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常"); + e.printStackTrace(); + } + } + + // 获取连接对象的方法 + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println("获取连接对象异常"); + e.printStackTrace(); + } + return conn; + } + + //查询的方法 + // 传入sql,返回结果集 + public static ResultSet Select (String sql,String... cs){ + ResultSet res = null; + try { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i 0){ + System.out.println("添加成功"); + }else{ + System.out.println("添加失败"); + } + + //修改 + String sql = "update student set name = ? ,sex = ? where id = ?"; + int i = FZ.Update(sql,"李六","男","4"); + if(i>0){ + System.out.println("修改成功"); + }else{ + System.out.println("修改失败"); + } + + //删除 + String sql = "delete from student where id = ?"; + int i = FZ.Delete(sql,"4"); + if(i>0){ + System.out.println("删除成功"); + }else{ + System.out.println("删除失败"); + } + + } +} + +``` + +```java +public class Studen { + private String id; + private String name; + private String sex; + public String getId() { + return id; + } + + public void setId(String 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; + } + + public Studen(int id, String name, String sex) { + } + + public Studen(String id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + @Override + public String toString() { + return "Studen{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } +``` + -- Gitee