java+JDBC小项目《学生管理系统》源码带注解

发布时间:2018-06-27 21:06:42   来源:文档文库   
字号:

Java+javabean+JDBC学生管理系统

1、项目结构

本项目是使用javabeanjdbc做的,这个包是实体包

这个是菜单包,源码不会再发这些,自己写就好了。

2、项目运行结果

3、源码

废话不多说,直接上源码:

这两个是关键源码,是负责登录和学生信息操作的逻辑类:

public class AdminDaoImpl extends DBHelper implements AdminDao {

Admin admin = null;

/**

* 登录

*/

@SuppressWarnings("resource")

@Override

public Admin login(String name) {

String sql = "select * from admin where username=?";

Object[] param = {name};

Object obj = this.excute(sql, param);

ResultSet rs = (ResultSet) obj;

try {

while (rs.next()) {

admin = new Admin();

String username = rs.getString("username");

String password = rs.getString("password");

admin.setUsername(username);

admin.setPassword(password);

}

} catch (SQLException e) {

System.out.println("未找到此name");

}

return admin;

}

}

public class StudentDaoImpl extends DBHelper implements StudentDao {

Student stu = null;

List list = null;

@SuppressWarnings("resource")

@Override

public Student getInfoByid(int id) {

String sql = "select * from student where id=?";

Object[] param = { id };

Object obj = this.excute(sql, param);

ResultSet rs = (ResultSet) obj;

stu = new Student();

try {

while (rs.next()) {

stu.setId(rs.getInt("id"));

stu.setName(rs.getString("name"));

stu.setAge(rs.getInt("age"));

stu.setGender(rs.getString("gender"));

stu.setGrade(rs.getString("grade"));

stu.setPhone(rs.getLong("phone"));

stu.setEmail(rs.getString("email"));

stu.setAddress(rs.getString("address"));

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

this.closeAll();

}

return stu;

}

@SuppressWarnings("resource")

@Override

public List getAllStu() {

String sql = "select * from student";

Object obj = this.excute(sql, null);

ResultSet rs = (ResultSet) obj;

list = new List();

try {

while (rs.next()) {

stu = new Student();

stu.setId(rs.getInt("id"));

stu.setName(rs.getString("name"));

stu.setAge(rs.getInt("age"));

stu.setGender(rs.getString("gender"));

stu.setGrade(rs.getString("grade"));

stu.setPhone(rs.getLong("phone"));

stu.setEmail(rs.getString("email"));

stu.setAddress(rs.getString("address"));

list.add(stu);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

this.closeAll();

}

return list;

}

@SuppressWarnings("resource")

@Override

public String getNameById(int id) {

String name = null;

String sql = "select name from student where id=?";

Object[] param = { id };

Object obj = this.excute(sql, param);

ResultSet rs = (ResultSet) obj;

try {

while (rs.next()) {

name = rs.getString("name");

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

this.closeAll();

}

return name;

}

@SuppressWarnings("resource")

@Override

public int getidByIntput(int id) {

int num = 0;

String sql = "select id from student where id=?";

Object[] param = { id };

Object obj = this.excute(sql, param);

ResultSet rs = (ResultSet) obj;

try {

while (rs.next()) {

num = rs.getInt("id");

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

this.closeAll();

}

return num;

}

@Override

public boolean addStudent(Object[] param) {

boolean b = false;

String sql = "insert into student values(?,?,?,?,?,?,?,?)";

Object obj = this.excute(sql, param);

b = (boolean) obj;

return b;

}

@Override

public boolean removeStuById(int id) {

boolean b = false;

String sql = "delete from student where id=?";

Object[] param = { id };

Object obj = this.excute(sql, param);

b = (boolean) obj;

return b;

}

@Override

public boolean modifyAllStuById(Student stu) {

boolean b = false;

String sql = "update student set age = ?,grade=?,address=?,phone=?,email=? where id = ?";

Object[] param = { stu.getAge(), stu.getGrade(), stu.getAddress(),

stu.getPhone(), stu.getEmail(), stu.getId() };

Object obj = this.excute(sql, param);

b = (boolean) obj;

return b;

}

@Override

public boolean modifyPartStuById(Student stu, String attr) {

boolean b = false;

if (attr.equals("age")) {

String sql = "update student set age =? where id=?";

Object[] param = { stu.getAge(), stu.getId() };

Object obj = this.excute(sql, param);

b = (boolean) obj;

} else if (attr.equals("grade")) {

String sql = "update student set grade =? where id=?";

Object[] param = { stu.getGrade(), stu.getId() };

Object obj = this.excute(sql, param);

b = (boolean) obj;

} else if (attr.equals("address")) {

String sql = "update student set address =? where id=?";

Object[] param = { stu.getAddress(), stu.getId() };

Object obj = this.excute(sql, param);

b = (boolean) obj;

} else if (attr.equals("phone")) {

String sql = "update student set phone =? where id=?";

Object[] param = { stu.getPhone(), stu.getId() };

Object obj = this.excute(sql, param);

b = (boolean) obj;

} else if (attr.equals("email")) {

String sql = "update student set email =? where id=?";

Object[] param = { stu.getEmail(), stu.getId() };

Object obj = this.excute(sql, param);

b = (boolean) obj;

}

return b;

}

}

好吧,到此为止,逻辑算是完成了,接下来就是工具包,也就是JDBC通式

public class DBHelper {

private static final String url = "jdbc:mysql://localhost:3306/sms?characterEncoding=utf-8";

private static final String Driver = "com.mysql.jdbc.Driver";

private static final String name = "root";

private static final String pwd = "sa123456";

private Connection conn = null;

private PreparedStatement pstmt = null;

private ResultSet rs = null;

/**

* 创建数据库连接

*

* @return

*/

public Connection Getconn() {

try {

Class.forName(Driver);

conn = DriverManager.getConnection(url, name, pwd);

} catch (ClassNotFoundException e) {

System.out.println("注册驱动失败");

} catch (SQLException e) {

System.out.println("驱动包路径错误");

}

return conn;

}

public Object excute(String sql, Object[] param) {

int a = 0;

Object o = null;

this.Getconn();

try {

pstmt = conn.prepareStatement(sql);

if (param != null) {

for (int i = 0; i < param.length; i++) {

pstmt.setObject(i + 1, param[i]);

}

}

boolean b = pstmt.execute();

if (b) {

rs = pstmt.getResultSet();

o = rs;

} else {

a = pstmt.getUpdateCount();

if (a > 0) {

o = true;

} else {

o = false;

}

closeAll();

}

} catch (SQLException e) {

e.printStackTrace();

}

return o;

}

/**

* 关闭数据库

*/

public void closeAll() {

try {

if (rs != null) {

rs.close();

}

if (pstmt != null) {

pstmt.close();

}

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

System.out.println("错误关闭");

}

}

}

至于这个类,是一些控制台输入信息判断,当然可以贴出来供大家参考~

/**

* 匹配信息

*

* @author Administrator

*

*/

public class Matches {

Scanner input = new Scanner(System.in);

static String id = null;

static String gender = null;

static String age = null;

static String grade = null;

static String phone = null;

static String email = null;

/**

* 匹配id

*

* @return

*/

public String matchesId() {

id = input.next();

if (Pattern.matches("^[0-9]{1,}$", id)) {

} else {

System.out.println("输入错误,只能输入数字:");

this.matchesId();

}

return id;

}

/**

* 匹配性别

*

* @return

*/

public String matchesGender() {

gender = input.next();

if (!(gender.equals("男") || gender.equals("女"))) {

System.out.println("性别只能是男或者女:");

this.matchesGender();

}

return gender;

}

/**

* 匹配年龄

*

* @return

*/

public int matchesAge() {

age = input.next();

if (!Pattern.matches("^[0-9]{1,}$", age)) {

System.out.println("以上输入不合法,只能输入1-120之内的数字:");

this.matchesAge();

} else if (Integer.valueOf(age) < 1 || Integer.valueOf(age) > 120) {

System.out.println("以上输入不合法,只能输入1-120之内的数字:");

this.matchesAge();

}

return Integer.parseInt(age);

}

/**

* 匹配年级

*

* @return

*/

public String matchesGrade() {

grade = input.next();

if (!(grade.equals("初级") || grade.equals("中级") || grade.equals("高级"))) {

System.out.println("无此年级设置,年级只能输入初级、中级或高级,请重新输入:");

this.matchesGrade();

}

return grade;

}

/**

* 匹配手机号

*

* @return

*/

public long matchesPhone() {

phone = input.next();

if (!Pattern.matches("^[0-9]{11}$", phone)) {

System.out.println("输入有误,电话号码只能是11位数字,请重新输入:");

this.matchesPhone();

}

return Long.parseLong(phone);

}

/**

* 匹配email

*

* @return

*/

public String matchesEmail() {

email = input.next();

if (!Pattern.matches("^[0-9a-zA-Z]+@[0-9a-zA-Z]+.[0-9a-zA-Z]+$", email)) {

System.out.println("邮箱格式有误,请输入正确的电子邮箱(包含@和.com)");

this.matchesEmail();

}

return email;

}

}

好了,别的我就不说了,怎么调用,我更就不用说了吧?

本文为原创作品,转载需注明出处

本文来源:https://www.2haoxitong.net/k/doc/082f4aa6aa00b52acec7ca47.html

《java+JDBC小项目《学生管理系统》源码带注解.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式