From ce9a3138d745a9f414509988ddc7d9feefe003ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B4=8B?= Date: Sat, 20 May 2023 08:51:55 +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 --- ...72\347\241\200\344\275\234\344\270\232.md" | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 "48 \346\235\250\346\264\213/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232.md" diff --git "a/48 \346\235\250\346\264\213/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232.md" "b/48 \346\235\250\346\264\213/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232.md" new file mode 100644 index 0000000..dd62c20 --- /dev/null +++ "b/48 \346\235\250\346\264\213/20230517 JDBC\345\237\272\347\241\200\344\275\234\344\270\232.md" @@ -0,0 +1,127 @@ +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + + ```sql + CREATE DATABASE student_db CHARSET utf8; + use student_db; + CREATE TABLE student( + id int , + name VARCHAR(4), + sex enum("男","女") + ); + INSERT into student VALUES + (1,"张三","男"),(2,"李四","女"),(3,"王五","男"); + ``` + + + +3. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + 5. ```java + import java.sql.*; + + public class composite { + public static void main(String[] args) throws SQLException, ClassNotFoundException { + select1(); + add2(); + update3(); + delete4(); + } + public static void select1() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + + //String url="jdbc:mysql://localhost/user?useSSL=false"; + String url="jdbc:mysql://localhost/student_db?useSSL=false"; + String username="root"; + String password="root"; + Connection conn = DriverManager.getConnection(url, username, password); + + //String sql="select * from math"; + String sql="select * from student"; + + Statement st = conn.createStatement(); + + ResultSet rs = st.executeQuery(sql);//专用来执行select语句,因为它会返回一个结果集 + //int i = st.executeUpdate(sql);//用来执行除了select以外的语句,返回的是一个受影响的函数 + + while (rs.next()){ + //结果集取值方式,结果集对象名.getXxx(索引,or 字段名) Xxx是数据类型 + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id+","+name+","+sex); + } + rs.close(); + st.close(); + conn.close(); + } + public static void add2() throws ClassNotFoundException, SQLException { + 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 (10,'朱元璋','男')"; // 添加 + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("添加成功了" + i + "行"); + } else System.out.println("添加失败" + i + "行"); + st.close(); + conn.close(); + } + public static void update3() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + //Connection conn = DriverManager.getConnection("jbdc:mysql//localhost:3306/user?useSSL=false", "root", "root"); + 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='周京泽' where id=10"; //修改 + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("修改成功了" + i + "行"); + } else System.out.println("修改失败" + i + "行"); + st.close(); + conn.close(); + } + public static void delete4() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + //Connection conn = DriverManager.getConnection("jbdc:mysql//localhost:3306/user?useSSL=false", "root", "root"); + 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=2"; //删除 + Statement st = conn.createStatement(); + int i = st.executeUpdate(sql); + if (i > 0) { + System.out.println("删除成功了" + i + "行"); + } else System.out.println("删除失败" + i + "行"); + st.close(); + conn.close(); + } + } + + ``` + + + +4. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file -- Gitee