/*
* 实现将sql server数据库表中的内容显示在JTable中
* 做一个mini版的学生管理系统
*/
package com.test;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Dome4 extends JFrame implements ActionListener{
//面板
JPanel panel1=null;
JPanel panel2=null;
//按钮
JButton button_select=null;
JButton button_back=null;
JButton button_add=null;
JButton button_alter=null;
JButton button_delete=null;
JTextField jtf=null;
JLabel label=null;
JTable table=null;
JScrollPane scrollPane=null;
MyTableModel tablemodel=null;
String sql=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Dome4 dome=new Dome4();
}
public Dome4(){
label=new JLabel("请输入部门名");
jtf=new JTextField(10);
button_select=new JButton("查询");
button_select.addActionListener(this);
button_select.setActionCommand("select");
button_back=new JButton("返回");
button_back.addActionListener(this);
button_back.setActionCommand("back");
panel1=new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(label);
panel1.add(jtf);
panel1.add(button_select);
panel1.add(button_back);
button_add=new JButton("添加");
button_add.addActionListener(this);
button_add.setActionCommand("add");
button_alter=new JButton("修改");
button_alter.addActionListener(this);
button_alter.setActionCommand("alter");
button_delete=new JButton("删除");
button_delete.addActionListener(this);
button_delete.setActionCommand("delete");
panel2=new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(button_add);
panel2.add(button_alter);
panel2.add(button_delete);
MyTableModel tablemodel=new MyTableModel();
table=new JTable(tablemodel);
scrollPane=new JScrollPane(table);
this.add(panel1,"North");
this.add(scrollPane);
this.add(panel2,"South");
this.setTitle("学生管理系统");
this.setSize(400,300);
this.setVisible(true);
this.setResizable(false);
this.setLocation(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("select")){
System.out.println("jinlai");
String name=this.jtf.getText().trim();
sql="select * from dpt where dname='"+name+"'";
tablemodel=new MyTableModel(sql);
table.setModel(tablemodel);
}else if(e.getActionCommand().equals("back")){
sql="select * from dpt";
tablemodel=new MyTableModel(sql);
table.setModel(tablemodel);
}else if(e.getActionCommand().equals("add")){
AddDialog dialog=new AddDialog(this, "添加学生", true);
tablemodel=new MyTableModel();
tablemodel.UpdateTable(dialog.sql, dialog.command);
tablemodel=new MyTableModel();
table.setModel(tablemodel);
}else if(e.getActionCommand().equals("alter")){
int j=this.table.getSelectedRow();
if(j==-1){
JOptionPane.showMessageDialog(this, "请选择一行要修改的信息");
}else{
AlterDialog dialog=new AlterDialog(this, "修改学生", true,this.table);
tablemodel=new MyTableModel();
tablemodel.UpdateTable(dialog.sql, dialog.command);
tablemodel=new MyTableModel();
table.setModel(tablemodel);
}
}else if(e.getActionCommand().equals("delete")){
sql="delete from dpt where dno=?";
int j=this.table.getSelectedRow();
if(j==-1) {
JOptionPane.showMessageDialog(this, "请选择一行");
}else{
String[] command={this.table.getValueAt(j, 0).toString()};
tablemodel=new MyTableModel();
tablemodel.UpdateTable(sql, command);
tablemodel=new MyTableModel();
table.setModel(tablemodel);
}
}
}
}
class AlterDialog extends JDialog implements ActionListener{
JLabel label_dno=null;
JLabel label_dname=null;
JLabel label_dloc=null;
JTextField jtf_dno=null;
JTextField jtf_dname=null;
JTextField jtf_dloc=null;
JButton jb_ok=null;
JButton jb_cancle=null;
JPanel panel1=null;
JPanel panel2=null;
JPanel panel3=null;
JPanel panel4=null;
String sql=null;
String[] command=null;
public AlterDialog(Frame frame,String title,boolean model, JTable table){
super(frame,title,model);
int j=table.getSelectedRow();
label_dno=new JLabel("部门号");
jtf_dno=new JTextField(table.getValueAt(j, 0).toString(),10);
jtf_dno.setEditable(false);
panel1=new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(label_dno);
panel1.add(jtf_dno);
label_dname=new JLabel("部门名");
jtf_dname=new JTextField(table.getValueAt(j, 1).toString(),10);
panel2=new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(label_dname);
panel2.add(jtf_dname);
label_dloc=new JLabel("地 区");
jtf_dloc=new JTextField(table.getValueAt(j, 2).toString(),10);
panel3=new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(label_dloc);
panel3.add(jtf_dloc);
jb_ok=new JButton("确定");
jb_ok.addActionListener(this);
jb_ok.setActionCommand("ok");
jb_cancle=new JButton("取消");
jb_cancle.addActionListener(this);
jb_cancle.setActionCommand("cancle");
panel4=new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(jb_ok);
panel4.add(jb_cancle);
this.setLayout(new GridLayout(4,1));
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.add(panel4);
this.setSize(200,300);
this.setLocation(800,300);
this.setResizable(false);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("ok")){
sql="update dpt set dname=?,dloc=? where dno='"+this.jtf_dno.getText()+"'";
command=new String[]{this.jtf_dname.getText(),this.jtf_dloc.getText()};
this.dispose();
}else if(e.getActionCommand().equals("cancle")){
this.dispose();
}
}
}
class AddDialog extends JDialog implements ActionListener{
JLabel label_dno=null;
JLabel label_dname=null;
JLabel label_dloc=null;
JTextField jtf_dno=null;
JTextField jtf_dname=null;
JTextField jtf_dloc=null;
JButton jb_ok=null;
JButton jb_cancle=null;
JPanel panel1=null;
JPanel panel2=null;
JPanel panel3=null;
JPanel panel4=null;
String sql=null;
String[] command=null;
public AddDialog(Frame frame,String title,boolean model){
super(frame,title,model);
label_dno=new JLabel("部门号");
jtf_dno=new JTextField(10);
panel1=new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(label_dno);
panel1.add(jtf_dno);
label_dname=new JLabel("部门名");
jtf_dname=new JTextField(10);
panel2=new JPanel();
panel2.setLayout(new FlowLayout());
panel2.add(label_dname);
panel2.add(jtf_dname);
label_dloc=new JLabel("地 区");
jtf_dloc=new JTextField(10);
panel3=new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(label_dloc);
panel3.add(jtf_dloc);
jb_ok=new JButton("确定");
jb_ok.addActionListener(this);
jb_ok.setActionCommand("ok");
jb_cancle=new JButton("取消");
jb_cancle.addActionListener(this);
jb_cancle.setActionCommand("cancle");
panel4=new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(jb_ok);
panel4.add(jb_cancle);
this.setLayout(new GridLayout(4,1));
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.add(panel4);
this.setSize(200,300);
this.setLocation(800,300);
this.setResizable(false);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("ok")){
sql="insert into dpt