希望对各位有所帮助

源代码在线查看: search.java~54~

软件大小: 2491 K
上传用户: haibokaishi2006
关键词:
下载地址: 免注册下载 普通下载 VIP

相关代码

				package studentms;
				
				import com.borland.jbcl.layout.*;
				import java.awt.*;
				import java.sql.*;
				import javax.swing.*;
				import javax.swing.table.*;
				import java.util.*;
				import java.awt.event.*;
				
				
				public class search extends JFrame {
				
				  XYLayout xYLayout1 = new XYLayout();
				  JLabel jLabel1 = new JLabel();
				  JTextField jTContent = new JTextField();
				  JButton jBOK = new JButton();
				  JButton jBCancel = new JButton();
				
				  String searchType =new String();     //标记查询类别
				  String searchValue=new String();     //标记查询值
				
				  String infoShowType=new String();      //标记要显示窗体的类型,综合、修改还是删除
				
				  Database DB=new Database();
				  ResultSet rs ;
				  String sql=new String();
				
				  JRadioButton jRByName = new JRadioButton();
				  JRadioButton jRByNum = new JRadioButton();
				  JRadioButton jRByClass = new JRadioButton();
				  ButtonGroup group=new ButtonGroup();
				
				  public search() {
				    try {
				      jbInit();
				    }
				    catch(Exception e) {
				      e.printStackTrace();
				    }
				  }
				  private void jbInit() throws Exception {
				    jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
				    xYLayout1.setWidth(298);
				    xYLayout1.setHeight(130);
				    this.getContentPane().setLayout(xYLayout1);
				    jBOK.setFont(new java.awt.Font("Dialog", 0, 12));
				    jBOK.setText("确  定");
				    jBOK.addActionListener(new search_jBOK_actionAdapter(this));
				    jBCancel.setFont(new java.awt.Font("Dialog", 0, 12));
				    jBCancel.setText("取  消");
				    jBCancel.addActionListener(new search_jBCancel_actionAdapter(this));
				    jTContent.setFont(new java.awt.Font("Dialog", 0, 12));
				    jTContent.setText("");
				    this.setTitle("人员查询");
				    jRByName.setFont(new java.awt.Font("Dialog", 0, 12));
				    jRByName.setText("按姓名");
				    jRByName.addActionListener(new search_jRByName_actionAdapter(this));
				    jRByNum.setFont(new java.awt.Font("Dialog", 0, 12));
				    jRByNum.setText("按学号");
				    jRByNum.addActionListener(new search_jRByNum_actionAdapter(this));
				    jRByClass.setFont(new java.awt.Font("Dialog", 0, 12));
				    jRByClass.setText("按班级");
				    jRByClass.addActionListener(new search_jRByClass_actionAdapter(this));
				    group.add(jRByName);
				    group.add(jRByNum);
				    group.add(jRByClass);
				    this.getContentPane().add(jLabel1,  new XYConstraints(26, 23, 161, 23));
				    this.getContentPane().add(jTContent, new XYConstraints(30, 57, 130, 25));
				    this.getContentPane().add(jBOK,    new XYConstraints(200, 23, 66, 24));
				    this.getContentPane().add(jBCancel,     new XYConstraints(200, 59, 66, 24));
				    this.getContentPane().add(jRByName, new XYConstraints(24, 103, 66, 17));
				    this.getContentPane().add(jRByNum, new XYConstraints(95, 103, -1, 16));
				    this.getContentPane().add(jRByClass,  new XYConstraints(168, 103, 69, 16));
				  }
				
				  //设置标签内容
				  void setLabelText(String content){
				    jLabel1.setText(content);
				  }
				
				  //设置查询类型
				  void setType(String i){
				    searchType=i;
				  }
				
				  //设置要显示的人员信息窗体
				  void setShowType(String type){
				    infoShowType=type;
				  }
				
				  //退出
				  void jBCancel_actionPerformed(ActionEvent e) {
				    this.dispose();
				  }
				
				  //显示结果集为多个时的列表
				  void listShow(){
				    stuShow stuList=new stuShow();
				    Dimension dlgSize = stuList.getPreferredSize();
				    Dimension frmSize = getSize();
				    Point loc = getLocation();
				    stuList.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
				    stuList.pack();
				    stuList.show() ;
				    stuList.resultShow(rs);
				  }
				
				 //参数为学生信息类,显示查询到的内容
				  void setContent(stuInfo result){
				    try{
				      result.jTNum.setText(rs.getString("Num"));
				      result.jTName.setText(rs.getString("Name"));
				      result.jTSex.setText(rs.getString("Sex"));
				      result.jTHome.setText(rs.getString("Home"));
				      result.jTClass.setText(rs.getString("Class"));
				      result.jTMinzu.setText(rs.getString("Minzu"));
				      result.jTBirth.setText(rs.getString("Birthday"));
				      result.nullSet() ;
				    }
				    catch(SQLException ex){
				    }
				  }
				
				 //显示查询结果
				  void showResult(){
				      if(infoShowType=="edit"){
				         setContent(menuClass.editInfo);
				         menuClass.editInfo.setAbled(true);
				         menuClass.editInfo.setButton(true,true,true,true,true);
				      }
				      else if(infoShowType=="del"){
				        setContent(menuClass.delInfo );
				        menuClass.delInfo .setAbled(false);
				        menuClass.delInfo .setButton(true,true,true,false,false);
				      }
				      else{
				        menuClass.SIFall .setLocation(200, 160);
				        menuClass.SIFall.pack();
				        menuClass.SIFall.show();
				        setContent(menuClass.SIFall);
				        menuClass.SIFall.setAbled(false);
				        menuClass.SIFall.setButton(true, true, true, false, false);
				      }
				   }
				
				  //执行查询操作
				  void jBOK_actionPerformed(ActionEvent e) {
				     if(jTContent.getText().equals("")){
				       JOptionPane.showMessageDialog(null, "查询值不能为空!");
				     }
				     else{
				       sql = "select * from stuTable ";
				
				       //根据选项判断查询的类型
				       if (searchType == "Name") {
				         sql = sql + "  where  Name='" + jTContent.getText().toString() + "'";
				       }
				       else if (searchType == "Num") {
				         sql = sql + "  where  Num=" + Integer.parseInt(jTContent.getText());
				       }
				       else if (searchType == "Class") {
				         sql = sql + "where Class='" + jTContent.getText().toString() + "'";
				       }
				
				       rs = DB.getResult(sql);
				       try {
				         if (rs.first()) { //判断记录是否存在
				           if (rs.isLast()) { //判断是否只有一条记录,若是,直接显示,否则显示列表
				             rs.beforeFirst();
				             rs.next();
				             showResult();
				             this.dispose();
				           }
				           else {
				             listShow();
				             this.dispose();
				           }
				         }
				         else {
				           JOptionPane.showMessageDialog(null, "此人不存在!");
				         }
				       }
				       catch (SQLException ex) {
				         JOptionPane.showMessageDialog(null, " search failed!");
				       }
				     }
				
				  }
				
				  //响应单选按钮选中时的事件
				  void jRByName_actionPerformed(ActionEvent e) {
				       jLabel1.setText("请输入要查询人员的姓名:");
				       jTContent.setEnabled(true);
				       setType("Name");
				  }
				
				  void jRByNum_actionPerformed(ActionEvent e) {
				    jLabel1.setText("请输入要查询人员的学号:");
				    jTContent.setEnabled(true);
				    setType("Num");
				  }
				
				  void jRByClass_actionPerformed(ActionEvent e) {
				    jLabel1.setText("请输入要查询人员的班级:");
				    jTContent.setEnabled(true);
				    setType("Class");
				  }
				
				}
				
				//*******************以下是监听类部分*******************
				class search_jBCancel_actionAdapter implements java.awt.event.ActionListener {
				  search adaptee;
				
				  search_jBCancel_actionAdapter(search adaptee) {
				    this.adaptee = adaptee;
				  }
				  public void actionPerformed(ActionEvent e) {
				    adaptee.jBCancel_actionPerformed(e);
				  }
				}
				
				class search_jBOK_actionAdapter implements java.awt.event.ActionListener {
				  search adaptee;
				
				  search_jBOK_actionAdapter(search adaptee) {
				    this.adaptee = adaptee;
				  }
				  public void actionPerformed(ActionEvent e) {
				    adaptee.jBOK_actionPerformed(e);
				  }
				}
				
				class search_jRByName_actionAdapter implements java.awt.event.ActionListener {
				  search adaptee;
				
				  search_jRByName_actionAdapter(search adaptee) {
				    this.adaptee = adaptee;
				  }
				  public void actionPerformed(ActionEvent e) {
				    adaptee.jRByName_actionPerformed(e);
				  }
				}
				
				class search_jRByNum_actionAdapter implements java.awt.event.ActionListener {
				  search adaptee;
				
				  search_jRByNum_actionAdapter(search adaptee) {
				    this.adaptee = adaptee;
				  }
				  public void actionPerformed(ActionEvent e) {
				    adaptee.jRByNum_actionPerformed(e);
				  }
				}
				
				class search_jRByClass_actionAdapter implements java.awt.event.ActionListener {
				  search adaptee;
				
				  search_jRByClass_actionAdapter(search adaptee) {
				    this.adaptee = adaptee;
				  }
				  public void actionPerformed(ActionEvent e) {
				    adaptee.jRByClass_actionPerformed(e);
				  }
				}			

相关资源