正文

数据库课程设计2006-06-23 16:18:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/16130.html

分享到:

/*数据库课程设计
 *JAVA程序处理前端连接等操作
 *使用:
 *1,打开数据库管理器,打开查询编辑器,在查询编辑器输入附加SQL语言,创建将要使用的目标数据库
 *2,进行JDBC-ODBC连接
 *过程:打开管理工具,选择数据源ODBC,在系统DSN添加:名称:studyDSN,连接到(local),选择网络ID选项,默认数据库为study,连接!
 *3,使用JCreator之类的工具编译程序.
 *4,用户名:admin 密码:123456
 *@2006.6.23                                         */


 /*附加SQL语言

create database study
go
use study
go
create table student(sno char(5) not null UNIQUE,
   sname char(10),
   ssex char(2),
   sage int,
   sdept char(20)
   PRIMARY KEY(sno))
   
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.border.*;
import java.util.*;

public class Allmain{
//---------------------------------------------------------变量定义
//swing
JLabel conLabel,addLabel,modLabel,droLabel,aboLabel,finLabel,namLabel,pasLabel;
JButton conButton,addButton,modButton,droButton,finButton,okButton,canButton;
JTextField conField,namField;
JPasswordField pasField;
JFrame mainFrame,passFrame;
//string
static final String JDBC="sun.jdbc.odbc.JdbcOdbcDriver";
static final String DATEBASE="jdbc:odbc:studyDSN";
//数据库API变量
public Connection connection;
public Statement statement;
String sqlString;
//_____________________________________________________________________
 Allmain(){
  mainFrame=new JFrame("数据库课程设计");
  JPanel mainPanel=new JPanel();
  mainPanel.setBackground(new Color(255,192,192));
  mainPanel.setLayout(new GridLayout(6,2,5,5));
//------------------------------------------------------外观值
aboLabel=new JLabel("");

conLabel=new JLabel("数据库名称");
addLabel=new JLabel("添加数据");
modLabel=new JLabel("修改数据");
droLabel=new JLabel("删除数据");
finLabel=new JLabel("查询数据");
conButton=new JButton("连接");
conButton.setBackground(new Color(50,192,192));
conButton.setFont(new Font("SansSerif",Font.PLAIN,18));
addButton=new JButton("添加");
addButton.setBackground(new Color(50,192,192));
addButton.setFont(new Font("SansSerif",Font.PLAIN,18));
modButton=new JButton("修改");
modButton.setBackground(new Color(50,192,192));
modButton.setFont(new Font("SansSerif",Font.PLAIN,18));
droButton=new JButton("删除");
droButton.setBackground(new Color(50,192,192));
droButton.setFont(new Font("SansSerif",Font.PLAIN,18));
finButton=new JButton("查询");
finButton.setBackground(new Color(50,192,192));
finButton.setFont(new Font("SansSerif",Font.PLAIN,18));
conField=new JTextField("暂时无用");
//_______________________________________________________________________ 
  mainPanel.add(conLabel);
  mainPanel.add(conField);
  mainPanel.add(aboLabel);
  mainPanel.add(conButton);
 
  mainPanel.add(addLabel);
  mainPanel.add(addButton);
  mainPanel.add(modLabel);
  mainPanel.add(modButton);
  mainPanel.add(droLabel);
  mainPanel.add(droButton);
  mainPanel.add(finLabel);
  mainPanel.add(finButton);
 mainFrame.getContentPane().add(mainPanel);
 mainFrame.setBounds(200,200,200,200);
 mainFrame.setVisible(false);
 pass();
//------------------------------------------------------------------添加进监听
conButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
 con(); }});
addButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
 add(); }});
modButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
 mod(); }});
droButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
 dro(); }});
finButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
 fin(); }});
 mainFrame.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent event)
   {
    terminate();
   }
  });
//____________________________________________________________________________   
 }
//--------------------------------------------------------------------方法equils
void pass(){
 passFrame=new JFrame("---------登陆---------");
 namLabel=new JLabel("管理人员:");
 pasLabel=new JLabel("  密码:     ");
 namField=new JTextField(10);
 pasField=new JPasswordField(10);
 okButton=new JButton("确定");
//
 okButton.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     if( (namField.getText().equals("admin") ) && (pasField.getText().equals("123456") ) )
   { mainFrame.show();
    passFrame.dispose();
   }
       else
   JOptionPane.showMessageDialog(null,"用户名或者密码有误","错误",JOptionPane.ERROR_MESSAGE);
   }});
 canButton=new JButton("取消");
   canButton.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     namField.setText("");
     pasField.setText("");
    }
   }); 
// 
 passFrame.getContentPane().setLayout(new FlowLayout());
 passFrame.getContentPane().add(namLabel);
 passFrame.getContentPane().add(namField);
 passFrame.getContentPane().add(pasLabel);
 passFrame.getContentPane().add(pasField);
 passFrame.getContentPane().add(okButton);
 passFrame.getContentPane().add(canButton);
 passFrame.setBounds(200,200,200,130);
 passFrame.setVisible(true);
 mainFrame.dispose();
}
void con(){
 initize(); }
void add(){//添加
new AddStudentFrame();mainFrame.dispose();
}
void mod(){//修改
new UpdateStudentFrame();mainFrame.dispose();
}
void dro(){//删除
new DeleteStudentFrame();mainFrame.dispose();
}
void fin(){//查询
 new findClass();
//new DisplayStudents();
}
void terminate()
   {
      //关闭
      try {
            statement.close();
            connection.close();           
         }
    catch ( SQLException sqlException ) {
            JOptionPane.showMessageDialog( null,
               sqlException.getMessage(), "Database Error",
               JOptionPane.ERROR_MESSAGE );
      System.exit( 1 );
         }}
void initize(){//初始化建立连接
try{
 Class.forName(JDBC);
 connection=DriverManager.getConnection(DATEBASE,"sa","");//注意:可改变
 statement=connection.createStatement();
  
}
catch(SQLException sqlException){
   JOptionPane.showMessageDialog(null,sqlException.getMessage(),"Database error",JOptionPane.ERROR_MESSAGE);
   System.exit(1);
  }
  catch(ClassNotFoundException classNotFound){
   JOptionPane.showMessageDialog(null,classNotFound.getMessage(),"driver not found",JOptionPane.ERROR_MESSAGE);
   System.exit(1);
  }
catch(Exception e){
 System.out.print(e.toString());
}
}
//_____________________________________________________________________________
//----------------------------------------------------------------------------------------main
 public static void main(String args[]){
  new Allmain(); 
 }
//___________________________________________________________________________________________________
//-------------------------------------------------------------AddStudentFrame.class
class AddStudentFrame extends JFrame{
 private StudentUI userInterface;
 private JButton clearButton,writeButton,backButton;
 
 String names[]={"学号","姓名","性别","年龄","所在系"
 };
 public AddStudentFrame(){
  super("添加一个学生记录");
  userInterface=new StudentUI(names);
  getContentPane().add(userInterface,BorderLayout.CENTER);
//-------------------------------------------------------------------保存也就是添加
  writeButton=userInterface.getDotask1Button();
  writeButton.setText("保存");
 
  writeButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent event){
    addRecord();
   }
  });
//______________________________________________________________________________________________
 //返回
 backButton=userInterface.getDotask3Button();
 backButton.setText("返回");
 backButton.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     mainFrame.show();
     dispose();
    }
   });
//-----------------------------------------------------------------------清除
  clearButton=userInterface.getDoTask2Button();
  clearButton.setText("清除");
 
  clearButton.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     userInterface.clearFields();
    }
   });
    addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent event)
   {
    terminate();
   }
  });
//_________________________________________________________________________________
   setBounds(200,200,300,200);
  setVisible(true);
 }
//----------------------------------------------------------------------------方法
//addRecord()
 public void addRecord(){
  String fieldValues[]=userInterface.getFieldValues();
  if(!fieldValues[StudentUI.SNO].equals("")){
   try{
    int numberAge=Integer.parseInt(fieldValues[StudentUI.SAGE]);
   
      String sqlInsert = "INSERT INTO student " +
         "VALUES ('" +
        fieldValues[0] + "', '" +
        fieldValues[1]  +"', '"+
        fieldValues[2]+ "', "
        +numberAge+",'"+fieldValues[4] + "')";
    int result=statement.executeUpdate(sqlInsert);
    if(result!=0){
     userInterface.clearFields();
     JOptionPane.showMessageDialog(this,"添加成功","添加",JOptionPane.INFORMATION_MESSAGE);
    }
   
   }
   catch(NumberFormatException formatException){
    JOptionPane.showMessageDialog(this,"年龄有错误","invalid numver format",JOptionPane.ERROR_MESSAGE);
   }
   catch(SQLException ee)
    {System.out.println(ee);
    }
   }
   else JOptionPane.showMessageDialog(this,"学号有误","invalid number format",JOptionPane.ERROR_MESSAGE);
  }

 //_____________________________________________________________________________________________________ 
  }
//______________________________________________________________________________________________
//--------------------------------------------------------------------------StudentUI
public class StudentUI extends JPanel{
 protected JLabel labels[];
 protected JTextField fields[];
 protected JButton doTask1,doTask2,doTask3;
 protected JPanel innerPanelCenter,innerPanelSouth;
 protected int size;
 
 public static final int SNO=0,SNAME=1,SSEX=2,SAGE=3,SDEPT=4;
 
 public StudentUI(String arrayString[]){
  size=arrayString.length;
  labels=new JLabel[size];
  fields=new JTextField[size];
 
  for(int count=0;count<labels.length;count++)
  labels[count]=new JLabel(arrayString[count]);
 
  for(int count=0;count<fields.length;count++)
  fields[count]=new JTextField();
 
  innerPanelCenter=new JPanel();
  innerPanelCenter.setLayout(new GridLayout(size,2));
 
  for(int count=0;count<size;count++){
   innerPanelCenter.add(labels[count]);
   innerPanelCenter.add(fields[count]);
  }
 
  doTask1=new JButton();doTask2=new JButton();doTask3=new JButton();
  innerPanelSouth=new JPanel();
  innerPanelSouth.add(doTask1);
  innerPanelSouth.add(doTask2);
  innerPanelSouth.add(doTask3);
 
  setLayout(new BorderLayout());
  add(innerPanelCenter,BorderLayout.CENTER);
  add(innerPanelSouth,BorderLayout.SOUTH);
  setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
  validate();
 }
 
 public JButton getDotask1Button(){
  return doTask1;
 }
 public JButton getDoTask2Button(){
  return doTask2;
 }
 
 public JButton getDoTask1Button(){
   return doTask1;
 }
 
 public JButton getDotask3Button(){
  return doTask3;
 }
 
 public JTextField[] getFields(){
  return fields;
 }
 public void clearFields(){
  for(int count=0;count<size;count++)
  fields[count].setText("");
 }
 
 public void setFieldValues(String strings[])
 throws IllegalArgumentException
 {if(strings.length!=size)throw new IllegalArgumentException("there must be"+size+"strings in the array");
 for(int count=0;count<size;count++)
  fields[count].setText(strings[count]);
 }
 public String[] getFieldValues()
 {
  String values[]=new String[size];
  for(int count=0;count<size;count++)
  values[count]=fields[count].getText();
  return values;
 }
}
//_________________________________________________________________________________
//---------------------------------------------------UpdateStudentFrame修改
class UpdateStudentFrame extends JFrame {
   private StudentUI userInterface1,userInterface2;
   private JButton  firstButton1,secondButton1, firstButton2,secondButton2,backButton1,backButton2;
   String snoUpdate;

   String sqlString ;
      // set up GUI
   public UpdateStudentFrame()
   {
      super( "Update a record of students" );
  
      String names1[] = { "请输入要修改的学生的学号:"};
           
      userInterface1= new StudentUI( names1 );  // four textfields
     
      //设置显示要修改的记录画面
      String names2[] = { "学  号","姓  名","性 别","年 龄","所 在 系"};
      userInterface2 = new StudentUI(names2 );
      Container c= getContentPane();
      Box box = Box.createVerticalBox();
      box.add(userInterface1  );
      box.add(userInterface2 );
     
      c.add(box);

      firstButton1 = userInterface1.getDoTask1Button();
      firstButton1.setText( "确认" );

      firstButton1.addActionListener(
         new ActionListener() {

            // call DisplayRecord() when firstButton1 pressed
            public void actionPerformed( ActionEvent event )
            {
              
               DisplayRecord();
            }

         });
//清除按钮
      secondButton1 = userInterface1.getDoTask2Button();
      secondButton1.setText( "清除" );
      secondButton1.addActionListener(

         new ActionListener() {
            public void actionPerformed( ActionEvent event )
            {
               userInterface1.clearFields();
            }
        }
      );
secondButton2 = userInterface2.getDoTask2Button();
      secondButton2.setText( "清除" );
      secondButton2.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent event )
            {
               userInterface2.clearFields();
            }});   
 //返回
 backButton1=userInterface1.getDotask3Button();
 backButton1.setText("返回");
 backButton1.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     mainFrame.show();
     dispose();
    }
   });
 backButton2=userInterface2.getDotask3Button();
 backButton2.setText("返回");
 backButton2.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     mainFrame.show();
     dispose();
    }
   });     
//________________________________________________________________________     
      firstButton2 = userInterface2.getDoTask1Button();
      firstButton2.setText( "确认修改" );

      firstButton2.addActionListener(

         new ActionListener() {
            public void actionPerformed( ActionEvent event )
            {
               UpdateRecord();
            }

         });
       addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent event ){
            terminate(); } }
);
    setBounds(200,200,400,260);
      setVisible( true );

   }
  
   public void DisplayRecord()
   {
    
    String fieldValues1[] = userInterface1.getFieldValues();
    
    String fieldValues2 []=new String[5] ;
    if ( ! fieldValues1[ StudentUI.SNO ].equals( "" ) ) {
        
         snoUpdate=fieldValues1[0];
         try {
            int numberAge = Integer.parseInt(
            fieldValues1[ StudentUI.SNO ] );
            
            String sqlString = "select * from student "+
                             " where sno='"+fieldValues1[0] + "'";
                               
            ResultSet resultSet =statement.executeQuery( sqlString)  ;
            ResultSetMetaData metaData = resultSet.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
           
            if ( resultSet.next() ) {
                fieldValues2[0]=resultSet.getString( 1 ) ;
                fieldValues2[1]=resultSet.getString( 2 ) ;
                fieldValues2[2]=resultSet.getString( 3 ) ;
                fieldValues2[3]=String.valueOf(resultSet.getInt( 4 )) ;
                fieldValues2[4]=resultSet.getString( 5 ) ;
               
                userInterface2.setFieldValues(fieldValues2);
            }   
            else
            {
                userInterface2.clearFields();
                JOptionPane.showMessageDialog( this,
                  "没找到记录", "Find Result",
                    JOptionPane.INFORMATION_MESSAGE );
            }  }

         catch ( NumberFormatException formatException ) {
            JOptionPane.showMessageDialog( this,
               "年龄有错误", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
         }

         catch (SQLException ee)
  {   System.out.println(ee); }
   }
  
   else
            JOptionPane.showMessageDialog( this,
               "Bad sno number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
      
   }

   public void UpdateRecord()
   {
     
      String fieldValues[] = userInterface2.getFieldValues();

      if ( ! fieldValues[ StudentUI.SNO ].equals( "" ) ) {

         try {
            int numberAge = Integer.parseInt(
            fieldValues[ StudentUI.SAGE ] );
 
            String sqlString = "Update student set "+
                                "sno='"+fieldValues[0] + "',"+
        "sname='"+fieldValues[1] + "',"+
        "ssex='"+fieldValues[2]  +"', "+
        "sage="+numberAge+ ", "+
        "sdept='"+fieldValues[4]+
        "' where sno='"+snoUpdate + "'";
        
            int result = statement.executeUpdate(sqlString);
            if (result!=0) {
                JOptionPane.showMessageDialog( this,
                "修改成功!", "Update Result",
                 JOptionPane.INFORMATION_MESSAGE );
            }}

         catch ( NumberFormatException formatException ) {
            JOptionPane.showMessageDialog( this,
               "Bad age number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
         }
         catch (SQLException ee)
    { System.out.println(ee); }
  }
  else
            JOptionPane.showMessageDialog( this,
               "Bad sno number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE ); }
  
}
//______________________________________________________________________________
//-------------------------------------------------DeleteStudentFrame删除
class DeleteStudentFrame extends JFrame {
   private StudentUI userInterface1,userInterface2;
   private JButton  firstButton1,secondButton1, firstButton2,secondButton2,backButton1,backButton2;
   String snoUpdate;

   public DeleteStudentFrame()
   {
      super( "Delete records from students" );

      String names1[] = { "请输入要删除的学生的学号:"};
           
      userInterface1= new StudentUI( names1 );

      String names2[] = { "学  号","姓  名","性 别","年 龄","所 在 系"};
      userInterface2 = new StudentUI(names2 );
      Container c= getContentPane();
      Box box = Box.createVerticalBox();
      box.add(userInterface1  );
      box.add(userInterface2 );
     
      c.add(box);
     
      firstButton1 = userInterface1.getDoTask1Button();
      firstButton1.setText( "确认" );

      firstButton1.addActionListener(

         new ActionListener() {

            public void actionPerformed( ActionEvent event )
            {
               DisplayRecord();
            }
         }
      );
 //返回
 backButton1=userInterface1.getDotask3Button();
 backButton1.setText("返回");
 backButton1.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     mainFrame.show();
     dispose();
    }
   });
 backButton2=userInterface2.getDotask3Button();
 backButton2.setText("返回");
 backButton2.addActionListener(
   new ActionListener(){
    public void actionPerformed(ActionEvent event){
     mainFrame.show();
     dispose();
    }
   }); 
      secondButton1 = userInterface1.getDoTask2Button();
      secondButton1.setText( "清除" );

      secondButton1.addActionListener(

         new ActionListener() {

            public void actionPerformed( ActionEvent event )
            {
               userInterface1.clearFields();
            }}
 );

      firstButton2 = userInterface2.getDoTask1Button();
      firstButton2.setText( "确认删除" );

      firstButton2.addActionListener(

         new ActionListener() {

            public void actionPerformed( ActionEvent event )
            {
               UpdateRecord();
            } } );

      secondButton2 = userInterface2.getDoTask2Button();
      secondButton2.setText( "放弃" );

      secondButton2.addActionListener(

         new ActionListener() {

            public void actionPerformed( ActionEvent event )
            {
               userInterface2.clearFields();
            } }
      );
      addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent event )
            {
              
               terminate();
            }} );
    setBounds(200,200,400,260);
      setVisible( true );

   }
  
   public void DisplayRecord()
   {
    
    String fieldValues1[] = userInterface1.getFieldValues();
    
    String fieldValues2 []=new String[5] ;
    if ( ! fieldValues1[ StudentUI.SNO ].equals( "" ) ) {
        
         snoUpdate=fieldValues1[0];

         try {
            int numberAge = Integer.parseInt(
            fieldValues1[ StudentUI.SNO ] );
            
            String sqlString = "select * from student "+
                             " where sno='"+fieldValues1[0] + "'";
                               
            ResultSet resultSet =statement.executeQuery( sqlString)  ;
            ResultSetMetaData metaData = resultSet.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
           
            if ( resultSet.next() ) {
                fieldValues2[0]=resultSet.getString( 1 ) ;
                fieldValues2[1]=resultSet.getString( 2 ) ;
                fieldValues2[2]=resultSet.getString( 3 ) ;
                fieldValues2[3]=String.valueOf(resultSet.getInt( 4 )) ;
                fieldValues2[4]=resultSet.getString( 5 ) ;
               
                userInterface2.setFieldValues(fieldValues2);
            }   
            else
            {
                userInterface2.clearFields();
                JOptionPane.showMessageDialog( this,
                  "没有这个记录", "Find Result",
                    JOptionPane.INFORMATION_MESSAGE );
            }    
                            
         }

         catch ( NumberFormatException formatException ) {
            JOptionPane.showMessageDialog( this,
               "年龄有错误", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
         }
         catch (SQLException ee)
  {   System.out.println(ee); }
  } 
  else
            JOptionPane.showMessageDialog( this,
               "Bad sno number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
      }

   public void UpdateRecord()
   {
     
      String fieldValues[] = userInterface2.getFieldValues();

      if ( ! fieldValues[ StudentUI.SNO ].equals( "" ) ) {

         try {
            int numberAge = Integer.parseInt(
            fieldValues[ StudentUI.SAGE ] );
            String sqlString = "delete from student  "+
                               "where sno='"+snoUpdate + "'";
        
            int result = statement.executeUpdate(sqlString);
            if (result!=0) {
                JOptionPane.showMessageDialog( this,
                "Deleted sucess!", "Delete Result",
                 JOptionPane.INFORMATION_MESSAGE );
            } }
       catch ( NumberFormatException formatException ) {
            JOptionPane.showMessageDialog( this,
               "Bad age number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
         }
         catch (SQLException ee)
    { JOptionPane.showMessageDialog( null, ee.getMessage()+"请重新连接",
            "二次连接错误", JOptionPane.ERROR_MESSAGE ); }
  }
  else
            JOptionPane.showMessageDialog( this,
               "Bad sno number ", "Invalid Number Format",
               JOptionPane.ERROR_MESSAGE );
   }
}
//_______________________________________________________________________________
//--------------------------------------------------DisplayStudents查询
class DisplayStudents extends JFrame {
String sqlString;
   public DisplayStudents(String all)
   {  
      super( "Students Table of Study Database" );
  sqlString=all;
      try {
       
         ResultSet resultSet =
            statement.executeQuery
            ( sqlString)  ;
         StringBuffer results = new StringBuffer();
         ResultSetMetaData metaData = resultSet.getMetaData();
         int numberOfColumns = metaData.getColumnCount();
         for ( int i = 1; i <= numberOfColumns; i++ )
            results.append( metaData.getColumnName( i ) + "\t" );
         results.append( "\n" );
         while ( resultSet.next() ) {
            for ( int i = 1; i <= numberOfColumns; i++ )
               results.append( resultSet.getObject( i ) + "\t" );
            results.append( "\n" );
         }

         JTextArea textArea = new JTextArea( results.toString() );
         Container container = getContentPane();

         container.add( new JScrollPane( textArea ) );
         setBounds(200,200,450,200);
         setVisible( true );
        
      }

      catch ( SQLException sqlException ) {
         JOptionPane.showMessageDialog( null, sqlException.getMessage()+"请重新连接",
            "错误", JOptionPane.ERROR_MESSAGE );
        
         System.exit( 1 );
      }
      }
}
//_________________________________________________________________________________
//查询列表
class findClass extends JFrame{
private JTextField snoField,snameField,ssexField,sageField,sdeptField; 
private JButton snoButton,snameButton,ssexButton,sageButton,sdeptButton,lieButton;
private Button backButton;
public findClass(){
  super("查询专用");
  getContentPane().setLayout(new GridLayout(6,2));
//变量
  snoButton=new JButton("按照学号列表");
  snoButton.setFont(new Font("SansSerif",Font.PLAIN,18));
  snameButton=new JButton("按照姓名列表");
  snameButton.setFont(new Font("SansSerif",Font.PLAIN,18));
  ssexButton=new JButton("按照性别列表");
  ssexButton.setFont(new Font("SansSerif",Font.PLAIN,18));
  sageButton=new JButton("按照年龄列表");
  sageButton.setFont(new Font("SansSerif",Font.PLAIN,18));
  sdeptButton=new JButton("按照系别列表");
  sdeptButton.setFont(new Font("SansSerif",Font.PLAIN,18));
  lieButton=new JButton("总列表");
  lieButton.setFont(new Font("SansSerif",Font.PLAIN,18));
  backButton=new Button("返回");
  backButton.setFont(new Font("SansSerif",Font.PLAIN,18));
  snoField=new JTextField(10);
  snameField=new JTextField(10);
  ssexField=new JTextField(10);
  sageField=new JTextField(10);
  sdeptField=new JTextField(10);
 
  getContentPane().add(snoField);
  getContentPane().add(snoButton);
  getContentPane().add(snameField);
  getContentPane().add(snameButton);
  getContentPane().add(ssexField);
  getContentPane().add(ssexButton);
  getContentPane().add(sageField);
  getContentPane().add(sageButton);
  getContentPane().add(sdeptField);
  getContentPane().add(sdeptButton);
  getContentPane().add(lieButton);
  getContentPane().add(backButton);
 
  setBounds(200,200,300,200);
  setVisible(true);
  mainFrame.dispose();

//----------监听
  addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent event ){
            terminate(); } }
);
  snoButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
  String all=new String("select 学号 = sno,姓名=sname,ssex as 性别,sage as 年龄,sdept as 所在系 from student where sno ="+"'"+snoField.getText()+"'");
 new DisplayStudents(all);
 }});
 snameButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
  String all=new String("select 学号 = sno,姓名=sname,ssex as 性别,sage as 年龄,sdept as 所在系 from student where sname ="+"'"+snameField.getText()+"'");
 new DisplayStudents(all);
 }});
 ssexButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
  String all=new String("select 学号 = sno,姓名=sname,ssex as 性别,sage as 年龄,sdept as 所在系 from student where ssex ="+"'"+ssexField.getText()+"'");
 new DisplayStudents(all);
 }});
 sageButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
  String all=new String("select 学号 = sno,姓名=sname,ssex as 性别,sage as 年龄,sdept as 所在系 from student where sage ="+"'"+sageField.getText()+"'");
 new DisplayStudents(all);
 }});
 sdeptButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
  String all=new String("select 学号 = sno,姓名=sname,ssex as 性别,sage as 年龄,sdept as 所在系 from student where sdept ="+"'"+sdeptField.getText()+"'");
 new DisplayStudents(all);
 }});
 lieButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
  String all=new String("select 学号 = sno,姓名=sname,ssex as 性别,sage as 年龄,sdept as 所在系 from student ");
  new DisplayStudents(all);
 }});
 backButton.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
  mainFrame.show();
  dispose();
 }});
//_________________________________ 
 }
}
//__________________________________________________________________________________
}

阅读(544) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册