From 0c1a25eda31d03ae9d2471284b839834cf4b3da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B8=85=E5=8D=8E?= Date: Fri, 19 May 2023 22:33:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?20230517=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230517 JDBC.md" | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 "01 \350\213\217\346\270\205\345\215\216/20230517 JDBC.md" diff --git "a/01 \350\213\217\346\270\205\345\215\216/20230517 JDBC.md" "b/01 \350\213\217\346\270\205\345\215\216/20230517 JDBC.md" new file mode 100644 index 0000000..95cf4b8 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20230517 JDBC.md" @@ -0,0 +1,159 @@ +### JDBC作业: + +1. MySQL中创建一个数据库 + + ```mysql + create database student_db charset utf8; + use student_db; + ``` + +2. 库中创建student表 + + ```mysql + create table student(sid int,sname varchar(5),ssex char(2)); + ``` + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```mysql + create table student(sid int,sname varchar(5),ssex char(2)); + ``` + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + package Student; + + import java.sql.*; + + public class Select { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql="select * from student"; + Statement st = conn.createStatement(); + ResultSet res = st.executeQuery(sql); + while(res.next()){ + int sid=res.getInt("sid"); + String sname=res.getString("sname"); + String ssex=res.getString("ssex"); + System.out.println((sid + sname + ssex)); + } + res.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + } + + ``` + + 2. 添加功能 + + ```java + public class Addition { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql="insert into student values(4,'牙牙','女')"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if(i>0){ + System.out.println("添加成功!"); + }else{ + System.out.println("添加失败!"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + ``` + + + + 3. 修改功能 + + ```java + public class Update { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql="update student set sname ='金昇玟墙头' where sid=4"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if(i>0){ + System.out.println("修改成功!"); + }else{ + System.out.println("修改失败!"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + ``` + + + + 4. 删除功能 + + ```java + public class Delete { + public static void main(String[] args) { + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql="delete from student where sname='王五'"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if(i>0){ + System.out.println("删除成功!"); + }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对象 \ No newline at end of file -- Gitee From c7d0f51dba78295785cc5d478df662ffed3d70b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B8=85=E5=8D=8E?= Date: Sun, 21 May 2023 21:13:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?20230520=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230520 \345\260\201\350\243\205.md" | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 "01 \350\213\217\346\270\205\345\215\216/20230520 \345\260\201\350\243\205.md" diff --git "a/01 \350\213\217\346\270\205\345\215\216/20230520 \345\260\201\350\243\205.md" "b/01 \350\213\217\346\270\205\345\215\216/20230520 \345\260\201\350\243\205.md" new file mode 100644 index 0000000..b67237c --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20230520 \345\260\201\350\243\205.md" @@ -0,0 +1,106 @@ +使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历这个集合的对象 + +```java +public class Student { + private int id; + private String name; + private String sex; + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + public Student() { + } + + public int getId() { + return id; + } + + public void setId(int 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; + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } +} + +``` + +```java +import java.sql.*; +import java.util.ArrayList; + +public class DBUtil { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&character=utf8", "root", "122319"); + return conn; + } + public static ResultSet Query(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + ResultSet res = pre.executeQuery(); + return res; + } + public static void close(Connection con,PreparedStatement pre,ResultSet res) throws SQLException { + if(res!=null){ + res.close(); + }if(pre!=null){ + pre.close(); + }if(con!=null){ + con.close(); + } + } +} +class Test{ + public static void main(String[] args) throws SQLException { + String sql="select * from student"; + ResultSet res = DBUtil.Query(sql); + ArrayList list = new ArrayList<>(); + while (res.next()){ + list.add(new Student(res.getInt(1),res.getString(2),res.getString(3))); + } + for (int i = 0; i < list.size(); i++) { + Student student = list.get(i); + System.out.println(student); + } + DBUtil.close(null,null,res); + } +} + +``` + -- Gitee