diff --git "a/19 \345\275\255\345\213\207\346\226\214/\345\260\201\350\243\205.md" "b/19 \345\275\255\345\213\207\346\226\214/\345\260\201\350\243\205.md" index ec9c257ba7f8d7ba123b85cfe2d161b3763461b5..38a84963605c9dff71efe5c8ee8b832921c983d2 100644 --- "a/19 \345\275\255\345\213\207\346\226\214/\345\260\201\350\243\205.md" +++ "b/19 \345\275\255\345\213\207\346\226\214/\345\260\201\350\243\205.md" @@ -1,20 +1,20 @@ -将id、name、sex封装起来,并放在集合里实现功能 - ```java -public class Test01{ +import javax.xml.namespace.QName; + +public class Student { private int id; private String name; private String sex; - public Test01() { - } - - public Test01(int id, String name, String sex) { + public Student(int id, String name, String sex) { this.id = id; this.name = name; this.sex = sex; } + public Student() { + } + public int getId() { return id; } @@ -41,164 +41,87 @@ public class Test01{ @Override public String toString() { - return "编号:" + id + - ",姓名:" + name + - ",性别:" + sex ; + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; } -public class DBUtil { - private static final String url = "jdbc:mysql:///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) { - System.out.println("驱动注册异常"); - e.printStackTrace(); - } - } - public static Connection getConn(){ - Connection conn = null; - try { - conn = DriverManager.getConnection(url, username, password); - } catch (SQLException e) { - System.out.println("连接异常,检查url等"); - e.printStackTrace(); - } - return conn; - - } - public static ResultSet select(String select, Object ...values){ - Connection conn = getConn(); - ResultSet rs = null; - try { - PreparedStatement stmt = conn.prepareStatement(select); - for (int i = 0; i < values.length; i++) { - stmt.setObject((i+1),values[i]); +} +import java.sql.*; +public class All { + private static final String url = "jdbc:mysql:///user?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String use = "root"; + private static final String password = "root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册异常"); } - rs = stmt.executeQuery(); - } catch (SQLException e) { - System.out.println("SQL执行异常"); - e.printStackTrace(); } - return rs; - } - public static int update(String sql,Object ...values){ - Connection conn = getConn(); - int num = 0; - try { - PreparedStatement stmt = conn.prepareStatement(sql); - for (int j = 0; j < values.length; j++) { - stmt.setObject((j+1),values[j]); + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, use, password); + } catch (SQLException e) { + System.out.println("获取失败"); } - num = stmt.executeUpdate(); - } catch (SQLException e) { - System.out.println("SQL语句异常"); - e.printStackTrace(); + return conn; } - return num; - } -} -public class Test02{ - static Scanner sc = new Scanner(System.in); - static Connection conn = DBUtil.getConn(); - public static void main(String[] args) { - out: - while (true) { - System.out.print("=========操作序号列表========\n" + - "1.查询信息\n" + - "2.添加功能\n" + - "3.修改功能\n" + - "4.删除功能\n" + - "5.退出系统\n" + - "输入序号进行对应操作:"); - int i = 0; + public static ResultSet getSql(String sql,String ...keys){ + ResultSet re = null; try { - i = sc.nextInt(); - } catch (Exception e) { - System.out.println("非法字符!请按要求输入!"); + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + re = pre.executeQuery(); + } catch (SQLException e) { + System.out.println("sql异常"); e.printStackTrace(); } - switch (i){ - case 1: - selectTest01(); - break; - case 2: - addTest01(); - break; - case 3: - updateTest01(); - break; - case 4: - deleteTest01(); - break; - case 5: - try { - conn.close(); - } catch (SQLException e) { - System.out.println("SQL执行错误"); - e.printStackTrace(); - } - System.out.println("即将退出,期待下次登录"); - break out; - default: - System.out.println("请按要求进行操作!!"); - } - } - } - - private static void deleteTest01() { - System.out.print("请输入要删除的学生学号:"); - int id = sc.nextInt(); - int i = DBUtil.update("delete from student where id = ?",id); - if (i>0){ - System.out.println("删除成功"); - }else { - System.out.println("删除失败"); - } - } - - private static void updateTest01() { - System.out.print("请输入要修改的学号:"); - int id = sc.nextInt(); - System.out.print("请输入要修改的姓名:"); - String name = sc.next(); - System.out.print("请输入要修改的性别:"); - String sex = sc.next(); - int i=DBUtil.update("update student set name =?,sex=? where id=? ",name,sex,id); - if (i>0){ - System.out.println("修改成功"); - }else { - System.out.println("修改失败"); + return re; } - } - - private static void addTest01() { - System.out.print("请输入学号:"); - int id = sc.nextInt(); - System.out.print("请输入姓名:"); - String name = sc.next(); - System.out.print("请输入性别男或女:"); - String sex = sc.next(); - int i = DBUtil.update("insert into student values (?,?,?)",id,name,sex); - if (i>0){ - System.out.println("添加成功!"); + public static int getUpdat(String sql,String ...keys){ + int num = 0; + try { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + num = pre.executeUpdate(); + } catch (SQLException e) { + System.out.println("sql异常"); + e.printStackTrace(); + } + return num; } - } +} +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; - private static void selectTest01() { - ResultSet resultSet = DBUtil.select("select * from student"); - ArrayList list = new ArrayList<>(); +public class Text { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + String sql = "select * from biao"; + ResultSet res = All.getSql(sql); try { - while (resultSet.next()){ - list.add(new Test01(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3))); + while (res.next()){ + int id = res.getInt(1); + String name = res.getString(2); + String sex = res.getString(3); + list.add(new Student(id,name,sex)); } } catch (SQLException e) { - throw new RuntimeException(e); + System.out.println("sql异常"); + e.printStackTrace(); } for (int i = 0; i < list.size(); i++) { - Test01 test01 = list.get(i); - System.out.println(test01); + System.out.println(list.get(i)); } } }