用Java实现增删改查

迈不过友情╰ 2022-01-29 02:07 392阅读 0赞

1.先写一个student类:

  1. public class student {
  2. private String name;
  3. private String sex;
  4. private String brithday;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public String getSex() {
  12. return sex;
  13. }
  14. public void setSex(String sex) {
  15. this.sex = sex;
  16. }
  17. public student() {
  18. }
  19. public student(String name, String sex, String brithday) {
  20. this.name = name;
  21. this.sex = sex;
  22. this.brithday = brithday;
  23. }
  24. public String getBrithday() {
  25. return brithday;
  26. }
  27. public void setBrithday(String brithday) {
  28. this.brithday = brithday;
  29. }
  30. @Override
  31. public String toString() {
  32. return "student{" +
  33. "name='" + name + '\'' +
  34. ", sex='" + sex + '\'' +
  35. ", brithday='" + brithday + '\'' +
  36. '}';
  37. }
  38. }

2.写一个可以获取文件上的资源并注册驱动的类(文件自己按照自己的数据库填)

  1. public class Deu_tils {
  2. private static String diverClass; //注册驱动的jar包地址值
  3. private static String url; //数据库的表格地址值
  4. private static String user; //Mysql的用户名
  5. private static String password; //Mysql的用户密码
  6. static {
  7. ResourceBundle rb=ResourceBundle.getBundle("dbinfo"); //获取文件的资源
  8. diverClass=rb.getString("driverClass");
  9. url=rb.getString("url");
  10. user=rb.getString("user");
  11. password=rb.getString("password");
  12. try {
  13. Class.forName(diverClass); //注册驱动
  14. } catch (ClassNotFoundException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. /**
  19. * 得到资源的方法
  20. * @return 资源
  21. * @throws SQLException
  22. */
  23. public static Connection getConnection() throws SQLException {
  24. return DriverManager.getConnection(url,user,password);
  25. }
  26. }
  • 3.实现增删改查

    1. public class DoLogin {
    2. private String sql;
    3. private PreparedStatement pstmt;
    4. private Connection conn;
    5. /**
    6. * 增加数据库里面一条学生对象信息的方法
    7. *
    8. * @param s
    9. * @return
    10. */
    11. public void Insert(student s) throws SQLException {
    12. Connection conn = Deu_tils.getConnection();
    13. int i = 0;
    14. sql = "INSERT INTO `text2`.`student`(`name`, `sex`, `brithday`) VALUES (?,?,?)";
    15. pstmt =conn.prepareStatement(sql);
    16. pstmt.setString(1, s.getName());
    17. pstmt.setString(2, s.getSex());
    18. pstmt.setString(3, s.getBrithday());
    19. i = pstmt.executeUpdate();
    20. pstmt.close();
    21. conn.close();
    22. }
    23. /**
    24. *
    25. * 根据数据库id改名字性别和生日的方法
    26. * @param s
    27. * @return
    28. * @throws SQLException
    29. */
    30. public void Update(int id,student s) throws SQLException {
    31. Connection conn = Deu_tils.getConnection();
    32. int i = 0;
    33. sql ="UPDATE `text2`.`student` SET `name` = '"+s.getName()+"', `sex` = '"+s.getSex()+"', `brithday` = '"+s.getBrithday()+"' WHERE `id` = "+id;
    34. pstmt =conn.prepareStatement(sql);
    35. i = pstmt.executeUpdate();
    36. pstmt.close();
    37. conn.close();
    38. }
    39. /**
    40. * 根据对象名删除数据库对象信息的方法
    41. * @param name
    42. * @return
    43. * @throws SQLException
    44. */
    45. public void Delete(String name) throws SQLException {
    46. Connection conn = Deu_tils.getConnection();
    47. int i = 0;
    48. sql ="DELETE FROM `text2`.`student` WHERE `name` = '"+name+"'";
    49. pstmt =conn.prepareStatement(sql);
    50. i = pstmt.executeUpdate();
    51. pstmt.close();
    52. conn.close();
    53. }
    54. /**
    55. * 查询表格的方法
    56. * @throws SQLException
    57. */
    58. public void Select() throws SQLException {
    59. Connection conn = Deu_tils.getConnection();
    60. sql = "SELECT * FROM student;";
    61. pstmt = conn.prepareStatement(sql);
    62. ResultSet rs = pstmt.executeQuery();
    63. student s = new student();
    64. while (rs.next()) {
    65. System.out.println(rs.getObject("id"));
    66. System.out.println(rs.getObject("name"));
    67. System.out.println(rs.getObject("sex"));
    68. System.out.println(rs.getObject("brithday"));
    69. System.out.println("---------------------------");
    70. }
    71. conn.close();
    72. pstmt.close();
    73. rs.close();
    74. }
    75. }

4.测试类:

  1. public class Text {
  2. public static void main(String[] args) throws SQLException {
  3. Scanner sc=new Scanner(System.in);
  4. System.out.println("请输入录入对象的名字:");
  5. String names=sc.next();
  6. System.out.println("请输入录入对象的性别:");
  7. String sexs=sc.next();
  8. System.out.println("请输入录入对象的生日:");
  9. String bris=sc.next();
  10. student s1=new student(names,sexs,bris);
  11. DoLogin dl1=new DoLogin();
  12. dl1.Insert(s1);
  13. dl1.Select();
  14. System.out.println("请输入要改对象的id:");
  15. int id=sc.nextInt();
  16. System.out.println("请输入要改的数据值名字:");
  17. String name=sc.next();
  18. System.out.println("请输入要改的数据值性别:");
  19. String sex=sc.next();
  20. System.out.println("请输入要改的数据值生日:");
  21. String bri=sc.next();
  22. student s2=new student(name,sex,bri);
  23. DoLogin dl2=new DoLogin();
  24. dl2.Update(id,s2);
  25. dl2.Select();
  26. System.out.println("请输入您要删除的对象姓名:");
  27. String namess=sc.next();
  28. DoLogin dl3=new DoLogin();
  29. dl3.Delete(namess);
  30. dl3.Select();
  31. }
  32. }





























id name sex brithday
1 张三 1999-02-13
2 李四 1999-08-16
       

最后:能力有限,如有不对之处,还请指教。

发表评论

表情:
评论列表 (有 0 条评论,392人围观)

还没有评论,来说两句吧...

相关阅读