- 论坛徽章:
- 0
|
![]()
文件:stu.rar
大小:35KB
下载:
下载
/**
* 作者:flywing0927 sdau
* email:yongqings@126.com
*/
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class StFrame extends JFrame implements ActionListener,MouseListener {
JTable table;
JLabel lable[]=new JLabel[8];
JTextField text[]=new JTextField[8];
String sl[]={"学号","姓名","性别","出生日期","数学","英语","计算机","总分","名次"};
JButton jb_show,jb_add,jb_del,jb_update,jb_serch,jb_sort;
JTextField jtf_cxtj; //输入查询内容
JComboBox jcb_cxtj; //选择查询条件
JRadioButton jrb1; //性别选择
JRadioButton jrb2;
JPanel p1,p2,p;
Object body[][]=new Object[50][9];
JTabbedPane tp;
int row;
Connection con;
Statement stat,stat1;
ResultSet rs;
String cxtj;
public StFrame(){ //构造函数,窗口设置
super("学生信息管理");
this.setSize(850,600);
this.setLocation(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table=new JTable(body,sl);
tp=new JTabbedPane();
tp.add("学生信息表",new JScrollPane(table));
this.getContentPane().add(tp,"Center");
p1=new JPanel();
p=new JPanel();
jrb1=new JRadioButton("男",true);
jrb2=new JRadioButton("女");
ButtonGroup bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
p1.setLayout(new GridLayout(8,3));
for(int i=0;i<7;i++){
lable=new JLabel(sl);
p1.add(lable);
if(sl=="性别"){
p.add(jrb1);
p.add(jrb2);
p1.add(p);
}
else{
text=new JTextField();
p1.add(text);
}
}
text[4].setText("0");
text[5].setText("0");
text[6].setText("0");
this.getContentPane().add(p1,"East");
p2=new JPanel();
p2.setLayout(new GridLayout(1,9));
jb_show=new JButton("显示");
jb_add=new JButton("添加");
jb_del=new JButton("删除");
jb_update=new JButton("修改");
jb_serch=new JButton("查询");
jb_sort=new JButton("排序");
p2.add(jb_show);
p2.add(jb_add);
p2.add(jb_del);
p2.add(jb_update);
p2.add(jb_sort);
JLabel jl_cxtj=new JLabel("请选择查询条件");
jcb_cxtj=new JComboBox(sl); //查询条件选择框
jtf_cxtj=new JTextField();
p2.add(jl_cxtj);
p2.add(jcb_cxtj);
p2.add(jtf_cxtj);
p2.add(jb_serch);
////////添加事件监听器
table.addMouseListener(this);
jcb_cxtj.addActionListener(this);
jb_show.addActionListener(this);
jb_add.addActionListener(this);
jb_del.addActionListener(this);
jb_update.addActionListener(this);
jb_serch.addActionListener(this);
jb_sort.addActionListener(this);
this.getContentPane().add(p2,"South");
this.setVisible(true);
this.setResizable(false);
//this.conDB();
}
public void conDB(){ //连接数据库
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:student";
con=DriverManager.getConnection(url);
stat=con.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public void show1(){ //初始化table,显示已有数据并计算总分
for(int i=0;i<body.length;i++){
for(int j=0;j<body.length;j++){
body[j]=null;
}
}
int m=0;
try {
stat1=con.createStatement();
rs=stat.executeQuery("select * from student");
int s[][]=new int[50][3];
while(rs.next()){
body[m][0]=rs.getString(1);
body[m][1]=rs.getString(2);
body[m][2]=rs.getString(3);
body[m][3]=rs.getString(4);
s[m][0]=rs.getInt(5);
s[m][1]=rs.getInt(6);
s[m][2]=rs.getInt(7);
body[m][4]=s[m][0];
body[m][5]=s[m][1];
body[m][6]=s[m][2];
int sum=s[m][0]+s[m][1]+s[m][2];
stat1.executeUpdate("update student set sum='"+sum+"' where sno='"+body[m][0]+"'");
body[m][7]=rs.getInt(8);
m++;
}
table.repaint();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void add(){ //添加记录
int math=0;
int english=0;
int computer=0;
String sno=new String(text[0].getText());
String name=new String(text[1].getText());
String sex=null;
if(jrb1.isSelected())
sex="男";
if(jrb2.isSelected())
sex="女";
String birthday=new String(text[3].getText());
math=Integer.parseInt(text[4].getText());
english=Integer.parseInt(text[5].getText());
computer=Integer.parseInt(text[6].getText());
try {
stat.executeUpdate("insert into student (sno,name,sex,birthday,math,english,computer) values('"
+sno+"','"+name+"','"+sex+"','"+birthday+"','"+math+"','"+english+"','"+computer+"')");
show1();
JOptionPane.showMessageDialog(null, "记录添加成功!");
text[0].setText("");
text[1].setText("");
text[3].setText("");
text[4].setText("0");
text[5].setText("0");
text[6].setText("0");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "记录添加失败!");
}
}
public void del(){ //删除记录
int row=table.getSelectedRow();
try {
stat.executeUpdate("delete from student where sno='"+body[row][0]+"'");
JOptionPane.showMessageDialog(null, "记录删除成功!");
show1();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(){ // 修改记录
String sno=new String(text[0].getText());
String name=new String(text[1].getText());
String sex=null;
if(jrb1.isSelected())
sex="男";
if(jrb2.isSelected())
sex="女";
String birthday=new String(text[3].getText());
int math=Integer.parseInt(text[4].getText());
int english=Integer.parseInt(text[5].getText());
int computer=Integer.parseInt(text[6].getText());
try {
stat.executeUpdate("update student set sno='"+sno+"',name='"+name+"',sex='"+
sex+"',birthday='"+birthday+"',math='"+math+"',english='"
+english+"',computer='"+computer+"' where sno='"+body[row][0]+"'");
JOptionPane.showMessageDialog(null, "记录修改成功!");
} catch ( SQLException e) {
e.printStackTrace();
}
show1();
}
public void serch(){ //查询记录
for(int i=0;i<body.length;i++){
for(int j=0;j<body.length;j++){
body[j]=null;
}
}
String sql=null;
String cxnr=new String(jtf_cxtj.getText());
if (cxnr.length()==0){
JOptionPane.showMessageDialog(null, "请输入查询条件!");
}
else{
int cxx=jcb_cxtj.getSelectedIndex();
cxtj=null;
int cxnr1;
switch(cxx){
case 0:cxtj="sno";
sql="select * from student where "+cxtj+"='"+cxnr+"'";
break;
case 1:cxtj="name";
sql="select * from student where "+cxtj+"='"+cxnr+"'";
break;
case 2:cxtj="sex";
sql="select * from student where "+cxtj+"='"+cxnr+"'";
break;
case 3:cxtj="birthday";
sql="select * from student where "+cxtj+"='"+cxnr+"'";
break;
case 4:cxtj="math";
cxnr1=Integer.parseInt(jtf_cxtj.getText());
sql="select * from student where "+cxtj+"="+cxnr1;
break;
case 5:cxtj="english";
cxnr1=Integer.parseInt(jtf_cxtj.getText());
sql="select * from student where "+cxtj+"="+cxnr1;
break;
case 6:cxtj="computer";
cxnr1=Integer.parseInt(jtf_cxtj.getText());
sql="select * from student where "+cxtj+"="+cxnr1;
break;
case 7:cxtj="sum";
cxnr1=Integer.parseInt(jtf_cxtj.getText());
sql="select * from student where "+cxtj+"="+cxnr1;
break;
}
//System.out.print(sql);
int m=0;
try {
rs=stat.executeQuery(sql);
while(rs.next()){
body[m][0]=rs.getString(1);
body[m][1]=rs.getString(2);
body[m][2]=rs.getString(3);
body[m][3]=rs.getString(4);
body[m][4]=rs.getInt(5);
body[m][5]=rs.getInt(6);
body[m][6]=rs.getInt(7);
body[m][7]=rs.getInt(8);
m++;
}
table.repaint();
} catch (Exception e) {
}
}
}
public void sort(){
int m=0;
try {
rs=stat.executeQuery("SELECT * FROM student ORDER BY sum DESC");
while(rs.next()){
body[m][0]=rs.getString(1);
body[m][1]=rs.getString(2);
body[m][2]=rs.getString(3);
body[m][3]=rs.getString(4);
body[m][4]=rs.getInt(5);
body[m][5]=rs.getInt(6);
body[m][6]=rs.getInt(7);
body[m][7]=rs.getInt(8);
body[m][8]=m+1;
m++;
}
table.repaint();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb_show){
conDB();
show1();
}
if(e.getSource()==jb_add){
conDB();
add();
}
if(e.getSource()==jb_del){
conDB();
int a=JOptionPane.showConfirmDialog(null, "真的要删除?","提示",JOptionPane.YES_NO_OPTION);
if(a==0)
del();
}
if(e.getSource()==jb_serch){
conDB();
serch();
}
if(e.getSource()==jb_update){
conDB();
update();
}
if(e.getSource()==jb_sort){
conDB();
sort();
}
}
public void mouseClicked(MouseEvent e) { //单击table中记录使其显示在右侧文本栏内
// TODO Auto-generated method stub
try{
row=table.getSelectedRow();
text[0].setText((String) body[row][0]);
text[1].setText((String) body[row][1]);
String s=(String) body[row][2];
if(s.equals("男")){
jrb1.setSelected(true);
}
else{
jrb2.setSelected(true);
}
text[3].setText((String) body[row][3]);
text[4].setText(body[row][4].toString());
text[5].setText(body[row][5].toString());
text[6].setText(body[row][6].toString());
}catch (final Exception e1) {
text[3].setText("");
text[4].setText("0");
text[5].setText("0");
text[6].setText("0");
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public static void main(String[] args) {
new StFrame();
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/68060/showart_1722233.html |
|