package com.shuise.project.run;
import com.shuise.project.view.OriginView;
public class Main {
public static void main(String[] args) {
new OriginView();
}
}
package com.shuise.project.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
/**
* 模块说明:数据库工具类
*
*/
public class JDBCUtils {
private static JDBCUtils db;
private static Connection conn;
private PreparedStatement ps;
private ResultSet rs;
private JDBCUtils() {
}
public static JDBCUtils getDBUtil() {
if (db == null) {
db = new JDBCUtils();
}
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 static String driver;
private static String url;
private static String username;
private static String password;
static{
try {
// 1.获取当前类获取类加载器
ClassLoader classloader = JDBCUtils.class.getClassLoader();
// 2.通过类加载器的方法获得一个输入流
InputStream is = classloader.getResourceAsStream("db.properties");
// 3.创建一个properties对象
Properties props = new Properties();
// 4.加载输入流
props.load(is);
//5.获取相干参数
ResourceBundle rb = ResourceBundle.getBundle("db");
driver = rb.getString("driver");
url =rb.getString("url");
username =rb.getString("username");
password =rb.getString("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn() {
try {
if (conn == null || conn.isClosed()) {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
}
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "mysql连接失败");
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();
}
}
}
(1)增加记录操作
package com.shuise.project.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
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 com.shuise.project.DAO;
import com.shuise.project.base.BaseDAO;
import com.shuise.project.dao.StudentDAO;
import com.shuise.project.model.Student;
import com.shuise.project.view.MainView;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class AddView extends JFrame {
/**
* 添加
*/
private static final long serialVersionUID = 1L;
private JPanel add_center,add_south;
private JLabel
JL_name,JL_sex,JL_Id_learn,JL_department,JL_major,JL_classes,
JL_score,JL_home,JL_email,JL_phone,JL_study_time,JL_dorm;
private Font font2 =new Font("xxx",Font.BOLD,14);
private JTextField name,sex,Id_learn,department,classes,major,home,phone,score,
email,study_time,dorm ;
private JButton add_enter,add_canel;
public AddView() {
init();
}
public void init(){
this.setVisible(true);
this.setTitle("添加学生信息");
this.setSize(400, 600);
this.setLocation(800, 200);
this.setResizable(false); //设置不可缩放
add_center = new JPanel();
add_center.setLayout(new GridLayout(15, 2));
add_center.setBackground(Color.pink);
add_south = new JPanel();
add_south.setLayout(new GridLayout(1, 2));
add_south.setBackground(Color.blue);
//创建标签
JL_name = new JLabel(" 姓 名");
JL_name.setFont(font2);
JL_sex = new JLabel(" 性 别");
JL_sex.setFont(font2);
JL_Id_learn = new JLabel(" 学 号");
JL_Id_learn.setFont(font2);
JL_department = new JLabel(" 院 系");
JL_department.setFont(font2);
JL_major = new JLabel(" 专 业");
JL_major.setFont(font2);
JL_classes = new JLabel(" 班 级");
JL_classes.setFont(font2);
JL_dorm = new JLabel(" 宿 舍");
JL_dorm.setFont(font2);
JL_study_time = new JLabel(" 入校-毕业时间");
JL_study_time.setFont(font2);
JL_home = new JLabel(" 籍 贯");
JL_home.setFont(font2);
JL_score = new JLabel(" 分 数");
JL_score.setFont(font2);
JL_email = new JLabel(" 电子 邮箱");
JL_email.setFont(font2);
JL_phone = new JLabel(" 联系 方式");
JL_phone.setFont(font2);
//创建文本框
name = new JTextField(20);
name.setFont(font2);
sex = new JTextField(20);
sex.setFont(font2);
Id_learn = new JTextField(20);
Id_learn.setFont(font2);
department = new JTextField(20);
department.setFont(font2);
major = new JTextField(20);
major.setFont(font2);
classes = new JTextField(20);
classes.setFont(font2);
dorm = new JTextField(20);
dorm.setFont(font2);
study_time = new JTextField(20);
study_time.setFont(font2);
home = new JTextField(20);
home.setFont(font2);
score = new JTextField(20);
score.setFont(font2);
email = new JTextField(20);
email.setFont(font2);
phone = new JTextField(20);
phone.setFont(font2);
//创建按钮
add_enter = new JButton("确定");
add_canel = new JButton("取消");
//将组件添加到中心面板
add_center.add(JL_name); add_center.add(name);
add_center.add(JL_sex); add_center.add(sex);
add_center.add(JL_Id_learn); add_center.add(Id_learn);
add_center.add(JL_department); add_center.add(department);
add_center.add(JL_major); add_center.add(major);
add_center.add(JL_classes); add_center.add(classes);
add_center.add(JL_dorm); add_center.add(dorm);
add_center.add(JL_study_time); add_center.add(study_time);
add_center.add(JL_home); add_center.add(home);
add_center.add(JL_score); add_center.add(score);
add_center.add(JL_email); add_center.add(email);
add_center.add(JL_phone); add_center.add(phone);
//将组件添加到南面板
add_south.add(add_enter);
add_south.add(add_canel);
//将面板添加到窗体
this.add(add_center,BorderLayout.CENTER);
this.add(add_south,BorderLayout.SOUTH);
//add 确定键 鼠标点击 关闭添加窗口
add_enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add_check();
}
});
//add 确定键键盘 关闭添加窗口
add_enter.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
add_check();
}
}
});
//add 取消按钮 鼠标点击 关闭添加窗口
add_canel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
//add 取消按钮 键盘 关闭添加窗口
add_canel.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
dispose();
}
}
});
}
private void add_check() {
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);
}
}
}
private void buildStudent(Student stu) {
stu.setName(name.getText());
stu.setSex(sex.getText());
stu.setId_learn(Id_learn.getText());
stu.setDepartment(department.getText());
stu.setMajor(major.getText());
stu.setClasses(classes.getText());
stu.setDorm(dorm.getText());
stu.setStudy_time(study_time.getText());
stu.setHome(home.getText());
stu.setScore(score.getText());
stu.setEmail(email.getText());
stu.setPhone(phone.getText());
}
private void setEmpty() {
name.setText("");
sex.setText("");
Id_learn.setText("");
department.setText("");
major.setText("");
classes.setText("");
dorm.setText("");
study_time.setText("");
home.setText("");
score.setText("");
email.setText("");
phone.setText("");
}
private boolean check() {
boolean result = false;
if ("".equals(name.getText()) || "".equals(sex.getText())
|| "".equals(Id_learn.getText())|| "".equals(department.getText())
|| "".equals(major.getText()) ||
"".equals(classes.getText()) || "".equals(dorm.getText())|| "".equals(study_time.getText())
|| "".equals(email.getText())
|| "".equals(home.getText()) || "".equals(score.getText()) || "".equals(phone.getText())) {
return result;
} else {
result = true;
}
return result;
}
}
(2)删除记录操作
package com.shuise.project.view;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
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 com.shuise.project.DAO;
import com.shuise.project.base.BaseDAO;
import com.shuise.project.dao.StudentDAO;
import com.shuise.project.model.Student;
import com.shuise.project.view.MainView;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class DeleteView extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel delete_north,delete_south,delete_center;
private JLabel Lab_name,Lab_Id_learn;
private Font font =new Font("xxx",Font.BOLD+Font.ITALIC,17);
private Font font1 =new Font("xxx",Font.PLAIN,14);
private JTextField name,Id_learn ;
private JButton delete_enter,delete_canel;
public DeleteView(){
init();
}
public void init(){
this.setTitle("删除学生信息");
setLocation(800,350);
setSize(400, 200);
this.setVisible(true);
//this.setResizable(false); //设置不可缩放
//面板
delete_north = new JPanel();
delete_north.setBackground(Color.pink);
delete_center = new JPanel();
delete_center.setBackground(Color.pink);
delete_south = new JPanel();
delete_south.setBackground(Color.pink);
//标签
Lab_name = new JLabel("姓 名"); Lab_name.setFont(font);
Lab_Id_learn = new JLabel("学 号"); Lab_Id_learn.setFont(font);
//按钮
delete_enter = new JButton("确定");
delete_canel = new JButton("取消");
//创建文本框
name = new JTextField(20); name.setFont(font1);
Id_learn = new JTextField(20); Id_learn.setFont(font1);
this.setLayout(new GridLayout(3, 1));
//将组件添加到北面板
delete_north.add(Lab_name); delete_north.add(name);
//将组件添加到中心面板
delete_center.add(Lab_Id_learn); delete_center.add(Id_learn);
//将组件添加到南面板
delete_south.add(delete_enter);
delete_south.add(delete_canel);
//将面板添加到窗体
this.add(delete_north,BorderLayout.NORTH);
this.add(delete_center,BorderLayout.CENTER);
this.add(delete_south,BorderLayout.SOUTH);
//add 确定键 鼠标点击 关闭添加窗口
delete_enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
delete_check();
}
});
//add 确定键键盘 关闭添加窗口
delete_enter.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
delete_check();
}
}
});
//add 取消按钮 鼠标点击 关闭添加窗口
delete_canel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
//add 取消按钮 键盘 关闭添加窗口
delete_canel.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
dispose();
}
}
});
}
private void delete_check() {
if (check()) {
Student stu = new Student();
deleteStudent(stu);
boolean isDeleteSuccess = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).delete(stu);
if(isDeleteSuccess){
JOptionPane.showMessageDialog(null, "学生"+stu.getName()+"信息删除成功");
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);
}else {
JOptionPane.showMessageDialog(null, "学生"+stu.getName()+"信息删除失败");
}
}
}
private void deleteStudent(Student stu) {
stu.setName(name.getText());
stu.setId_learn(Id_learn.getText());
}
private void setEmpty() {
name.setText("");
Id_learn.setText("");
}
private boolean check() {
boolean result = false;
if ("".equals(name.getText()) || "".equals(Id_learn.getText())) {
return result;
} else {
result = true;
}
return result;
}
}
(3)修改操作
package com.shuise.project.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
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 com.shuise.project.DAO;
import com.shuise.project.base.BaseDAO;
import com.shuise.project.dao.StudentDAO;
import com.shuise.project.model.Student;
import com.shuise.project.view.MainView;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ModifyView extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel modify_center,modify_south;
private JLabel Lab_name,Lab_sex,Lab_Id_learn,Lab_department,Lab_major,Lab_classes,Lab_score,Lab_home,Lab_email,Lab_phone,Lab_study_time,Lab_dorm;
private Font font2 =new Font("xxx",Font.BOLD,14);
private Font font1 =new Font("xxx",Font.BOLD,18);
private JTextField name,sex,Id_learn,department,classes,major,home,phone,score,email,study_time,dorm ;
private JButton modify_enter,add_canel;
public ModifyView() {
init();
}
public void init() {
this.setVisible(true);
this.setTitle("修改学生信息");
this.setSize(400, 600);
this.setLocation(800, 200);
this.setResizable(false); //设置不可缩放
modify_center = new JPanel();
modify_center.setLayout(new GridLayout(15, 2));
modify_center.setBackground(Color.pink);
modify_south = new JPanel();
modify_south.setLayout(new GridLayout(1, 2));
modify_south.setBackground(Color.blue);
//创建标签
Lab_Id_learn = new JLabel(" 修改学生的学号"); Lab_Id_learn.setFont(font1);
Lab_name = new JLabel(" 姓 名"); Lab_name.setFont(font2);
Lab_sex = new JLabel(" 性 别"); Lab_sex.setFont(font2);
Lab_department = new JLabel(" 院 系"); Lab_department.setFont(font2);
Lab_major = new JLabel(" 专 业"); Lab_major.setFont(font2);
Lab_classes = new JLabel(" 班 级"); Lab_classes.setFont(font2);
Lab_dorm = new JLabel(" 宿 舍"); Lab_dorm.setFont(font2);
Lab_study_time = new JLabel(" 入校——毕业时间"); Lab_study_time.setFont(font2);
Lab_home = new JLabel(" 籍 贯"); Lab_home.setFont(font2);
Lab_score = new JLabel(" 分 数"); Lab_score.setFont(font2);
Lab_email = new JLabel(" 电子 邮箱"); Lab_email.setFont(font2);
Lab_phone = new JLabel(" 联系 方式"); Lab_phone.setFont(font2);
//创建文本框
Id_learn = new JTextField(20); Id_learn.setFont(font2);
name = new JTextField(20); name.setFont(font2);
sex = new JTextField(20); sex.setFont(font2);
department = new JTextField(20); department.setFont(font2);
major = new JTextField(20); major.setFont(font2);
classes = new JTextField(20); classes.setFont(font2);
dorm = new JTextField(20); dorm.setFont(font2);
study_time = new JTextField(20); study_time.setFont(font2);
home = new JTextField(20); home.setFont(font2);
score = new JTextField(20); score.setFont(font2);
email = new JTextField(20); email.setFont(font2);
phone = new JTextField(20); phone.setFont(font2);
//创建按钮
modify_enter = new JButton("确定");
add_canel = new JButton("取消");
//将组件添加到中心面板
modify_center.add(Lab_Id_learn); modify_center.add(Id_learn);
modify_center.add(Lab_name); modify_center.add(name);
modify_center.add(Lab_sex); modify_center.add(sex);
modify_center.add(Lab_department); modify_center.add(department);
modify_center.add(Lab_major); modify_center.add(major);
modify_center.add(Lab_classes); modify_center.add(classes);
modify_center.add(Lab_dorm); modify_center.add(dorm);
modify_center.add(Lab_study_time); modify_center.add(study_time);
modify_center.add(Lab_home); modify_center.add(home);
modify_center.add(Lab_score); modify_center.add(score);
modify_center.add(Lab_email); modify_center.add(email);
modify_center.add(Lab_phone); modify_center.add(phone);
//将组件添加到南面板
modify_south.add(modify_enter);
modify_south.add(add_canel);
//将面板添加到窗体
this.add(modify_center,BorderLayout.CENTER);
this.add(modify_south,BorderLayout.SOUTH);
//add 确定键 鼠标点击 关闭添加窗口
modify_enter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
modify_check();
}
});
//add 确定键键盘 关闭添加窗口
modify_enter.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
modify_check();
}
}
});
//add 取消按钮 鼠标点击 关闭添加窗口
add_canel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
//add 取消按钮 键盘 关闭添加窗口
add_canel.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
dispose();
}
}
});
}
private void modify_check() {
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);
}
}
}
private void buildStudent(Student stu) {
stu.setId_learn(Id_learn.getText());
stu.setName(name.getText());
stu.setSex(sex.getText());
stu.setDepartment(department.getText());
stu.setMajor(major.getText());
stu.setClasses(classes.getText());
stu.setDorm(dorm.getText());
stu.setStudy_time(study_time.getText());
stu.setHome(home.getText());
stu.setScore(score.getText());
stu.setEmail(email.getText());
stu.setPhone(phone.getText());
}
private void setEmpty() {
name.setText("");
sex.setText("");
Id_learn.setText("");
department.setText("");
major.setText("");
classes.setText("");
dorm.setText("");
study_time.setText("");
home.setText("");
score.setText("");
email.setText("");
phone.setText("");
}
private boolean check() {
boolean result = false;
if ("".equals(name.getText()) || "".equals(sex.getText()) || "".equals(Id_learn.getText())|| "".equals(department.getText())
|| "".equals(major.getText()) || "".equals(classes.getText()) || "".equals(dorm.getText())|| "".equals(study_time.getText())
|| "".equals(email.getText()) || "".equals(home.getText()) || "".equals(score.getText()) || "".equals(phone.getText())) {
return result;
} else {
result = true;
}
return result;
}
}
(4)查询操作
private void find() {
currPageNum = 0;
String param = index.getText();
if ("".equals(param) || param == null) {
initJTable(MainView.jTable, null);
return;
}
String[][] result = ((StudentDAO) BaseDAO.getAbilityDAO(DAO.StudentDAO)).queryByName(param);
index.setText("");
initJTable(MainView.jTable, result);
}