java学生管理系统源码(3)

2019-03-03 19:56

public static void closeConn(Connection conn) { if (conn != null) { try {

conn.close();

} catch (SQLException e) { e.printStackTrace(); } } }

public static PreparedStatement getPstmt(Connection conn, String sql)

throws SQLException {

PreparedStatement pstmt = conn.prepareStatement(sql); return pstmt; }

public static void closePstmt(PreparedStatement pstmt) { if (pstmt != null) { try {

pstmt.close();

} catch (SQLException e) { e.printStackTrace(); } } }

public static ResultSet executeQuery(PreparedStatement pstmt) throws SQLException {

ResultSet rs = pstmt.executeQuery(); return rs; }

public static void closeRs(ResultSet rs) { if (rs != null) { try {

rs.close();

} catch (SQLException e) { e.printStackTrace(); } } } }

com.ccniit.student.manage.StudentManage类

说明:此类是逻辑处理层里面的学生管理类,有添加学生、删除学生、修改学生信息、检查学生是否登录成功、查找学生等方法,显示层可以直接调用这些方法来实现需要的功能。其中查找方法是有方法的组合。

package com.ccniit.student.manage;

import java.sql.Connection; import java.sql.Date;

import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;

import com.ccniit.student.Student;

import com.ccniit.student.util.DBConnection;

public class StudentManage { // 添加学生

public static boolean add(Student s) { Connection conn = null;

PreparedStatement pstmt = null; boolean flag = false; try {

conn = DBConnection.getConn();

String sql = \password, sex, birth, phone, qq, homeAddr, entranceTime, photo, classes) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\;

pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, s.getStudentID()); pstmt.setString(2, s.getStudentName()); pstmt.setString(3, s.getPassword()); pstmt.setString(4, s.getSex()); pstmt.setDate(5, s.getBirth()); pstmt.setString(6, s.getPhone()); pstmt.setString(7, s.getQq());

pstmt.setString(8, s.getHomeAddr()); pstmt.setDate(9, s.getEntranceTime()); pstmt.setString(10, s.getPhoto()); pstmt.setString(11, s.getClasses()); if (pstmt.executeUpdate() > 0) {

flag = true; }

} catch (SQLException e) { e.printStackTrace(); } finally {

DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }

return flag; }

/*

* public static void main(String[] args) { // Calendar c = * Calendar.getInstance(); // c.set(2011, 7, 28); //

* System.out.println(add(new Student(\骆巍\男\new //

* Date(c // .getTimeInMillis()), \东软学院\

* System.out.println(add(new Student(\骆巍\\男\

* Date.valueOf(\都江堰成都东软学院\

* // System.out.println(delete(\ * Calendar.getInstance(); // c.set(2011, 7, 28); // * System.out.println(c.getTime()); } */

// 删除学生

public static boolean delete(String id) { Connection conn = null;

PreparedStatement pstmt = null; boolean flag = false; try {

conn = DBConnection.getConn();

String sql = \; pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, id);

if (pstmt.executeUpdate() > 0) { flag = true; }

} catch (SQLException e) { e.printStackTrace(); } finally {

DBConnection.closePstmt(pstmt);

DBConnection.closeConn(conn); }

return flag; }

// 修改学生信息

public static boolean update(Student s) { Connection conn = null;

PreparedStatement pstmt = null; boolean flag = false; try {

conn = DBConnection.getConn(); String sql = \student set studentName = ?, password = ?, sex = ?, birth = ?, phone = ?, qq = ?, homeAddr = ?, entranceTime = ?, photo = ?, classes = ? where studentID = ?\;

pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, s.getStudentName()); pstmt.setString(2, s.getPassword()); pstmt.setString(3, s.getSex()); pstmt.setDate(4, s.getBirth()); pstmt.setString(5, s.getPhone()); pstmt.setString(6, s.getQq());

pstmt.setString(7, s.getHomeAddr()); pstmt.setDate(8, s.getEntranceTime()); pstmt.setString(9, s.getPhoto()); pstmt.setString(10, s.getClasses()); pstmt.setString(11, s.getStudentID()); if (pstmt.executeUpdate() > 0) { flag = true; }

} catch (SQLException e) { e.printStackTrace(); } finally {

DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }

return flag; }

// 检查学生登录是否成功

public static Student check(String studentID, String password) throws StudentNotFoundException, PasswordNotCorrectException { Student student = null;

Connection conn = null;

PreparedStatement pstmt = null; ResultSet rs = null; try {

conn = DBConnection.getConn();

String sql = \; pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, studentID);

rs = DBConnection.executeQuery(pstmt); if (!rs.next()) {

throw new StudentNotFoundException(); } else if

(!rs.getString(\).trim().equals(password)) {

// System.out.println(rs.getString(\ // System.out.println(password);

throw new PasswordNotCorrectException(); } else {

student = new Student(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getDate(5), rs.getString(6), rs.getString(7), rs.getString(8),

rs.getDate(9), rs.getString(10), rs.getString(11)); }

} catch (SQLException e) { e.printStackTrace(); } finally {

DBConnection.closeRs(rs);

DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }

return student; }

// 按照studentID来查找学生,返回查找到的学生信息,没有找到则抛出异常 public static Student findByID(String studentID) throws StudentNotFoundException {

String condition = \ + studentID; List students = new ArrayList(); if (find(students, condition) < 1) {

throw new StudentNotFoundException(); }

return students.get(0); }


java学生管理系统源码(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:《陈涉世家》2009-2011年中考试题集锦

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: