/*ch0805.java*/
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
public class ch0805 implements ActionListener
{
  static JFrame frame;
  static JPanel panel;
  JLabel labelName;
  JLabel labelDept;
  JLabel labelSex;
  JTextField textName;
  JTextField textDept;
  JTextField textSex;
  JButton buttonquery=new JButton("插入");

  public static void main(String args[])
  {
    new ch0805();
  }

  public ch0805()
  {
      //创建一个面板并添加至frame上
    panel=new JPanel();
    frame=new JFrame("学生信息登记窗口");
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.getContentPane().add(panel);
      //创建标签对象
    labelName=new JLabel("姓名：");
    labelDept=new JLabel("学号：");
    labelSex=new JLabel("性别：");
      //创建文本框对象
    textName=new JTextField(10);
    textSex=new JTextField(10);
    textDept=new JTextField(10);
      //将组件添加至面板
    panel.add(labelDept);
    panel.add(textDept);
    panel.add(labelName);
    panel.add(textName);
    panel.add(labelSex);
    panel.add(textSex);
    panel.add(buttonquery);
    buttonquery.addActionListener(this);
  }

  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");
      if(source==buttonquery)
      {
        try
        {
          String s="insert into student(学号,姓名,性别)values(?,?,?)";
          PreparedStatement stat1=con.prepareStatement(s);
          stat1.setString(1,textDept.getText());
          stat1.setString(2,textName.getText());
          stat1.setString(3,textSex.getText());
          stat1.executeUpdate();
        }
        catch (Exception exception)
        {
          System.out.println("Insert Not Done"+exception);
        }
      }
    }
    catch (Exception ev)
    {
      System.out.println("Not connecting");
    }
  }
}
