/*ch0804.java*/
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;

public class ch0804 implements ActionListener
{
  static JFrame frame;
  static JPanel panel;
  JLabel labelName;
  JLabel labelDept;
  JLabel labelResult;
  JLabel labelSex;
  JTextField textName;
  JTextField textDept;
  JTextField textSex;
  JButton buttonquery=new JButton("查询");
  public static void main(String args[])
  {
    new ch0804();
  }
  public ch0804()
  {
      //创建一个面板并添加至frame上
    panel=new JPanel();
    frame=new JFrame("查询窗口");
    frame.setSize(300,150);
    frame.setVisible(true);
    frame.getContentPane().add(panel);
      //创建标签对象
    labelName=new JLabel("姓名：");
    labelDept=new JLabel("请输入要查询学生学号：");
    labelSex=new JLabel("性别：");
    labelResult=new JLabel("查询结果是：");
      //创建文本框对象
    textName=new JTextField(10);
    textSex=new JTextField(10);
    textDept=new JTextField(10);
      //将组件添加至面板
    panel.add(labelDept);
    panel.add(textDept);
    panel.add(buttonquery);
    panel.add(labelResult);
    panel.add(labelName);
    panel.add(textName);
    panel.add(labelSex);
    panel.add(textSex);
    buttonquery.addActionListener(this);
    labelResult.setVisible(false);
    labelName.setVisible(false);
    textName.setVisible(false);
    labelSex.setVisible(false);
    textSex.setVisible(false);
  }
  public void actionPerformed(ActionEvent e)
  {
    try
    {
      Object source=e.getSource();
      Connection con;
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:MydataSource","la","1234");
      ResultSet rs;
      if(source==buttonquery)
      {
        try
        {
          String s="SELECT 姓名,性别 FROM student WHERE 学号=?";
          PreparedStatement stat1=con.prepareStatement(s);
          stat1.setString(1,textDept.getText());
          rs=stat1.executeQuery();
          rs.next();
          labelResult.setVisible(true);
          labelName.setVisible(true);
          textName.setVisible(true);
          labelSex.setVisible(true);
          textSex.setVisible(true);
          textName.setText(rs.getString(1));
          textSex.setText(rs.getString(2));
        }
        catch(Exception exception)
        {
          System.out.println("Query Not Done"+exception);
        }
      }
    }
    catch(Exception ev)
    {
      System.out.println("Not connection");
    }
  }
}
