Main
package cs.itcast.jdbc; import cs.itcast.Student.student; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import static cs.itcast.jdbc.mysqlutil.*; public class mysql { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<student> list = new ArrayList<>(); while (true) { System.out.println("1.获取本地数据库连接"); System.out.println("2.添加学生进入数据库"); System.out.println("3.从数据库中删除学生"); System.out.println("4.查询数据库中的学生"); System.out.println("5.展示数据库成员信息"); int choice = sc.nextInt(); switch (choice) { case 1: getmysql();break; case 2: addmysql();break; case 3: delete();break; case 4: select();break; case 5: getmysql();break; } } } }
mysqlutils
package cs.itcast.jdbc; import cs.itcast.Student.student; import javafx.scene.paint.Stop; import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class mysqlutil { public static void getmysql() { List<student> list = new ArrayList<>(); Statement stmt = null; ResultSet rs = null; Connection conn = null; //使用list集合封装 try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zzt?serverTimezone=GMT&useSSL=false", "root", "root"); String sql = "select *from stu"; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); student s = new student(); while (rs.next()) { String name = rs.getString("name"); String gender = rs.getString("gender"); double weight = rs.getDouble("weight"); double height = rs.getDouble("height"); String hobby = rs.getString("hobby"); s = new student(); s.setName(name); s.setGender(gender); s.setWeight(weight); s.setHeight(height); s.setHobby(hobby); list.add(s); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } if (stmt != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } if (conn != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } //获取数据库连接 System.out.println(list); } public static void addmysql() { Scanner sc = new Scanner(System.in); PreparedStatement pstmt = null; int count = 0; Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zzt?serverTimezone=GMT&useSSL=false", "root", "root"); String sql = "insert into stu values(?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); System.out.println("请输入姓名"); String name = sc.next(); pstmt.setString(1, name); System.out.println("请输入性别"); String gender = sc.next(); pstmt.setString(2, gender); System.out.println("请输入体重"); double weight = sc.nextDouble(); pstmt.setDouble(3, weight); System.out.println("请输入身高"); double height = sc.nextDouble(); pstmt.setDouble(4, height); System.out.println("请输入爱好"); String hobby = sc.next(); pstmt.setString(5, hobby); count = pstmt.executeUpdate(); System.out.println(count); if (count > 0) System.out.println("添加成功"); else System.out.println("添加失败"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } public static void delete() { Scanner sc = new Scanner(System.in); PreparedStatement pstmt = null; int count = 0; Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zzt?serverTimezone=GMT&useSSL=false", "root", "root"); String sql = "delete from stu where name = ?"; pstmt = conn.prepareStatement(sql); System.out.println("请输入想要删除的学生姓名"); String name = sc.next(); pstmt.setString(1, name); count = pstmt.executeUpdate(); if (count > 0) System.out.println("删除成功"); else System.out.println("删除失败"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } } public static void select() { Scanner sc = new Scanner(System.in); PreparedStatement pstmt = null; ResultSet rs = null; int count = 0; Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zzt?serverTimezone=GMT&useSSL=false", "root", "root"); String sql = "select *from stu where name = ?"; pstmt = conn.prepareStatement(sql); System.out.println("请输入想要查询的学生姓名"); String name = sc.next(); pstmt.setString(1, name); rs = pstmt.executeQuery(); while (rs.next()) { String name01 = rs.getString("name"); String gender = rs.getString("gender"); double weight = rs.getDouble(3); double height = rs.getDouble(4); String hobby = rs.getString("hobby"); System.out.println(name01 + "-------" + gender + "-------" + weight + "-------" + height + "-------" + hobby); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } } }
student
package cs.itcast.Student; import java.util.Objects; public class student { private String name; private String gender; private double weight; private double height; private String hobby; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; student student = (student) o; return Double.compare(student.weight, weight) == 0 && Double.compare(student.height, height) == 0 && Objects.equals(name, student.name) && Objects.equals(gender, student.gender) && Objects.equals(hobby, student.hobby); } @Override public int hashCode() { return Objects.hash(name, gender, weight, height, hobby); } @Override public String toString() { return "student{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", weight=" + weight + ", height=" + height + ", hobby='" + hobby + '\'' + '}'+'\n'; } }