From 1c22e3f1243a1ef3a7593ac84532ad514a4e32e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BC=BA?= <“3108497868@qq.com”> Date: Sat, 20 May 2023 12:01:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../230517jsp\344\275\234\344\270\232.md" | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 "45 \347\216\213\345\274\272/230517jsp\344\275\234\344\270\232.md" diff --git "a/45 \347\216\213\345\274\272/230517jsp\344\275\234\344\270\232.md" "b/45 \347\216\213\345\274\272/230517jsp\344\275\234\344\270\232.md" new file mode 100644 index 0000000..4dd8850 --- /dev/null +++ "b/45 \347\216\213\345\274\272/230517jsp\344\275\234\344\270\232.md" @@ -0,0 +1,141 @@ +```mysql +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(5),sex char(2)); + insert into student values + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); +``` + +```java +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + 2. 添加功能 + 3. 修改功能 + 4. 删除功能 + + +import java.sql.*; + +// 1. 查询功能,查询student中所有数据 +public class Select { + 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 = "select * from student"; + Statement st = conn.createStatement(); + ResultSet re = st.executeQuery(sql); + while (re.next()) { + int id = re.getInt("id"); + String name = re.getString("name"); + String sex = re.getString("sex"); + System.out.println(id+","+name+","+sex); + } + re.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +// 2. 添加功能 +class Add { + 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 st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >= 1) { + System.out.println("成功添加了"+i+"行数据"); + }else { + System.out.println("添加不了一点"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +// 3. 修改功能 +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 id = 5 where id = 4"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >= 1) { + System.out.println("成功修改了"+i+"行数据"); + }else { + System.out.println("修改不了一点"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +// 4. 删除功能 +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 = 5"; + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i >= 1) { + System.out.println("成功删除了"+i+"行数据"); + }else { + System.out.println("删除不了一点"); + } + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` -- Gitee