diff --git "a/38 \351\273\204\346\255\243\347\204\225/20230517 JDBC\344\275\234\344\270\232.md" "b/38 \351\273\204\346\255\243\347\204\225/20230517 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..e9eff4fb20e5a8025b374a1abff5ff53c3175a65 --- /dev/null +++ "b/38 \351\273\204\346\255\243\347\204\225/20230517 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,207 @@ +### JDBC作业: + +1. MySQL中创建一个数据库student_db + +2. 库中创建student表 + +3. 表中数据如下 + +4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + +5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + ```java + import java.sql.*; + + 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); + //编写SQL语句 + String SQl = "select * from student"; + //获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //执行SQL语句 + ResultSet rs = st.executeQuery(SQl);//专门用来执行select语句的返回值 + //处理返回结果 + //处理结果要遍历 + while (rs.next()){ + int stuid = rs.getInt("stuid"); + String stuname = rs.getString("stuname"); + String stusex = rs.getString("stusex"); + System.out.println(stuid + stuname + stusex); + } + //释放资源 + rs.close(); + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + } + + ``` + + + + 2. 添加功能 + + ```java + import java.sql.*; + + public 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); + //编写SQL语句 + String SQl = "insert into student values(4,'孙芳','女')"; + //获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //执行SQL语句 + int rs = st.executeUpdate(SQl);//专门用来执行除了select语句的返回值 + //处理返回结果 + //处理结果要遍历 + if (rs>0){ + System.out.println("增加了"+rs+"行"); + }else { + System.out.println("增加失败!"); + System.out.println("增加了"+rs+"行"); + } + //释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + } + + ``` + + + + 3. 修改功能 + + ```java + import java.sql.Connection; + import java.sql.DriverManager; + import java.sql.SQLException; + import java.sql.Statement; + + public class Alter { + 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); + //编写SQL语句 + String SQl = "update student set stusex ='男' where stuid=4"; + //获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //执行SQL语句 + int rs = st.executeUpdate(SQl);//专门用来执行除了select语句的返回值 + //处理返回结果 + //处理结果要遍历 + if (rs>0){ + System.out.println("修改了"+rs+"行"); + }else { + System.out.println("修改失败!"); + System.out.println("修改了"+rs+"行"); + } + //释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + } + + ``` + + + + 4. 删除功能 + + ```java + 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 { + //注册驱动 + 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); + //编写SQL语句 + String SQl = "delete from student where stuid=4"; + //获取执行SQL语句的对象 + Statement st = conn.createStatement(); + //执行SQL语句 + int rs = st.executeUpdate(SQl);//专门用来执行select语句的返回值 + //处理返回结果 + //处理结果要遍历 + if (rs>0){ + System.out.println("删除了"+rs+"行"); + }else { + System.out.println("删除失败!"); + System.out.println("删除了"+rs+"行"); + } + //释放资源 + st.close(); + conn.close(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + } + + ``` + + + +6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file diff --git "a/38 \351\273\204\346\255\243\347\204\225/20230520 \345\260\201\350\243\205jdbc\345\267\245\345\205\267\347\261\273.md" "b/38 \351\273\204\346\255\243\347\204\225/20230520 \345\260\201\350\243\205jdbc\345\267\245\345\205\267\347\261\273.md" new file mode 100644 index 0000000000000000000000000000000000000000..6d027bf2db4c609c6a23271a7d852343c80744dd --- /dev/null +++ "b/38 \351\273\204\346\255\243\347\204\225/20230520 \345\260\201\350\243\205jdbc\345\267\245\345\205\267\347\261\273.md" @@ -0,0 +1,119 @@ +```mysql +create database student_db character set utf8; +use student_db; +drop database student_db; +create table student( + stuid int, + stuname varchar(10), + stusex varchar(2) +); +insert into student values(1,"张三","男"); +insert into student values(2,"李四","女"); +insert into student values(3,"王五","男"); +``` + +````java +//JDBC工具类 +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", ""); + 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("stuid"); + String name = re.getString("stuname"); + String sex = re.getString("stusex"); + 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()); + } + } +} +```` +