diff --git "a/14 \345\256\213\345\256\217\346\211\254/20230520\345\205\263\344\272\216JDBC\347\232\204\350\277\233\351\230\266\344\275\234\344\270\232.md" "b/14 \345\256\213\345\256\217\346\211\254/20230520\345\205\263\344\272\216JDBC\347\232\204\350\277\233\351\230\266\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..4aff3ab75ff4269b63fcbb14fefa33efdc5223e5 --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20230520\345\205\263\344\272\216JDBC\347\232\204\350\277\233\351\230\266\344\275\234\344\270\232.md" @@ -0,0 +1,196 @@ +#### 建表 + +```mysql +create database student_db charset utf8; +use Student_db; +create table student (id int PRIMARY KEY AUTO_INCREMENT,`name` varchar(5),sex char(2)); +insert into student values + (1,"张三","男"), + (2,"李四","女"), + (3,"王五","男"); + +``` + +#### 工具类 + +```java +import java.sql.*; + +public class Tool { + /* + 使用新的执行SQL的对象,封装一个工具类, + 然后用这个工具类查询上次作业的数据库,实现将查询结果封装成学生对象, + 最后存入一个集合窗口,遍历之个集合的对象 + */ + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding"; + private static String username = "root"; + private static String password = "shy666nb"; + public static Connection co() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, password); + return conn; + } + public static ResultSet query(String sql,String ...keys) throws SQLException { + PreparedStatement pr = co().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pr.setString((i+1),keys[i]); + } + ResultSet re = pr.executeQuery(); + return re; + } + public static int update(String sql,String ...keys) throws SQLException { + Connection conn = co(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setString((i+1),keys[i]); + } + int i = pst.executeUpdate(); + return i; + } + public static void close(Connection conn,PreparedStatement pst,ResultSet rst) throws SQLException { + if (rst!=null){ + rst.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } + +} + +``` + +#### 测试类 + +```java +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Scanner; + +public class Test { + public static void main(String[] args) throws SQLException { + System.out.println("欢迎进入系统!" + + "\n1.查询全部信息" + + "\n2.查询个别信息" + + "\n3.添加信息" + + "\n4.修改信息" + + "\n5.删除信息" + + "\n6.退出系统"); + Scanner sc = new Scanner(System.in); + while (true) { + System.out.println("请输入要选择的功能选项:"); + String i = sc.nextLine(); + switch (i) { + case "1": + System.out.println("您选择了查询全部信息"); + allSelect(); + break; + case "2": + System.out.println("您选择了查询个别信息"); + select(); + break; + case "3": + System.out.println("您选择了添加信息"); + add(); + break; + case "4": + System.out.println("您选择了修改信息"); + update(); + break; + case "5": + System.out.println("您选择了删除信息"); + delete(); + break; + case "6": + System.out.println("您选择了退出系统"); + return; + default: + System.out.println("请输入正确的功能选项!"); + break; + } + } + } + private static void allSelect() throws SQLException { + String sql = "select * from student"; + ResultSet rs = Tool.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); + } + Tool.close(null,null,rs); + } + private static void select() throws SQLException { + Scanner sc = new Scanner(System.in); + System.out.print("请输入编号:"); + String userid=sc.nextLine(); + System.out.print("请输入姓名:"); + String username=sc.nextLine(); + String sql = "select * from student where id=? and name=?"; + String[] keys= {userid,username}; + ResultSet rs = Tool.query(sql,keys); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id+","+name+","+sex); + } + Tool.close(null,null,rs); + } + private static void add() throws SQLException { + Scanner sc = new Scanner(System.in); + System.out.print("请输入姓名:"); + String username=sc.nextLine(); + System.out.print("请输入性别:"); + String usersex=sc.nextLine(); + String sql2= "insert into student values (null,?,?)"; + int i = Tool.update(sql2,username,usersex); + if (i>0){ + System.out.println("添加成功"); + }else{ + System.out.println("添加失败了"); + } + } + private static void update() throws SQLException { + Scanner sc = new Scanner(System.in); + System.out.print("请输入修改后姓名:"); + String username=sc.nextLine(); + System.out.print("请输入修改前编号:"); + String userid=sc.nextLine(); + String sql2= "update student set name=? where id=?"; + int i = Tool.update(sql2,username,userid); + if (i>0){ + System.out.println("修改成功"); + }else{ + System.out.println("修改失败了"); + } + } + private static void delete() throws SQLException { + Scanner sc = new Scanner(System.in); + System.out.print("请输入编号:"); + String userid = sc.nextLine(); + System.out.print("请输入姓名:"); + String username = sc.nextLine(); + String sql2 = "delete from student where id=? and name=?"; + int i = Tool.update(sql2,userid,username); + if (i > 0) { + System.out.println("删除成功"); + } else { + System.out.println("删除失败了"); + } + } +} + + +``` +