From 5b8032f06a59757e146bfd0ac5aae5dac3727a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B1=BD=E5=A2=83?= <1537282342@qq.com> Date: Sat, 20 May 2023 11:51:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=85=B3=E8=81=94mysql=E5=AD=A6=E7=94=9F?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\346\234\254\346\226\207\346\241\243.txt" | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 "28 \351\273\204\346\242\223\345\242\203/20230520/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git "a/28 \351\273\204\346\242\223\345\242\203/20230520/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/28 \351\273\204\346\242\223\345\242\203/20230520/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000..e664b05 --- /dev/null +++ "b/28 \351\273\204\346\242\223\345\242\203/20230520/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1,168 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ~~~MySQL + create database student_db default char set utf8; + use student_db; + create table student_db ( + id int , + name varchar(10), + sex char + ); + insert into student_db values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + ~~~ + + + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + 2. 添加功能 + 3. 修改功能 + 4. 删除功能 + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 + + ~~~java + import java.sql.*; + + public class Jdbc { + public static void main(String[] args) { + try { + select();//查询 + update();//修改 + insert();//添加 + delete();//删除 + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + + } + public static void select() throws SQLException, ClassNotFoundException { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接对象 + 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); + //3.定义SQL + String sql = "select * from sonyang;"; + + //4.获取执行SQL + Statement st = conn.createStatement(); + + //5.执行SQL + ResultSet rs = st.executeQuery(sql); + //6.处理返回结果 + System.out.println("id "+"name"); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + System.out.println(id+" "+name); + } + //7.释放资源 + rs.close(); + st.close(); + conn.close(); + } + public static void update() throws SQLException, ClassNotFoundException { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接对象 + 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); + //3.定义SQL + String sql = "update sonyang set name = '宋扬2号'where id=1"; + + //4.获取执行SQL + Statement st = conn.createStatement(); + + //5.执行SQL + int i = st.executeUpdate(sql); + //6.处理返回结果 + if (i>0){ + System.out.println("修改成功"); + }else { + System.out.println("修改失败"); + } + //7.释放资源 + st.close(); + conn.close(); + } + public static void insert() throws ClassNotFoundException, SQLException { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接对象 + 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); + //3.定义SQL + String sql = "insert into sonyang values (2,'邓琪plus')"; + //4.获取执行SQL + Statement st = conn.createStatement(); + + //5.执行SQL + int i = st.executeUpdate(sql); + //6.处理返回结果 + if (i>0){ + System.out.println("添加"+i+"行数据成功"); + }else { + System.out.println("添加失败"); + } + //7.释放资源 + st.close(); + conn.close(); + } + public static void delete() throws ClassNotFoundException, SQLException { + //1.注册驱动 + Class.forName("com.mysql.jdbc.Driver"); + //2.获取连接对象 + 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); + //3.定义SQL + String sql = "delete from sonyang where name='张三'"; + //4.获取执行SQL + Statement st = conn.createStatement(); + //5.执行SQL + int i = st.executeUpdate(sql); + //6.处理返回结果 + if (i>0){ + System.out.println("删除了"+i+"行数据成功"); + }else { + System.out.println("删除失败"); + } + //7.释放资源 + st.close(); + conn.close(); + + } + } + + ~~~ + + \ No newline at end of file -- Gitee From 79fac332f8817aeb7d3df8220c4293cc864b3dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B1=BD=E5=A2=83?= <1537282342@qq.com> Date: Sun, 21 May 2023 21:16:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=85=B3=E8=81=94=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\234\254\346\226\207\346\241\243 (2).txt" | 97 +++++++++++++++ ...7\346\234\254\346\226\207\346\241\243.txt" | 117 ++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 "28 \351\273\204\346\242\223\345\242\203/20230521/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243 (2).txt" create mode 100644 "28 \351\273\204\346\242\223\345\242\203/20230521/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git "a/28 \351\273\204\346\242\223\345\242\203/20230521/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243 (2).txt" "b/28 \351\273\204\346\242\223\345\242\203/20230521/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243 (2).txt" new file mode 100644 index 0000000..d8ca417 --- /dev/null +++ "b/28 \351\273\204\346\242\223\345\242\203/20230521/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243 (2).txt" @@ -0,0 +1,97 @@ +import java.sql.*; + +public class Jdbc { + private Connection conn() { + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEchoing=utf8"; + Connection conn = DriverManager.getConnection(url, "root", "root"); + return conn; + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + ResultSet select(String sql){ + try { + PreparedStatement pst = conn().prepareStatement(sql); + ResultSet re = pst.executeQuery(); + return re; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +```java +public class Student { + private int id; + private String name; + private String sex; + + public Student() { + } + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + 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; + } +} +``` + +```java +import java.sql.*; +import java.util.ArrayList; + +public class Test { + public static void main(String[] args) { + Jdbc jdbc = new Jdbc(); + ResultSet re = jdbc.select("select * from student"); + ArrayList stu = new ArrayList<>(); + try { + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + Student s = new Student(id, name, sex); + stu.add(s); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + System.out.println("ID\t\t姓名\t\t性别"); + for (int i = 0; i < stu.size(); i++) { + Student s = stu.get(i); + System.out.println(s.getId()+"\t\t"+s.getName()+"\t\t"+s.getSex()); + } + } +} \ No newline at end of file diff --git "a/28 \351\273\204\346\242\223\345\242\203/20230521/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/28 \351\273\204\346\242\223\345\242\203/20230521/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000..be573dd --- /dev/null +++ "b/28 \351\273\204\346\242\223\345\242\203/20230521/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1,117 @@ +表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + try { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/text?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); + System.out.println("编号\t\t姓名\t\t性别"); + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + System.out.println(id+"\t\t"+name+"\t\t"+sex); + } + re.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + + + 2. 添加功能 + + ```java + 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,'A','男')"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("添加了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + + + 3. 修改功能 + + ```java + 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 name = 'B' where name = 'A'"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("修改了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + ``` + + + + 4. 删除功能 + + ```java + 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 = 1"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + System.out.println("删除了"+i+"行"); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 -- Gitee