diff --git "a/03 \345\276\220\351\233\250\346\231\264/20230517 JSP\345\205\245\351\227\250\342\200\224\342\200\224JDBC.md" "b/03 \345\276\220\351\233\250\346\231\264/20230517 JSP\345\205\245\351\227\250\342\200\224\342\200\224JDBC.md" new file mode 100644 index 0000000000000000000000000000000000000000..9bc88c305591f66102a84b3b3db55cd7f4d3e0c5 --- /dev/null +++ "b/03 \345\276\220\351\233\250\346\231\264/20230517 JSP\345\205\245\351\227\250\342\200\224\342\200\224JDBC.md" @@ -0,0 +1,428 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +```java +mysql> create database student_db charset utf8; +Query OK, 1 row affected (0.00 sec) + +mysql> use student_db; +Database changed +``` + +1. 库中创建student表 + +```java +mysql> create table student( + -> id int, + -> `name` varchar(5), + -> sex varchar(2) + -> ); +Query OK, 0 rows affected (0.06 sec) +``` + +1. 表中数据如下 + +```java +insert into student values (1,'张三','男'),(2,'李四','女'),(3,'王五','男'); +``` + +1. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +2. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + 2. 添加功能 + 3. 修改功能 + 4. 删除功能 + +```java +import java.sql.*; + +public class Select { + //查询功能 + public static void main(String[] args) { + try { + //0 导入驱动包 + //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 student"; + //4 获取执行SQL的对象 + Statement st= conn.createStatement(); + //5 执行SQL语句 + ResultSet rs=st.executeQuery(sql); + //6 处理返回结果 + while (rs.next()) { + int id=rs.getInt("id"); + String name=rs.getString("name"); + String sex=rs.getString("sex"); + System.out.println(id+" "+name+" "+sex); + } + //7 释放资源 + rs.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +import java.sql.*; + +public class Add { + public static void main(String[] args) { + //添加功能 + try { + //0 导入驱动包 + //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 student values (4,'赵梵','女')"; + //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(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Update { + public static void main(String[] args) { + //修改功能 + try { + //0 导入驱动包 + //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 student set sex='男' where id=2"; + //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(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class Delete { + public static void main(String[] args) {、 + //删除功能 + try { + //0 导入驱动包 + //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 student where id=3"; + //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(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +1. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 + +```java +import java.sql.*; + +public class Test { + //第一版 + public static final String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + public static final String username="root"; + public static final String password="root"; + public static void main(String[] args) throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn= DriverManager.getConnection(url,username,password); + String sql="select * from student"; + String sql1="insert into student values (4,'赵梵','女')"; + String sql2="Update student set sex='男' where id=2"; + String sql3="Delete from student where id=3"; + Statement st= conn.createStatement(); + ResultSet rs=st.executeQuery(sql); + if (sql.equals("select * from student")) { + while (rs.next()) { + int id=rs.getInt("id"); + String name=rs.getString("name"); + String sex=rs.getString("sex"); + System.out.println(id+" "+name+" "+sex); + } + } + Statement st1= conn.createStatement(); + int i=st1.executeUpdate(sql1); + if(sql1.equals("insert into student values (4,'赵梵','女')")){ + if (i >0) { + System.out.println("成功添加了"+i+"行数据!"); + }else{ + System.out.println("添加失败!请重新添加!"); + } + } + Statement st2= conn.createStatement(); + int i1=st2.executeUpdate(sql2); + if(sql2.equals("Update student set sex='男' where id=2")){ + if (i1 >0) { + System.out.println("成功修改了"+i1+"行数据!"); + }else{ + System.out.println("添加失败!请重新修改!"); + } + } + Statement st3= conn.createStatement(); + int i2 =st3.executeUpdate(sql3); + if(sql3.equals("Delete from student where id=3")){ + if (i2 >0) { + System.out.println("成功删除了"+ i2 +"行数据!"); + }else{ + System.out.println("添加失败!请重新删除!"); + } + } + } +} +class Select1 extends Test{ + public static void select(){ + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn= DriverManager.getConnection(url,username,password); + String sql="select * from student"; + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +class Add extends Test{ + public static void add(){ + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn= DriverManager.getConnection(url,username,password); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +class Update1 extends Test{ + public static void update(){ + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn= DriverManager.getConnection(url,username,password); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +class Delect extends Test{ + public static void delect(){ + try { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn= DriverManager.getConnection(url,username,password); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + +} +``` + +```java +import java.sql.*; +import java.util.Scanner; + +public class Homework { + //第二版 + private static final String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String password="root"; + + 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(url, username, password); + return conn; + } + public static ResultSet query(String sql, String ...keys) throws SQLException { + Connection conn=getConn(); + PreparedStatement ps = conn.prepareStatement(sql); + //赋值到对应?号处 + for (int i = 0; i < keys.length; i++) { + ps.setString((i+1),keys[i]); + } + ResultSet rs=ps.executeQuery(); + return rs; + } + public static int update(String sql,String ...keys) throws SQLException { + Connection conn=getConn(); + PreparedStatement ps = conn.prepareStatement(sql); + //赋值到对应?号处 + for (int i = 0; i < keys.length; i++) { + ps.setString((i+1),keys[i]); + } + int i=ps.executeUpdate(); + return i; + } + //通用释放方法 + public static void close(Connection conn,PreparedStatement ps,ResultSet rs) throws SQLException { + if (rs != null) { + rs.close(); + } + if (ps != null) { + ps.close(); + } + if (conn != null) { + conn.close(); + } + } +} +class Test{ + public static void main(String[] args) throws SQLException { +// String sql ="select * from student"; +// ResultSet rs = Homework.query(sql); +// while (rs.next()) { +// // 获取结果集里的内容的方法有两种:1,通过内容的位置,即(字段的个数的位置,从1开始) ,2 ,直接通过字段名获取 +// int id1 = rs.getInt("id"); +// String name1 = rs.getString("name"); +// String sex1 = rs.getString("sex"); +// System.out.println("编号:" + id1 + " " + name1 + " " + sex1); +// } + //用户输入ID和姓名 + Scanner sc = new Scanner(System.in); + System.out.println("请输入学号:"); + String userid=sc.nextLine(); + System.out.println("请输入姓名:"); + String username=sc.nextLine(); + System.out.println("请输入性别:"); + String usersex=sc.nextLine(); + + String sql = "select * from student where name=?"; + String[] keys= {userid,username}; + // 我们发现,要写通用的方法,?号是数量是不确定的,也意味着,形参的数量和位置不确定的 + // 此时就要用到可变参数 + ResultSet rs = Homework.query(sql,"王五"); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id+" "+name+" "+sex); + } + Homework.close(null,null,rs); + + // String sql2= "insert into student values (?,?,?)"; + String sql2= "update student set name=? where id=?"; + int i = Homework.update(sql2,"沁水","1"); + if (i>0){ + System.out.println("修改成功!"); + }else{ + System.out.println("修改失败!"); + } + + String sql3= "insert into student values (name=?,id=?,sex=?)"; + int n = Homework.update(sql3,"小懒","4","男"); + if (n>0){ + System.out.println("添加成功!"); + }else{ + System.out.println("添加失败!"); + } + + String sql4= "Delete student from id=?"; + int m = Homework.update(sql4,"4"); + if (n>0){ + System.out.println("删除成功!"); + }else{ + System.out.println("删除失败!"); + } +``` +