java学生管理系统jdbc_Java+JDBC学生信息管理系统

base包下:

package base;

import java.sql.ResultSet;

import java.sql.SQLException;

import dao.AdminDAO;

import dao.StudentDAO;

import util.DBUtil;

import temp.DAO;

public abstract class BaseDAO {

protected final DBUtil db = DBUtil.getDBUtil();

protected ResultSet rs;

private static BaseDAO baseDAO;

public BaseDAO() {

init();

}

private void init() {

// buildAbilityDAO();

}

// protected abstract void buildAbilityDAO();

public static synchronized BaseDAO getAbilityDAO(DAO dao) {

switch (dao) {

case AdminDAO:

if (baseDAO == null || baseDAO.getClass() != AdminDAO.class) {

baseDAO = AdminDAO.getInstance();

}

break;

case StudentDAO:

if (baseDAO == null || baseDAO.getClass() != StudentDAO.class) {

baseDAO = StudentDAO.getInstance();

}

break;

default:

break;

}

return baseDAO;

}

protected void destroy() {

try {

if (rs != null) {

rs.close();

}

} catch (SQLException se) {

se.printStackTrace();

} finally {

db.close();

}

}

}

dao包下:

package dao;

import java.sql.SQLException;

import base.BaseDAO;

/**

* 管理员增删查改

* @author Administrator

*

*/

public class AdminDAO extends BaseDAO{

private static AdminDAO ad = null;

public static synchronized AdminDAO getInstance() {

if (ad == null) {

ad = new AdminDAO();

}

return ad;

}

public boolean queryForLogin(String username, String password) {

boolean result = false;

if (username.length() == 0 || password.length() == 0) {

return result;

}

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

String[] param = { username, password };

rs = db.executeQuery(sql, param);

try {

if (rs.next()) {

result = true;

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

destroy();

}

return result;

}

}

package dao;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import model.Student;

import base.BaseDAO;

public class StudentDAO extends BaseDAO{

private final int fieldNum = 9;

private final int showNum = 15;

private static StudentDAO sd = null;

public static synchronized StudentDAO getInstance() {

if (sd == null) {

sd = new StudentDAO();

}

return sd;

}

// update

public boolean update(Student stu) {

boolean result = false;

if (stu == null) {

return result;

}

try {

// check

if (queryBySno(stu.getSno()) == 0) {

return result;

}

// update

String sql = "update student set sex=?,department=?,email=?,tel=?,hometown=?,mark=? where name=? and sno=?";

String[] param = { stu.getSex(), stu.getDepartment(), stu.getEmail(), stu.getTel(), stu.getHomeTown(),

stu.getMark(), stu.getName(), stu.getSno() };

int rowCount = db.executeUpdate(sql, param);

if (rowCount == 1) {

result = true;

}

} catch (SQLException se) {

se.printStackTrace();

} finally {

destroy();

}

return result;

}

// delete

public boolean delete(Student stu) {

boolean result = false;

if (stu == null) {

return result;

}

String sql = "delete from student where name=? and sno=?";

String[] param = { stu.getName(), stu.getSno() };

int rowCount = db.executeUpdate(sql, param);

if (rowCount == 1) {

result = true;

}

destroy();

return result;

}

// add

public boolean add(Student stu) {

boolean result = false;

if (stu == null) {

return result;

}

try {

// check

if (queryBySno(stu.getSno()) == 1) {

return result;

}

// insert

String sql = "insert into student(name,sno,sex,department,hometown,mark,email,tel) values(?,?,?,?,?,?,?,?)";

String[] param = { stu.getName(), stu.getSno(), stu.getSex(), stu.getDepartment(), stu.getHomeTown(),

stu.getMark(), stu.getEmail(), stu.getTel() };

if (db.executeUpdate(sql, param) == 1) {

result = true;

}

} catch (SQLException se) {

se.printStackTrace();

} finally {

destroy();

}

return result;

}

// query by name

public String[][] queryByName(String name) {

String[][] result = null;

if (name.length() < 0) {

return result;

}

List stus = new ArrayList();

int i = 0;

String sql = "select * from student where name like ?";

String[] param = { "%" + name + "%" };

rs = db.executeQuery(sql, param);

try {

while (rs.next()) {

buildList(rs, stus, i);

i++;

}

if (stus.size() > 0) {

result = new String[stus.size()][fieldNum];

for (int j = 0; j < stus.size(); j++) {

buildResult(result, stus, j);

}

}

} catch (SQLException se) {

se.printStackTrace();

} finally {

destroy();

}

return result;

}

// query

public String[][] list(int pageNum) {

String[][] result = null;

if (pageNum < 1) {

return result;

}

List stus = new ArrayList();

int i = 0;

int beginNum = (pageNum - 1) * showNum;

String sql = "select * from student limit ?,?";

Integer[] param = { beginNum, showNum };

rs = db.executeQuery(sql, param);

try {

while (rs.next()) {

buildList(rs, stus, i);

i++;

}

if (stus.size() > 0) {

result = new String[stus.size()][fieldNum];

for (int j = 0; j < stus.size(); j++) {

buildResult(result, stus, j);

}

}

} catch (SQLException se) {

se.printStackTrace();

} finally {

destroy();

}

return result;

}

// 将rs记录添加到list中

private void buildList(ResultSet rs, List list, int i) throws SQLException {

Student stu = new Student();

stu.setId(i + 1);

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

stu.setDepartment(rs.getString("department"));

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

stu.setHomeTown(rs.getString("hometown"));

stu.setMark(rs.getString("mark"));

stu.setSex(rs.getString("sex"));

stu.setSno(rs.getString("sno"));

stu.setTel(rs.getString("tel"));

list.add(stu);

}

// 将list中记录添加到二维数组中

private void buildResult(String[][] result, List stus, int j) {

Student stu = stus.get(j);

result[j][0] = String.valueOf(stu.getId());

result[j][1] = stu.getName();

result[j][2] = stu.getSno();

result[j][3] = stu.getSex();

result[j][4] = stu.getDepartment();

result[j][5] = stu.getHomeTown();

result[j][6] = stu.getMark();

result[j][7] = stu.getEmail();

result[j][8] = stu.getTel();

}

// query by sno

private int queryBySno(String sno) throws SQLException {

int result = 0;

if ("".equals(sno) || sno == null) {

return result;

}

String checkSql = "select * from student where sno=?";

String[] checkParam = { sno };

rs = db.executeQuery(checkSql, checkParam);

if (rs.next()) {

result = 1;

}

return result;

}

}

model包下:

package model;

public class Admin {

private int id;

private String name;

private String username;

private String password;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

package model;

public class Student {

private int id;

private String sno;//学号

private String name;

private String sex;

private String department;//院系

private String homeTown;//籍贯

private String mark;

private String email;

private String tel;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getSno() {

return sno;

}

public void setSno(String sno) {

this.sno = sno;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getDepartment() {

return department;

}

public void setDepartment(String department) {

this.department = department;

}

public String getHomeTown() {

return homeTown;

}

public void setHomeTown(String homeTown) {

this.homeTown = homeTown;

}

public String getMark() {

return mark;

}

public void setMark(String mark) {

this.mark = mark;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getTel() {

return tel;

}

public void setTel(String tel) {

this.tel = tel;

}

}

run包下:

package run;

import view.LoginView;

public class Main {

public static void main(String[] args) {

// TODO 自动生成的方法存根

new LoginView();

}

}

temp包下:

package temp;

/**

* 常量

* @author Administrator

*

*/

public class AppConstants {

//jdbc

public static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncodeing=UTF-8";

public static final String JDBC_USERNAME = "root";

public static final String JDBC_PASSWORD = "123456";

public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

// student field

public static final String STUDENT_NAME = "姓名";

public static final String STUDENT_SNO = "学号";

public static final String STUDENT_SEX = "性别";

public static final String STUDENT_DEPARTMETN = "院系";

public static final String STUDENT_HOMETOWN = "籍贯";

public static final String STUDENT_EMAIL = "电子邮件";

public static final String STUDENT_TEL = "联系方式";

public static final String STUDENT_MARK = "学分";

// login view

public static final String LOGIN_TITLE = "登录界面";

public static final String LOGIN_USERNAME = "用户名";

public static final String LOGIN_PASSWORD = "密码";

public static final String LOGIN = "登录";

public static final String RESET = "重置";

// main view

public static final String MAINVIEW_TITLE = "学生信息管理系统";

public static final String MAINVIEW_PAGENUM_JLABEL_DI = "第 ";

public static final String MAINVIEW_PAGENUM_JLABEL_YE = "/99 页";

public static final String MAINVIEW_FIND_JLABEL = "查询结果";

public static final String MAINVIEW_FIRST = "首页";

public static final String MAINVIEW_LAST = "末页";

public static final String MAINVIEW_PRE = "上一页";

public static final String MAINVIEW_NEXT = "下一页";

public static final String PARAM_FIND_CONDITION = "";

public static final String PARAM_FIND = "查找";

public static final String PARAM_ADD = "添加";

public static final String PARAM_DELETE = "删除";

public static final String PARAM_UPDATE = "更新";

// add view

public static final String ADDVIEW_TITLE = "添加学生信息";

public static final String ADDVIEW_ADDBUTTON = "添加";

public static final String EXITBUTTON = "退出";

// delete view

public static final String DELETEVIEW_TITLE = "删除学生信息";

public static final String DELETEVIEW_DELETEBUTTON = "删除";

// update view

public static final String UPDATEVIEW_TITLE = "更新学生信息";

public static final String UPDATEVIEW_UPDATEBUTTON = "更新";

}

package temp;

/**

* 定制枚举类型

* @author Administrator

*

*/

public enum DAO {

AdminDAO,StudentDAO;

}

util包下:

package util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import temp.AppConstants;

public class DBUtil {

private static DBUtil db;

private Connection conn;

private PreparedStatement ps;

private ResultSet rs;

private DBUtil() {

}

public static DBUtil getDBUtil() {

if (db == null) {

db = new DBUtil();

}

return db;

}

public int executeUpdate(String sql) {

int result = -1;

if (getConn() == null) {

return result;

}

try {

ps = conn.prepareStatement(sql);

result = ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}

return result;

}

public int executeUpdate(String sql, Object[] obj) {

int result = -1;

if (getConn() == null) {

return result;

}

try {

ps = conn.prepareStatement(sql);

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

ps.setObject(i + 1, obj[i]);

}

result = ps.executeUpdate();

close();

} catch (SQLException e) {

e.printStackTrace();

}

return result;

}

public ResultSet executeQuery(String sql) {

if (getConn() == null) {

return null;

}

try {

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

} catch (SQLException e) {

e.printStackTrace();

}

return rs;

}

public ResultSet executeQuery(String sql, Object[] obj) {

if (getConn() == null) {

return null;

}

try {

ps = conn.prepareStatement(sql);

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

ps.setObject(i + 1, obj[i]);

}

rs = ps.executeQuery();

} catch (SQLException e) {

e.printStackTrace();

}

return rs;

}

private Connection getConn() {

try {

if (conn == null || conn.isClosed()) {

Class.forName(AppConstants.JDBC_DRIVER);

conn = DriverManager.getConnection(AppConstants.JDBC_URL, AppConstants.JDBC_USERNAME,

temp.AppConstants.JDBC_PASSWORD);

}

} catch (ClassNotFoundException e) {

System.out.println("jdbc driver is not found.");

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

public void close() {

try {

if (rs != null) {

rs.close();

}

if (ps != null) {

ps.close();

}

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

view包下:

package view;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import base.BaseDAO;

import dao.StudentDAO;

import model.Student;

import temp.AppConstants;

import temp.DAO;

/**

* 添加学生

* @author Administrator

*

*/

public class AddView extends JFrame{

private static final long serialVersionUID = 2411072805644479176L;

private JPanel jPanelCenter, jPanelSouth;

private JButton addButton, exitButton;

private JTextField name, sno, department, hometown, mark, email, tel, sex;

public AddView() {

init();

}

private void init() {

setTitle(AppConstants.ADDVIEW_TITLE);

// center panel

jPanelCenter = new JPanel();

jPanelCenter.setLayout(new GridLayout(9, 2));

jPanelCenter.add(new JLabel(AppConstants.STUDENT_NAME));

name = new JTextField();

jPanelCenter.add(name);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_SNO));

sno = new JTextField();

jPanelCenter.add(sno);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_SEX));

sex = new JTextField();

jPanelCenter.add(sex);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_DEPARTMETN));

department = new JTextField();

jPanelCenter.add(department);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_HOMETOWN));

hometown = new JTextField();

jPanelCenter.add(hometown);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_MARK));

mark = new JTextField();

jPanelCenter.add(mark);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_EMAIL));

email = new JTextField();

jPanelCenter.add(email);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_TEL));

tel = new JTextField();

jPanelCenter.add(tel);

jPanelCenter.add(new JLabel("-------------------------------------------------"));

jPanelCenter.add(new JLabel("-------------------------------------------------"));

// south panel

jPanelSouth = new JPanel();

jPanelSouth.setLayout(new GridLayout(1, 2));

addButton = new JButton(AppConstants.ADDVIEW_ADDBUTTON);

addButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (check()) {

Student stu = new Student();

buildStudent(stu);

boolean isSuccess = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).add(stu);

if (isSuccess) {

setEmpty();

if (MainView.currPageNum < 0 || MainView.currPageNum > 99) {

MainView.currPageNum = 1;

}

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO))

.list(MainView.currPageNum);

MainView.initJTable(MainView.jTable, result);

}

}

}

});

jPanelSouth.add(addButton);

exitButton = new JButton(AppConstants.EXITBUTTON);

exitButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

dispose();

}

});

jPanelSouth.add(exitButton);

this.add(jPanelCenter, BorderLayout.CENTER);

this.add(jPanelSouth, BorderLayout.SOUTH);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setBounds(470, 200, 400, 270);

setResizable(false);

setVisible(true);

}

private boolean check() {

boolean result = false;

if ("".equals(name.getText()) || "".equals(sno.getText()) || "".equals(department.getText())

|| "".equals(sex.getText()) || "".equals(mark.getText()) || "".equals(tel.getText())

|| "".equals(email.getText()) || "".equals(hometown.getText())) {

return result;

} else {

result = true;

}

return result;

}

private void buildStudent(Student stu) {

stu.setDepartment(department.getText());

stu.setEmail(email.getText());

stu.setHomeTown(hometown.getText());

stu.setMark(mark.getText());

stu.setName(name.getText());

stu.setSno(sno.getText());

stu.setTel(tel.getText());

stu.setSex(sex.getText());

}

private void setEmpty() {

name.setText("");

sno.setText("");

department.setText("");

sex.setText("");

email.setText("");

hometown.setText("");

tel.setText("");

mark.setText("");

}

}

package view;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import base.BaseDAO;

import model.Student;

import dao.StudentDAO;

import temp.AppConstants;

import temp.DAO;

/**

* 删除学生

* @author Administrator

*

*/

public class DeleteView extends JFrame{

/**

* 添加已生成的串行版本标识

*/

private static final long serialVersionUID = 501541272051379470L;

private JPanel jPanelCenter, jPanelSouth;

private JButton deleteButton, exitButton;

private JTextField name, sno; // 根据姓名+学号删除学生

public DeleteView() {

init();

}

private void init() {

setTitle(AppConstants.DELETEVIEW_TITLE);

// center panel

jPanelCenter = new JPanel();

jPanelCenter.setLayout(new GridLayout(3, 2));

jPanelCenter.add(new JLabel(AppConstants.STUDENT_NAME));

name = new JTextField();

jPanelCenter.add(name);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_SNO));

sno = new JTextField();

jPanelCenter.add(sno);

jPanelCenter.add(new JLabel("-------------------------------------------------"));

jPanelCenter.add(new JLabel("-------------------------------------------------"));

// south panel

jPanelSouth = new JPanel();

jPanelSouth.setLayout(new GridLayout(1, 2));

deleteButton = new JButton(AppConstants.DELETEVIEW_DELETEBUTTON);

deleteButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (check()) {

Student stu = new Student();

buildStudent(stu);

boolean isSuccess = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).delete(stu);

if (isSuccess) {

setEmpty();

if (MainView.currPageNum < 0 || MainView.currPageNum > 99) {

MainView.currPageNum = 1;

}

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO))

.list(MainView.currPageNum);

MainView.initJTable(MainView.jTable, result);

}

}

}

});

jPanelSouth.add(deleteButton);

exitButton = new JButton(AppConstants.EXITBUTTON);

exitButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

dispose();

}

});

jPanelSouth.add(exitButton);

this.add(jPanelCenter, BorderLayout.CENTER);

this.add(jPanelSouth, BorderLayout.SOUTH);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setBounds(470, 250, 400, 130);

setResizable(false);

setVisible(true);

}

private boolean check() {

boolean result = false;

if ("".equals(name.getText()) || "".equals(sno.getText())) {

return result;

} else {

result = true;

}

return result;

}

private void buildStudent(Student stu) {

stu.setName(name.getText());

stu.setSno(sno.getText());

}

private void setEmpty() {

name.setText("");

sno.setText("");

}

}

package view;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

import base.BaseDAO;

import dao.AdminDAO;

import temp.AppConstants;

import temp.DAO;

/**

* 登录界面

* @author Administrator

*

*/

public class LoginView extends JFrame{

private static final long serialVersionUID = -4972101784603327025L;

private JPanel jPanelCenter, jPanelSouth;

private JTextField username;

private JPasswordField password;

private JButton loginButton, resetButton;

public LoginView() {

init();

}

private void init() {

this.setTitle("Login");

jPanelCenter = new JPanel();

jPanelCenter.setLayout(new GridLayout(3, 2));

jPanelCenter.add(new JLabel(AppConstants.LOGIN_USERNAME));

username = new JTextField();

jPanelCenter.add(username);

jPanelCenter.add(new JLabel(AppConstants.LOGIN_PASSWORD));

password = new JPasswordField();

// enter key listener

password.addKeyListener(new LoginListener());

jPanelCenter.add(password);

jPanelCenter.add(new JLabel("----------------------------------------------"));

jPanelCenter.add(new JLabel("----------------------------------------------"));

jPanelSouth = new JPanel();

jPanelSouth.setLayout(new GridLayout(1, 2));

loginButton = new JButton(AppConstants.LOGIN);

loginButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

check();

}

});

jPanelSouth.add(loginButton);

resetButton = new JButton(AppConstants.RESET);

resetButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

username.setText("");

password.setText("");

}

});

jPanelSouth.add(resetButton);

this.add(jPanelCenter, BorderLayout.CENTER);

this.add(jPanelSouth, BorderLayout.SOUTH);

this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

this.setBounds(450, 250, 375, 140);

this.setResizable(false);

this.setVisible(true);

}

private class LoginListener extends KeyAdapter {

@Override

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_ENTER) {

check();

}

}

}

private void check() {

AdminDAO adminDAO = (AdminDAO) BaseDAO.getAbilityDAO(DAO.AdminDAO);

if (adminDAO.queryForLogin(username.getText(), String.valueOf(password.getPassword()))) {

dispose();

new MainView();

} else {

username.setText("");

password.setText("");

}

}

}

package view;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.DefaultTableCellRenderer;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableColumn;

import dao.StudentDAO;

import base.BaseDAO;

import temp.AppConstants;

import temp.DAO;

/**

* 首页

* @author Administrator

*

*/

public class MainView extends JFrame{

/**

* 添加已生成的串行版本标识

*/

private static final long serialVersionUID = 6313156717813295316L;

private final int maxPageNum = 99;

private JPanel jPanelNorth, jPanelSouth, jPanelCenter;

private JButton jButtonFirst, jButtonLast, jButtonNext, jButtonPre, jButtonAdd, jButtonDelete, jButtonUpdate,

jButtonFind;

private JLabel currPageNumJLabel;

private JTextField condition;

public static JTable jTable;

private JScrollPane jScrollPane;

private DefaultTableModel myTableModel;

public static String[] column = { "id", AppConstants.STUDENT_NAME, AppConstants.STUDENT_SNO,

AppConstants.STUDENT_SEX, AppConstants.STUDENT_DEPARTMETN, AppConstants.STUDENT_HOMETOWN,

AppConstants.STUDENT_MARK, AppConstants.STUDENT_EMAIL, AppConstants.STUDENT_TEL };

public static int currPageNum = 1;

public MainView() {

init();

}

private void init() {

setTitle(AppConstants.MAINVIEW_TITLE);

// north panel

jPanelNorth = new JPanel();

jPanelNorth.setLayout(new GridLayout(1, 5));

condition = new JTextField(AppConstants.PARAM_FIND_CONDITION);

condition.addKeyListener(new FindListener());

jPanelNorth.add(condition);

// query by name

jButtonFind = new JButton(AppConstants.PARAM_FIND);

jButtonFind.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

find();

}

});

jButtonFind.addKeyListener(new FindListener());

// add

jPanelNorth.add(jButtonFind);

jButtonAdd = new JButton(AppConstants.PARAM_ADD);

jButtonAdd.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

new AddView();

}

});

jPanelNorth.add(jButtonAdd);

// delete

jButtonDelete = new JButton(AppConstants.PARAM_DELETE);

jButtonDelete.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

new DeleteView();

}

});

jPanelNorth.add(jButtonDelete);

// update

jButtonUpdate = new JButton(AppConstants.PARAM_UPDATE);

jButtonUpdate.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

new UpdateView();

}

});

jPanelNorth.add(jButtonUpdate);

// center panel

jPanelCenter = new JPanel();

jPanelCenter.setLayout(new GridLayout(1, 1));

// init jTable

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).list(currPageNum);

myTableModel = new DefaultTableModel(result, column);

jTable = new JTable(myTableModel);

DefaultTableCellRenderer cr = new DefaultTableCellRenderer();

cr.setHorizontalAlignment(JLabel.CENTER);

jTable.setDefaultRenderer(Object.class, cr);

initJTable(jTable, result);

jScrollPane = new JScrollPane(jTable);

jPanelCenter.add(jScrollPane);

// south panel

jPanelSouth = new JPanel();

jPanelSouth.setLayout(new GridLayout(1, 5));

jButtonFirst = new JButton(AppConstants.MAINVIEW_FIRST);

jButtonFirst.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

currPageNum = 1;

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).list(currPageNum);

initJTable(jTable, result);

currPageNumJLabel.setText(AppConstants.MAINVIEW_PAGENUM_JLABEL_DI + currPageNum

+ AppConstants.MAINVIEW_PAGENUM_JLABEL_YE);

}

});

jButtonPre = new JButton(AppConstants.MAINVIEW_PRE);

jButtonPre.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

currPageNum--;

if (currPageNum <= 0) {

currPageNum = 1;

}

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).list(currPageNum);

initJTable(jTable, result);

currPageNumJLabel.setText(AppConstants.MAINVIEW_PAGENUM_JLABEL_DI + currPageNum

+ AppConstants.MAINVIEW_PAGENUM_JLABEL_YE);

}

});

jButtonNext = new JButton(AppConstants.MAINVIEW_NEXT);

jButtonNext.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

currPageNum++;

if (currPageNum > maxPageNum) {

currPageNum = maxPageNum;

}

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).list(currPageNum);

initJTable(jTable, result);

currPageNumJLabel.setText(AppConstants.MAINVIEW_PAGENUM_JLABEL_DI + currPageNum

+ AppConstants.MAINVIEW_PAGENUM_JLABEL_YE);

}

});

jButtonLast = new JButton(AppConstants.MAINVIEW_LAST);

jButtonLast.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

currPageNum = maxPageNum;

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).list(currPageNum);

initJTable(jTable, result);

currPageNumJLabel.setText(AppConstants.MAINVIEW_PAGENUM_JLABEL_DI + currPageNum

+ AppConstants.MAINVIEW_PAGENUM_JLABEL_YE);

}

});

currPageNumJLabel = new JLabel(

AppConstants.MAINVIEW_PAGENUM_JLABEL_DI + currPageNum + AppConstants.MAINVIEW_PAGENUM_JLABEL_YE);

currPageNumJLabel.setHorizontalAlignment(JLabel.CENTER);

jPanelSouth.add(jButtonFirst);

jPanelSouth.add(jButtonPre);

jPanelSouth.add(currPageNumJLabel);

jPanelSouth.add(jButtonNext);

jPanelSouth.add(jButtonLast);

this.add(jPanelNorth, BorderLayout.NORTH);

this.add(jPanelCenter, BorderLayout.CENTER);

this.add(jPanelSouth, BorderLayout.SOUTH);

setBounds(400, 200, 750, 340);

setResizable(false);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setVisible(true);

}

public static void initJTable(JTable jTable, String[][] result) {

((DefaultTableModel) jTable.getModel()).setDataVector(result, column);

jTable.setRowHeight(20);

TableColumn firsetColumn = jTable.getColumnModel().getColumn(0);

firsetColumn.setPreferredWidth(30);

firsetColumn.setMaxWidth(30);

firsetColumn.setMinWidth(30);

TableColumn secondColumn = jTable.getColumnModel().getColumn(1);

secondColumn.setPreferredWidth(60);

secondColumn.setMaxWidth(60);

secondColumn.setMinWidth(60);

TableColumn thirdColumn = jTable.getColumnModel().getColumn(2);

thirdColumn.setPreferredWidth(90);

thirdColumn.setMaxWidth(90);

thirdColumn.setMinWidth(90);

TableColumn fourthColumn = jTable.getColumnModel().getColumn(3);

fourthColumn.setPreferredWidth(30);

fourthColumn.setMaxWidth(30);

fourthColumn.setMinWidth(30);

TableColumn seventhColumn = jTable.getColumnModel().getColumn(6);

seventhColumn.setPreferredWidth(30);

seventhColumn.setMaxWidth(30);

seventhColumn.setMinWidth(30);

TableColumn ninthColumn = jTable.getColumnModel().getColumn(8);

ninthColumn.setPreferredWidth(90);

ninthColumn.setMaxWidth(90);

ninthColumn.setMinWidth(90);

}

private class FindListener extends KeyAdapter {

@Override

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_ENTER) {

find();

}

}

}

private void find() {

currPageNum = 0;

String param = condition.getText();

if ("".equals(param) || param == null) {

initJTable(MainView.jTable, null);

currPageNumJLabel.setText(AppConstants.MAINVIEW_FIND_JLABEL);

return;

}

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).queryByName(param);

condition.setText("");

initJTable(MainView.jTable, result);

currPageNumJLabel.setText(AppConstants.MAINVIEW_FIND_JLABEL);

}

}

package view;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import base.BaseDAO;

import dao.StudentDAO;

import model.Student;

import temp.AppConstants;

import temp.DAO;

/**

* 更新学生信息

* @author Administrator

*

*/

public class UpdateView extends JFrame{

/**

* 添加已生成的串行版本标识

*/

private static final long serialVersionUID = -1259286514124919662L;

private JPanel jPanelCenter, jPanelSouth;

private JButton updateButton, exitButton;

private JTextField name, sno, department, hometown, mark, email, tel, sex;

public UpdateView() {

init();

}

private void init() {

setTitle(AppConstants.UPDATEVIEW_TITLE);

// center panel

jPanelCenter = new JPanel();

jPanelCenter.setLayout(new GridLayout(9, 2));

jPanelCenter.add(new JLabel(AppConstants.STUDENT_NAME));

name = new JTextField();

jPanelCenter.add(name);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_SNO));

sno = new JTextField();

jPanelCenter.add(sno);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_SEX));

sex = new JTextField();

jPanelCenter.add(sex);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_DEPARTMETN));

department = new JTextField();

jPanelCenter.add(department);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_HOMETOWN));

hometown = new JTextField();

jPanelCenter.add(hometown);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_MARK));

mark = new JTextField();

jPanelCenter.add(mark);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_EMAIL));

email = new JTextField();

jPanelCenter.add(email);

jPanelCenter.add(new JLabel(AppConstants.STUDENT_TEL));

tel = new JTextField();

jPanelCenter.add(tel);

jPanelCenter.add(new JLabel("-------------------------------------------------"));

jPanelCenter.add(new JLabel("-------------------------------------------------"));

// south panel

jPanelSouth = new JPanel();

jPanelSouth.setLayout(new GridLayout(1, 2));

updateButton = new JButton(AppConstants.UPDATEVIEW_UPDATEBUTTON);

updateButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (check()) {

Student stu = new Student();

buildStudent(stu);

boolean isSuccess = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).update(stu);

if (isSuccess) {

setEmpty();

if (MainView.currPageNum < 0 || MainView.currPageNum > 99) {

MainView.currPageNum = 1;

}

String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO))

.list(MainView.currPageNum);

MainView.initJTable(MainView.jTable, result);

}

}

}

});

jPanelSouth.add(updateButton);

exitButton = new JButton(AppConstants.EXITBUTTON);

exitButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

dispose();

}

});

jPanelSouth.add(exitButton);

this.add(jPanelCenter, BorderLayout.CENTER);

this.add(jPanelSouth, BorderLayout.SOUTH);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setBounds(470, 200, 400, 270);

setResizable(false);

setVisible(true);

}

private boolean check() {

boolean result = false;

if ("".equals(name.getText()) || "".equals(sno.getText()) || "".equals(department.getText())

|| "".equals(sex.getText()) || "".equals(mark.getText()) || "".equals(tel.getText())

|| "".equals(email.getText()) || "".equals(hometown.getText())) {

return result;

} else {

result = true;

}

return result;

}

private void buildStudent(Student stu) {

stu.setDepartment(department.getText());

stu.setEmail(email.getText());

stu.setHomeTown(hometown.getText());

stu.setMark(mark.getText());

stu.setName(name.getText());

stu.setSno(sno.getText());

stu.setTel(tel.getText());

stu.setSex(sex.getText());

}

private void setEmpty() {

name.setText("");

sno.setText("");

department.setText("");

sex.setText("");

email.setText("");

hometown.setText("");

tel.setText("");

mark.setText("");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值