diff --git "a/37 \351\222\237\345\207\244\350\213\261/20230516 JDBC\344\275\234\344\270\232.md" "b/37 \351\222\237\345\207\244\350\213\261/20230516 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..687dac13c63f252291cca163adb70de2395909f0 --- /dev/null +++ "b/37 \351\222\237\345\207\244\350\213\261/20230516 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,246 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + + ```mysql + CREATE DATABASE stuent_db CHARSET utf8; + //建立学生表student + CREATE TABLE student( + id int, + name VARCHAR(5), + sex char(1) + ); + INSERT INTO student VALUES + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); + ``` + + + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + + + ```java + import java.sql.*; + + public class Select { + public static void main(String[] args) { + //注册驱动 + ResultSet re = null; + Statement st = null; + Connection conn = 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); + //编写SQL语句 + String sql = "SELECT * FROM student"; + //获取执行SQL的对象 + st = conn.createStatement(); + //执行SQL语句 + re = st.executeQuery(sql); + //处理返回的结果 + while (re.next()) { + int id = re.getInt("id"); + String name = re.getString("name"); + System.out.println(id + name); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动导入异常"); + } catch (SQLException e) { + System.out.println("SQL发生了异常"); + } finally { + try { + if (re!=null){ + re.close(); + } + if (st!=null) { + st.close(); + } + if (conn!=null) { + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + } + } + import com.mysql.jdbc.Driver; + + import java.sql.*; + + public class Insert{ + public static void main(String[] args) { + //注册驱动 + Statement st = null; + Connection conn = 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); + //编写SQL语句 + String sql = "INSERT INTO student VALUES(4,'吴果','女')"; + //获取执行SQL的对象 + st = conn.createStatement(); + //执行SQL语句 + int i = st.executeUpdate(sql); + //处理返回的结果 + if (i>0){ + System.out.println("成功添加了"+i+"行"); + }else{ + System.out.println("添加了"+i+"行"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动导入异常"); + } catch (SQLException e) { + System.out.println("SQL发生了异常"); + } finally { + //释放资源 + try { + if (st!=null) { + st.close(); + } + if (conn!=null) { + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + + } + } + import com.mysql.jdbc.Driver; + + import java.sql.*; + + public class Update{ + public static void main(String[] args) { + //注册驱动 + Statement st = null; + Connection conn = 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); + //编写SQL语句 + String sql = "UPDATE student set name='优果' WHERE id=4"; + //获取执行SQL的对象 + st = conn.createStatement(); + //执行SQL语句 + int i = st.executeUpdate(sql); + //处理返回的结果 + System.out.println("成功修改了"+i+"行"); + } catch (ClassNotFoundException e) { + System.out.println("驱动导入异常"); + } catch (SQLException e) { + System.out.println("SQL发生了异常"); + } finally { + //释放资源 + try { + + if (st!=null) { + st.close(); + } + if (conn!=null) { + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + + } + } + + import com.mysql.jdbc.Driver; + + import java.sql.*; + + public class Delete{ + public static void main(String[] args) { + //注册驱动 + Statement st = null; + Connection conn = 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); + //编写SQL语句 + String sql = "DELETE FROM student WHERE id=3"; + //获取执行SQL的对象 + st = conn.createStatement(); + //执行SQL语句 + int i = st.executeUpdate(sql); + //处理返回的结果 + if (i>0){ + System.out.println("成功删除了"+i+"行"); + }else{ + System.out.println("删除了"+i+"行"); + } + } catch (ClassNotFoundException e) { + System.out.println("驱动导入异常"); + } catch (SQLException e) { + System.out.println("SQL发生了异常"); + } finally { + //释放资源 + try { + if (st!=null) { + st.close(); + } + if (conn!=null) { + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } + } + } + + ``` + + 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + + 2. 能否实现将查询的结果,封装成java对象 + + ```java + + ``` + + \ No newline at end of file