diff --git "a/23 \351\273\204\346\265\251\344\270\234/20230520 jdbc\345\267\245\345\205\267\347\261\273\344\275\234\344\270\232.md" "b/23 \351\273\204\346\265\251\344\270\234/20230520 jdbc\345\267\245\345\205\267\347\261\273\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..b9e68f6dc291f95ca63433fa9f4faaf5f7896e34 --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20230520 jdbc\345\267\245\345\205\267\347\261\273\344\275\234\344\270\232.md" @@ -0,0 +1,121 @@ +使用新的执行SQL的对象,封装一个工具类,然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象,最后存入一个集合窗口,遍历之个集合的对象 + +```java +//工具类 +import java.sql.*; +import java.util.ArrayList; +import java.util.Arrays; + +public class tool{ + private static final String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String name = "root"; + static { + try{ + Class.forName("com.mysql.jdbc.Driver"); + } + catch (ClassNotFoundException e){ + e.printStackTrace(); + } + } + public static Connection getCon() throws SQLException + { + Connection con = DriverManager.getConnection(url,name,name); + return con; + } + public static ArrayList Select(String sql,String ...key) throws SQLException{ + ArrayList info = new ArrayList<>(); + Connection con = getCon(); + PreparedStatement state = con.prepareStatement(sql); + for (int i = 0; i < key.length; i++) { + state.setString(i,key[i]); + } + ResultSet res = state.executeQuery(); + while (res.next()){ + int num = res.getInt("num"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(num,name,sex)); + } + return info; + } + public static ArrayList Select(String sql) throws SQLException{ + ArrayList info = new ArrayList<>(); + Connection con = getCon(); + PreparedStatement state = con.prepareStatement(sql); + ResultSet res = state.executeQuery(); + while (res.next()){ + int num = res.getInt("num"); + String name = res.getString("name"); + String sex = res.getString("sex"); + info.add(new Student(num,name,sex)); + } + return info; + } + + +} +//学生类 + +public class Student { + public Student(){} + public Student(int num,String name,String sex){ + this.setName(name); + this.setSex(sex); + this.setNum(num); + } + private int num; + private String name; + private String sex; + public String toString (){ + return "num = "+ getNum() +"\t name = "+ getName() +"\t sex = "+ getSex(); + } + + public int getNum() { + return num; + } + + public void setNum(int num) { + this.num = num; + } + + 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; + } +} +//实现类 +import java.sql.*; +import java.util.ArrayList; +import java.util.Scanner; + +import static java.lang.Class.forName; + +public class p1 { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + String sql = "select * from student"; + try { + list = tool.Select(sql); + } + catch (SQLException e){ + e.printStackTrace(); + } + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } + } + + +``` \ No newline at end of file diff --git "a/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" new file mode 100644 index 0000000000000000000000000000000000000000..c417bd420a94a7fad2931f8feedcfde5742ee4ac --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20235017 java\347\232\204jdbc\345\237\272\347\241\200.md" @@ -0,0 +1,184 @@ +6. 1. JDBC作业: + + 1. MySQL中创建一个数据库student_db + + 2. 库中创建student表 + + 3. 表中数据如下 + + 4. | 编号 | 姓名 | 性别 | + | ---- | ---- | ---- | + | 1 | 张三 | 男 | + | 2 | 李四 | 女 | + | 3 | 王五 | 男 | + + ```java + create database student_db charset utf8; + use student_db; + create table student ( + num int, + name varchar(20), + sex char + ); + insert into student values + (1,'张三','男'), + (2,'李四','女'), + (3,'王五','男'); + ``` + + 5. 编写java 4个类,分别实现以下功能 + + 1. 查询功能,查询student中所有数据 + + 2. 添加功能 + + 3. 修改功能 + + 4. 删除功能 + + 5. ```java + import java.sql.*; + import java.util.Scanner; + + import static java.lang.Class.forName; + + public class p1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + // 创建3个类 + Connection con = null; + Statement state = null ; + int resultSet = 0; + String url="jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String name = "root"; + String password = "root"; + try { + //添加驱动 + forName("com.mysql.jdbc.Driver"); + // 创建连接 + con = DriverManager.getConnection(url,name, password); + // 创建 Statement 对象 + state = con.createStatement(); + // 写执行语句 + choice:while (true){ + int choice = sc.nextInt(); + switch (choice){ + case 1: + System.out.println("查询"); + select(state); + break; + case 2: + System.out.println("删除"); + System.out.println("请输入要删除的id"); + delete(state,sc.nextInt()); + break; + case 3: + System.out.println("修改"); + System.out.println("请输入要修改的id"); + update(state,sc); + break ; + case 4: + System.out.println("添加"); + Add(state,sc); + break; + default: + break choice; + + } + } + } + catch(SQLException e){ + e.printStackTrace(); + } + catch(ClassNotFoundException e){ + e.printStackTrace(); + } + try { + if (state != null) { + state.close(); + } + if (con !=null) { + con.close(); + } + } + catch(SQLException e){ + e.printStackTrace(); + } + } + public static void select( Statement state) throws SQLException{ + ResultSet res = state.executeQuery("select * from student"); + while (res.next()){ + System.out.println(res.getInt("num") + "\t" + res.getString("name")+"\t"+res.getString("sex")); + } + res.close(); + } + public static void delete(Statement state,int num) throws SQLException{ + int res = state.executeUpdate("Delete from student where num ="+num); + System.out.println("删除了"+res+"行"); + } + public static void update(Statement state,Scanner sc) throws SQLException{ + int res; + int num = 0; + int all = 0; + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + all++; + if (result.getInt("num") != id){ + num++; + } + if (num == all){ + System.out.println("查无此人"); + result.close(); + return; + } + } + result.close(); + System.out.println("请输入要修改的姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + res = state.executeUpdate("update student " + + "set name = "+ "\'"+name+"\', " + + "sex ="+"\'"+sex+"\' where num ="+id); + + if (res >0){ + System.out.println("修改成功"+res+"行已被修改"); + } + else { + System.out.println("修改失败"); + } + } + public static void Add + (Statement state,Scanner sc) throws SQLException{ + System.out.println("请输入id"); + int id = sc.nextInt(); + ResultSet result= state.executeQuery("select * from student"); + while (result.next()){ + if (result.getInt("num") == id){ + System.out.println("该id已存在请换一个"); + return; + } + } + System.out.println("请输入姓名"); + String name = sc.next(); + System.out.println("请输入性别"); + char sex = sc.next().charAt(0); + int res = state.executeUpdate(String.format("insert into student values ("+ id+","+ "\'"+name+"\'"+","+"\'"+ sex+"\'"+")")); + if (res >0){ + System.out.println("添加成功"); + } + else { + System.out.println("添加失败"); + } + } + } + + ``` + + + + 6. 扩展题【预习题】 + + 1. 能否实现一个类中,用四个方法来实现上面4个类的功能 + 2. 能否实现将查询的结果,封装成java对象 \ No newline at end of file