服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - JDBC实现学生管理系统

JDBC实现学生管理系统

2021-07-16 16:03feidao0 Java教程

这篇文章主要为大家详细介绍了JDBC实现学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了jdbc实现学生管理系统的具体代码,供大家参考,具体内容如下

1、学生类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package manage;
 
import java.util.date;
 
/**
 * @author fanxf
 * @since 2018/4/27 17:01
 */
public class student {
 
  private int id;
 
  private int age;
 
  private string sex;
 
  private string name;
 
  private date datecreated;
 
  public int getid() {
    return id;
  }
 
  public void setid(int id) {
    this.id = id;
  }
 
  public int getage() {
    return age;
  }
 
  public void setage(int age) {
    this.age = age;
  }
 
  public string getsex() {
    return sex;
  }
 
  public void setsex(string sex) {
    this.sex = sex;
  }
 
  public string getname() {
    return name;
  }
 
  public void setname(string name) {
    this.name = name;
  }
 
  public date getdatecreated() {
    return datecreated;
  }
 
  public void setdatecreated(date datecreated) {
    this.datecreated = datecreated;
  }
 
  public student() {
  }
 
  public student(int age, string sex, string name) {
    this.age = age;
    this.sex = sex;
    this.name = name;
  }
 
  public student(int id, int age, string sex, string name) {
    this.id = id;
    this.age = age;
    this.sex = sex;
    this.name = name;
  }
 
  @override
  public string tostring() {
    return "student{" +
        "id=" + id +
        ", age=" + age +
        ", sex='" + sex + '\'' +
        ", name='" + name + '\'' +
        ", datecreated=" + datecreated +
        '}';
  }
}

2、jdbc工具类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package manage;
 
import java.io.ioexception;
import java.sql.*;
import java.util.properties;
 
/**
 * @author fanxf
 * @since 2018/4/27 11:06
 */
//数据库的工具类
public class jdbcutils {
 
  private static string driver = "";
  private static string url = "";
  private static string user = "";
  private static string password = "";
 
  static {
    properties p = new properties();
    try {
      p.load(thread.currentthread().getcontextclassloader().getresourceasstream("db.properties"));
    } catch (ioexception e) {
      e.printstacktrace();
    }
    driver = p.getproperty("driver");
    url = p.getproperty("url");
    user = p.getproperty("user");
    password = p.getproperty("password");
    try {
      class.forname(driver);
    } catch (classnotfoundexception e) {
      e.printstacktrace();
    }
  }
 
  public static connection getconnection() {
    try {
      return drivermanager.getconnection(url, user, password);
    } catch (sqlexception e) {
      e.printstacktrace();
    }
    return null;
  }
  //释放的时候要从小到大释放
  //connection -> statement --> resultset
 
 
  public static void release(resultset rs, statement stmt, connection conn) {
    if (rs != null) {
      try {
        rs.close();
      } catch (sqlexception e) {
        e.printstacktrace();
      }
    }
    if (stmt != null) {
      try {
        stmt.close();
      } catch (sqlexception e) {
        e.printstacktrace();
      }
    }
 
    if (conn != null) {
      try {
        conn.close();
      } catch (sqlexception e) {
        e.printstacktrace();
      }
    }
  }
}

3、代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package manage;
 
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.scanner;
 
/**
 * @author fanxf
 * @since 2018/4/27 17:06
 */
public class managesystem {
 
  private static connection conn = null;
  private static preparedstatement ps = null;
  private static resultset rs = null;
 
  /**
   * 添加学生数据
   *
   * @param student
   * @return
   */
  public static int addstudent(student student) {
    conn = jdbcutils.getconnection();
    int result = 0;
    try {
      ps = conn.preparestatement("insert into student (age, sex, `name`, datecreated) values (?, ?, ?, now())");
      ps.setint(1, student.getage()); //设置第一个参数
      ps.setstring(2, student.getsex()); //设置第二个参数
      ps.setstring(3, student.getname()); //设置第三个参数
      result = ps.executeupdate();
    } catch (sqlexception e) {
      e.printstacktrace();
    } finally {
      jdbcutils.release(null, ps, conn); //关闭连接
    }
    return result;
  }
 
  public void add() {
    scanner scan = new scanner(system.in);
    system.out.println("请输入学生年龄");
    int age = scan.nextint();
    system.out.println("请输入学生性别");
    string sex = scan.next();
    system.out.println("请输入学生姓名");
    string name = scan.next();
    student s = new student(age, sex, name);
    int flag = addstudent(s);
    if (flag > 0) {
      system.out.println("添加成功");
    } else {
      system.out.println("添加失败");
    }
  }
 
  /**
   * 修改
   *
   * @param student
   * @return
   */
  public static int updatestudent(student student) {
    conn = jdbcutils.getconnection();
    int result = 0;
    try {
      ps = conn.preparestatement("update student set age = ?, sex = ?, `name` = ? where id = ?");
      ps.setint(1, student.getage()); //设置第一个参数
      ps.setstring(2, student.getsex()); //设置第二个参数
      ps.setstring(3, student.getname()); //设置第三个参数
      ps.setint(4, student.getid());
      result = ps.executeupdate();
    } catch (sqlexception e) {
      e.printstacktrace();
    } finally {
      jdbcutils.release(null, ps, conn); //关闭连接
    }
    return result;
  }
 
  public void update() {
    scanner scan = new scanner(system.in);
    system.out.println("请输入学生id");
    int id = scan.nextint();
    system.out.println("请输入学生年龄");
    int age = scan.nextint();
    system.out.println("请输入学生性别");
    string sex = scan.next();
    system.out.println("请输入学生姓名");
    string name = scan.next();
    student s = new student(id, age, sex, name);
    int flag = updatestudent(s);
    if (flag > 0) {
      system.out.println("更新成功");
    } else {
      system.out.println("更新失败");
    }
  }
 
  /**
   * 删除
   *
   * @param id
   * @return
   */
  public static int deletestudent(int id) {
    conn = jdbcutils.getconnection();
    int result = 0;
    try {
      ps = conn.preparestatement("delete from student where id = ?");
      ps.setint(1, id); //设置第一个参数
      result = ps.executeupdate();
    } catch (sqlexception e) {
      e.printstacktrace();
    } finally {
      jdbcutils.release(null, ps, conn); //关闭连接
    }
    return result;
  }
 
  public void delete() {
    scanner scan = new scanner(system.in);
    system.out.println("请输入学生id");
    int id = scan.nextint();
    int flag = deletestudent(id);
    if (flag > 0) {
      system.out.println("删除成功");
    } else {
      system.out.println("删除失败");
    }
  }
 
  public static void main(string[] args) {
    system.out.println("************ 欢迎进入学生管理系统 *************");
    managesystem ms = new managesystem();
    boolean b = true;
    while (b) {
      system.out.println("你想进行以下哪项操作");
      system.out.println("1、添加学生  2、更新学生数据  3、学生信息查询  4、删除学生 0、退出");
      scanner scan = new scanner(system.in);
      int i = scan.nextint();
      switch (i) {
        case 1:
          ms.add();
          break;
        case 2:
          ms.update();
          break;
        case 3:
          system.out.println();
          break;
        case 4:
          ms.delete();
          break;
        default:
          system.out.println("没有该操作选项,请重新来过!");
          main(args);
          break;
      }
    }
  }
}

4、properties数据库文件自己配置

数据库字段根据学生类建立!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/feidao0/article/details/81535758

延伸 · 阅读

精彩推荐