public void freshTable(String sql){
myConnection conn=new myConnection();
ResultSet rs;
rs=conn.getResult(sql);
if (rs!=null){
try{
mm.setRowCount(0);
table.setModel(mm);
while(rs.next()){
String 学号 = rs.getString("学号");
String 姓名 = rs.getString("姓名");
String 性别 = rs.getString("性别");
String 民族 = rs.getString("民族");
String 年龄 = rs.getString("年龄");
String 年级 = rs.getString("年级");
String 班级 = rs.getString("班级");
String 专业 = rs.getString("专业");
String[] cloumns ={学号,姓名,性别,民族,年龄,年级,班级,专业};
mm.addRow(cloumns);
}
//table.clearSelection();
table.setModel(mm);
}catch(Exception e){
System.out.println(e.toString());
}
}
}
public void actionPerformed(ActionEvent e){
//退出
if (e.getSource()==miExit){
System.exit(0);
//查找
}else if(e.getSource()==miFind){
findFrame ff=new findFrame(this);
ff.setVisible(true);
//添加
}else if(e.getSource()==miAdd){
addFrame af=new addFrame(this);
af.setVisible(true);
//修改
}else if(e.getSource()==miEdit){
if (table.getSelectedRow()==-1){
JOptionPane.showMessageDialog(null, "请选择你要修改的内容", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else{
editFrame ef=new editFrame(this);
ef.学号.setText((String)table.getValueAt(table.getSelectedRow(),0));
ef.姓名.setText((String)table.getValueAt(table.getSelectedRow(),1));
ef.性别.setText((String)table.getValueAt(table.getSelectedRow(),2));
ef.民族.setText((String)table.getValueAt(table.getSelectedRow(),3));
ef.年龄.setText((String)table.getValueAt(table.getSelectedRow(),4));
ef.年级.setText((String)table.getValueAt(table.getSelectedRow(),5));
ef.班级.setText((String)table.getValueAt(table.getSelectedRow(),6));
ef.专业.setText((String)table.getValueAt(table.getSelectedRow(),7));
ef.setVisible(true);
this.freshTable("select * from 学生信息表");
}
//删除
}else if(e.getSource()==miDel){
if (table.getSelectedRow()==-1){
JOptionPane.showMessageDialog(null, "请选择你要删除的行", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else {
String sql="delete from 学生信息表 where 学号 = '"+table.getValueAt(table.getSelectedRow(),0)+"'";
//JOptionPane.showMessageDialog(null, sql, "温馨提示", JOptionPane.INFORMATION_MESSAGE);
myConnection conn=new myConnection();
if (conn.executeSql(sql)){
JOptionPane.showMessageDialog(null, "成功删除", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
this.freshTable("select * from 学生信息表");
}else {
JOptionPane.showMessageDialog(null, "未知错误", "删除失败", JOptionPane.INFORMATION_MESSAGE);
}
}
//显示
}else if(e.getSource()==miShow){
//JOptionPane.showMessageDialog(null, "未知错误", "删除失败", JOptionPane.INFORMATION_MESSAGE);
this.freshTable("select * from 学生信息表");
//用户管理
}else if(e.getSource()==miUser){
userFrame uf=new userFrame();
uf.setVisible(true);
//关于
}else if(e.getSource()==miAbout){
aboutFrame af=new aboutFrame();
af.setVisible(true);
}
}
}
class myConnection{
ResultSet re;
String strurl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=学生管理数据库.mdb";
public myConnection(){}
public ResultSet getResult(String sql){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(strurl);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet re=stmt.executeQuery(sql);
return re;
}
catch(Exception e){
System.out.println("getResult------"+e.toString());
return null;
}
}
public boolean executeSql(String sql){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(strurl);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
conn.commit();
return true;
}
catch(Exception e){
System.out.println("executeSql----"+e.toString());
return false;
}
}
}
class addFrame extends JDialog implements ActionListener{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
JLabel 学号1,姓名1,性别1,民族1,年龄1,年级1,班级1,专业1;
JTextField 学号,姓名,性别,民族,年龄,年级,班级,专业;
JButton b;
JPanel p;
mainFrame mf;
public addFrame(mainFrame mmf){
setTitle("修改学生信息");
setSize(WIDTH,HEIGHT);
setLocation(120,180);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
学号1=new JLabel("学号");
姓名1=new JLabel("姓名");
性别1=new JLabel("性别");
民族1=new JLabel("民族");
年龄1=new JLabel("年龄");
年级1=new JLabel("年级");
班级1=new JLabel("班级");
专业1=new JLabel("专业");
学号=new JTextField(5);
姓名=new JTextField(5);
性别=new JTextField(5);
民族=new JTextField(5);
年龄=new JTextField(5);
年级=new JTextField(5);
班级=new JTextField(10);
专业=new JTextField(10);
b=new JButton("确定");
p=new JPanel();
p.setLayout(new GridLayout(10,2,5,5));
p.add(学号1);
p.add(学号);
p.add(姓名1);
p.add(姓名);
p.add(性别1);
p.add(性别);
p.add(民族1);
p.add(民族);
p.add(年龄1);
p.add(年龄);
p.add(年级1);
p.add(年级);
p.add(班级1);
p.add(班级);
p.add(专业1);
p.add(专业);
p.add(new Label(""));
p.add(new Label(""));
p.add(b);
contentPane.add(p);
//添加按钮监听器
b.addActionListener(this);
mf=mmf;
}
public void actionPerformed(ActionEvent e){
if (学号.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入学号", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else if (姓名.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入姓名", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else if (年龄.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入年龄", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else{
String sql="select * from 学生信息表 where 学号='" + 学号.getText() + "'";
myConnection conn=new myConnection();
ResultSet rs;
rs=conn.getResult(sql);
try{
//System.out.println(rs.getRow());
if (rs.next()){
JOptionPane.showMessageDialog(null, "此学号已经存在", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else{
sql="insert into 学生信息表 values('" +学号.getText()+"','"+ 姓名.getText() +"','"+ 性别.getText() +"','"+ 民族.getText() +"',"+ 年龄.getText() +",'"+ 年级.getText() +"','"+ 班级.getText() +"','"+ 专业.getText() +"')";
if (conn.executeSql(sql)){
JOptionPane.showMessageDialog(null, "添加成功", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
mf.freshTable("select * from 学生信息表");
学号.setText("");
姓名.setText("");
性别.setText("");
民族.setText("");
年龄.setText("");
年级.setText("");
班级.setText("");
专业.setText("");
}else{
JOptionPane.showMessageDialog(null, "添加失败", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}catch(Exception er){
System.out.println(er.toString());
}
}
}
}
class editFrame extends JDialog implements ActionListener{
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
JLabel 学号1,姓名1,性别1,民族1,年龄1,年级1,班级1,专业1;
JTextField 学号,姓名,性别,民族,年龄,年级,班级,专业;
JButton b;
JPanel p;
mainFrame mf;
public editFrame(mainFrame mmf){
setTitle("修改学生信息");
setSize(WIDTH,HEIGHT);
setLocation(120,180);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
学号1=new JLabel("学号");
姓名1=new JLabel("姓名");
性别1=new JLabel("性别");
民族1=new JLabel("民族");
年龄1=new JLabel("年龄");
年级1=new JLabel("年级");
班级1=new JLabel("班级");
专业1=new JLabel("专业");
学号=new JTextField(5);
姓名=new JTextField(5);
性别=new JTextField(5);
民族=new JTextField(5);
年龄=new JTextField(5);
年级=new JTextField(5);
班级=new JTextField(10);
专业=new JTextField(10);
学号.setEnabled(false);
b=new JButton("确定");
p=new JPanel();
p.setLayout(new GridLayout(10,2,5,5));
p.add(学号1);
p.add(学号);
p.add(姓名1);
p.add(姓名);
p.add(性别1);
p.add(性别);
p.add(民族1);
p.add(民族);
p.add(年龄1);
p.add(年龄);
p.add(年级1);
p.add(年级);
p.add(班级1);
p.add(班级);
p.add(专业1);
p.add(专业);
p.add(new Label(""));
p.add(new Label(""));
p.add(b);
contentPane.add(p);
//添加按钮监听器
b.addActionListener(this);
mf=mmf;
}
public void actionPerformed(ActionEvent e){
if (学号.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入学号", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else if (姓名.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入姓名", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else if (年龄.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入年龄", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else{
String sql="update 学生信息表 set 姓名='"+姓名.getText()+"',性别='"+性别.getText()+"',年龄="+年龄.getText()+",年级='"+年级.getText()+"',班级='"+班级.getText()+"',专业='"+专业.getText()+"',民族='"+民族.getText()+"' where 学号='" + 学号.getText() + "'";
myConnection conn=new myConnection();
try{
//JOptionPane.showMessageDialog(null, sql, "温馨提示", JOptionPane.INFORMATION_MESSAGE);
if (conn.executeSql(sql)){
JOptionPane.showMessageDialog(null, "修改成功", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
mf.freshTable("select * from 学生信息表");
this.dispose();
}else{
JOptionPane.showMessageDialog(null, "修改失败", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}
}catch(Exception er){
System.out.println(er.toString());
}
}
}
}
class findFrame extends JDialog implements ActionListener{
mainFrame mf;
JPanel p;
JComboBox c;
JTextField t;
JButton b;
JButton fAll;
String sql="select * from student";
String[] colStr={"学号","姓名","性别","民族","年龄","年级","班级","专业"};
public findFrame(mainFrame mmf){
mf=mmf;
p=new JPanel();
c=new JComboBox(colStr);
t=new JTextField(10);
b=new JButton("查找");
fAll=new JButton("全部显示");
b.addActionListener(this);
fAll.addActionListener(this);
p.add(new JLabel("选择"));
p.add(c);
p.add(new JLabel("查找内容"));
p.add(t);
p.add(b);
p.add(fAll);
this.add(p);
this.setTitle("查找");
this.setSize(450,80);
}
public void actionPerformed(ActionEvent e){
//查找
if (e.getSource()==b){
String selectStr=c.getSelectedItem().toString();
if (selectStr=="年龄"){
sql="select * from 学生信息表 where "+selectStr+" = "+t.getText().toString();
}
else {
sql="select * from 学生信息表 where "+selectStr+" = '"+t.getText().toString()+"'";
}
mf.freshTable(sql);
//全部显示
}if (e.getSource()==fAll){
sql="select * from 学生信息表";
mf.freshTable(sql);
}
}
}
class aboutFrame extends JDialog{
aboutFrame(){
JPanel p1=new JPanel();
p1.add(new JLabel("学生信息管理系统"));
p1.add(new JLabel("版权所有:农福刚"));
p1.add(new JLabel("农福刚有限责任公司出口"));
p1.add(new JLabel("QQ:14159391"));
p1.setLayout(new GridLayout(4,1,5,5));
this.add(p1);
this.setTitle("关于我");
this.setSize(300,200);
}
}
class userFrame extends JFrame implements ActionListener{
JTextField user,pass;
JButton add,del;
JTable t;
JPanel p1,p2,p3,p4,p5;
DefaultTableModel m;
public userFrame(){
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
user=new JTextField(8);
pass=new JTextField(8);
add=new JButton("添加");
del=new JButton("删除");
String[] col = {"用户名","密码"};
m= new DefaultTableModel(col,0);
t=new JTable();
t.setModel(m);
JScrollPane sp = new JScrollPane(t);
p1.add(new JLabel("用户名"));
p1.add(user);
p1.add(new JLabel("密码"));
p1.add(pass);
p1.add(add);
p2.add(sp);
p3.add(del);
add.addActionListener(this);
del.addActionListener(this);
myConnection conn=new myConnection();
ResultSet rs;
rs=conn.getResult("select * from 管理员");
if (rs!=null){
try{
//m.setRowCount(0);
//table.setModel(mm);
while(rs.next()){
String 用户名 = rs.getString("用户名");
String 密码 = rs.getString("密码");
String[] cloumns ={用户名,密码};
m.addRow(cloumns);
}
t.setModel(m);
}catch(Exception e){
System.out.println(e.toString());
}
}
this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);
this.add(p3,BorderLayout.SOUTH);
this.add(p4,BorderLayout.WEST);
this.add(p5,BorderLayout.EAST);
this.setTitle("用户管理");
this.setSize(600,400);
}
public void actionPerformed(ActionEvent e){
//添加
if (e.getSource()==add){
if (user.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入用户名", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else if (pass.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入密码", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else{
myConnection conn=new myConnection();
ResultSet rs;
try{
rs=conn.getResult("select * from 管理员 where 用户名='"+user.getText().toString()+"'");
if (rs.next()){
JOptionPane.showMessageDialog(null, "此用户已经存在", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else{
if (conn.executeSql("insert into 管理员 values('"+user.getText().toString()+"','"+pass.getText().toString()+"')")){
String[] newUser={user.getText(),pass.getText()};
m.addRow(newUser);
t.setModel(m);
JOptionPane.showMessageDialog(null, "添加成功", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "添加失败", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}catch(Exception er){
System.out.println(er.toString());
}
}
//删除
}else if (e.getSource()==del){
if (t.getSelectedRow()==-1){
JOptionPane.showMessageDialog(null, "请选择你要删除的行", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else {
String sql="delete from 管理员 where 用户名 = '"+t.getValueAt(t.getSelectedRow(),0)+"'";
//JOptionPane.showMessageDialog(null, sql, "温馨提示", JOptionPane.INFORMATION_MESSAGE);
myConnection conn=new myConnection();
if (conn.executeSql(sql)){
m.removeRow(t.getSelectedRow());
t.setModel(m);
//t.removeRowSelectionInterval();
JOptionPane.showMessageDialog(null, "成功删除", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(null, "未知错误", "删除失败", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
class loginFrame extends JDialog implements ActionListener{
JPanel p;
JTextField user,pass;
JButton login,cancel;
public loginFrame(){
p=new JPanel();
user=new JTextField(10);
pass=new JTextField(10);
login=new JButton("登录");
cancel=new JButton("退出");
user.setText("admin");
pass.setText("admin");
login.addActionListener(this);
cancel.addActionListener(this);
p.add(new JLabel("账号"));
p.add(user);
p.add(new JLabel("密码"));
p.add(pass);
p.add(login);
p.add(cancel);
this.add(p);
this.setTitle("系统登录");
this.setSize(180,130);
}
public void actionPerformed(ActionEvent e){
//查找
if (e.getSource()==login){
if (user.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入用户名", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else if (pass.getText().toString().equals("")){
JOptionPane.showMessageDialog(null, "请输入密码", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
}else{
myConnection conn=new myConnection();
ResultSet rs;
String sql="select * from 管理员 where 用户名 = '"+user.getText().toString()+"' and 密码 = '"+pass.getText().toString()+"'";
try{
rs=conn.getResult(sql);
if (rs.next()){
this.dispose();
//JOptionPane.showMessageDialog(null, "此用户已经存在", "温馨提示", JOptionPane.INFORMATION_MESSAGE);
sql="select * from 学生信息表";
mainFrame mf=new mainFrame();
mf.setTitle("农福刚学生信息管理系统");
mf.setSize(600,486);
mf.freshTable(sql);
mf.setVisible(true);
mf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}else{
JOptionPane.showMessageDialog(null, "用户名或密码错误", "登录失败", JOptionPane.INFORMATION_MESSAGE);
}
}catch(Exception er){
System.out.println(er.toString());
}
}
//全部显示
}if (e.getSource()==cancel){
System.exit(0);
}
}
}