package com.qxl;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Collection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import com.qxl.DAO.IStudentDao;
import com.qxl.DAO.StudentDaoImpl;
import com.qxl.bo.Student;
import com.qxl.util.MyFrame;
/**
* 此窗口用来显示所有的使学生信息
*/
public class Show extends JPanel{
private IStudentDao stuDao;
JFrame frame;
private JScrollPane scrollPane;
private JTableHeader th;
private JTable table;
private JButton bt_show;
private JButton bt_add;
private JButton bt_delete;
private JButton bt_modify;
public Show(){
stuDao = StudentDaoImpl.getInstance();
initComponent();
frame = new MyFrame("Show");
frame.add(this);
selectAll();
}
private void initComponent(){
this.setLayout(null);
int x_scroll = 10;
int y_scroll = 10;
int width = 560;
int height = 240;
scrollPane = new JScrollPane();
scrollPane.setBounds(x_scroll,y_scroll,width,height);
this.add(scrollPane);
int x_bt = 60;
int y_bt = y_scroll+height+20;
int width_bt = 120;
bt_add = new JButton("增加");
bt_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose(); //当前窗口释放
new Add();
}
});
bt_add.setBounds(x_bt,y_bt,60,30);
this.add(bt_add);
bt_delete = new JButton("删除");
bt_delete.setBounds(x_bt+width_bt*1,y_bt,60,30);
bt_delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
//System.out.println(row);
if(row==-1){
JOptionPane.showMessageDialog(null,"未选中任意一行","错误",JOptionPane.ERROR_MESSAGE);
}else if(row>=0){
//获取第row+1行第1列的单元格的值
Integer id = (Integer)table.getValueAt(row,0);
//YES-0 NO-1 CANCEl-2
String sname = (String)table.getValueAt(row,1);
int n = JOptionPane.showConfirmDialog(null,"确定删除姓名为"+sname+"的记录");
//System.out.println(n);
//System.out.println("id:"+id);
if(n==0){
delete(id);
}
}
}
});
this.add(bt_delete);
bt_modify = new JButton("修改");
bt_modify.setBounds(x_bt+width_bt*2,y_bt,60,30);
bt_modify.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
//System.out.println(row);
if(row==-1){
JOptionPane.showMessageDialog(null,"未选中任意一行","错误",JOptionPane.ERROR_MESSAGE);
}else if(row>=0){
//获取第row+1行第1列的单元格的值
Integer id = (Integer)table.getValueAt(row,0);
//YES-0 NO-1 CANCEl-2
String sname = (String)table.getValueAt(row,1);
String gender = (String)table.getValueAt(row,2);
String address = (String)table.getValueAt(row,3);
Student student = new Student(id, sname, gender, address);
frame.dispose(); //当前窗口释放
new Update(student);
}
}
});
this.add(bt_modify);
bt_show = new JButton("显示");
bt_show.setBounds(x_bt+width_bt*3,y_bt,60,30);
bt_show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//System.out.println("show_button");
selectAll();
}
});
this.add(bt_show);
}
/**
* 删除指定的记录
*/
private void delete(int id){
try {
boolean flag = stuDao.delete(id);
if(flag){
JOptionPane.showMessageDialog(null,"成功删除!","提示",JOptionPane.INFORMATION_MESSAGE);
selectAll();
}else{
JOptionPane.showMessageDialog(null,"删除出错!","错误",JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询数据库所有数据
*/
private void selectAll(){
try {
Collection<Student> c = stuDao.showAll();
String[] title = new String[]{"id","姓名","性别","住址"};
Object[][] info = new Object[c.size()][4];
int count = 0;
for(Student s : c){
info[count][0] = s.getId();
info[count][1] = s.getSname();
info[count][2] = s.getGender();
info[count++][3] = s.getAddress();
}
this.table = new JTable(info,title);
//显示表头
this.th = this.table.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scrollPane.getViewport().add(table);
} catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
} catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据库错误","错误",JOptionPane.ERROR_MESSAGE);
} catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Show show = new Show();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java学生管理系统-数据库小项目
共38个文件
class:23个
java:11个
prefs:1个
需积分: 50 78 下载量 6 浏览量
2015-09-27
00:20:49
上传
评论 3
收藏 40KB ZIP 举报
温馨提示
学生管理系统,本项目是用java写的用的mysql数据库,主要用来练习数据库方法的一些知识
资源推荐
资源详情
资源评论
收起资源包目录
MyDBSystem.zip (38个子文件)
MyDBSystem
bin
com
qxl
Show$1.class 750B
Index.class 3KB
Index$1.class 2KB
Update.class 5KB
Add$1.class 2KB
Add.class 4KB
bo
Student.class 2KB
Update$3.class 784B
Show$2.class 2KB
SysStart.class 417B
Add$3.class 763B
Show$3.class 2KB
Show.class 5KB
Index$MyListener.class 1011B
Add$2.class 645B
util
DBUtil.class 2KB
MyFrame.class 778B
Constant.class 410B
Show$4.class 651B
Update$2.class 784B
Update$1.class 2KB
DAO
StudentDaoImpl.class 4KB
IStudentDao.class 391B
.settings
org.eclipse.jdt.core.prefs 629B
src
com
qxl
Add.java 4KB
SysStart.java 113B
Update.java 5KB
bo
Student.java 1KB
Show.java 5KB
util
MyFrame.java 475B
Constant.java 120B
DBUtil.java 2KB
Index.java 2KB
DAO
StudentDaoImpl.java 4KB
IStudentDao.java 402B
.project 386B
config.properties 138B
.classpath 576B
共 38 条
- 1
资源评论
龙腾四海365
- 粉丝: 176
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Ruby基于Ruby的MKS rebase脚本 Ruby语言基础
- 基于GPT+Pyhon开发的网页游戏,类似于最火的文字游戏《人生重开模拟器》源码
- 基于qt+C++实现u盘插拔检测.+源码(毕业设计&课程设计&项目开发)
- rust开发封装的windows gui源码
- checkpoint.zip
- 无人机姿态和轨迹跟踪控制-反步控制方法
- 基于Qt+opencv+C++实现图像旋转+自动&&手动+直线检测,角度计算+界面操作+源码(期末大作业&课程设计&项目开发)
- Object-CC++对象库,实现类似于Java的Object类的Object
- 基于servlet的简单游戏管理系统
- 广工基于Libero的数字逻辑设计仿真及验证实验报告
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功