⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame1.java

📁 java开发的一个基于mysql的小数据库系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package 桀骜学管理系统;

import java.awt.Dimension;
import javax.swing.JOptionPane.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import com.borland.dx.sql.dataset.*;
import com.borland.jbcl.layout.XYConstraints;
import com.borland.jbcl.layout.XYLayout;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
import javax.swing.table.*;
//import javax.swing.RadioBotton.*;

public class Frame1 extends JFrame {
    Connection connect;
    String url="jdbc:odbc:task";
    private DefaultTableModel defaultTableModel1=new DefaultTableModel();
    JPanel contentPane;
    XYLayout xYLayout1 = new XYLayout();
    JPanel jPanel1 = new JPanel();
    JPanel jPanel2 = new JPanel();
    JPanel jPanel3 = new JPanel();
    JPanel jPanel4 = new JPanel();
    JLabel jLabel1 = new JLabel();
    XYLayout xYLayout2 = new XYLayout();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JLabel jLabel5 = new JLabel();
    JLabel jLabel6 = new JLabel();
    JLabel jLabel7 = new JLabel();
    JLabel jLabel8 = new JLabel();
    XYLayout xYLayout4 = new XYLayout();
    JLabel jLabel9 = new JLabel();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JButton jButton3 = new JButton();
    JButton jButton4 = new JButton();
    Database database1 = new Database();
    XYLayout xYLayout3 = new XYLayout();
    JLabel jLabel10 = new JLabel();
    QueryDataSet queryDataSet1 = new QueryDataSet();
    QueryDataSet queryDataSet2 = new QueryDataSet();
    JTextField jTextField1 = new JTextField();
    JTextField jTextField2 = new JTextField();
    JTextField jTextField3 = new JTextField();
    JTextField jTextField4 = new JTextField();
    JTextField jTextField5 = new JTextField();
    JTextField jTextField6 = new JTextField();
    JTextField jTextField7 = new JTextField();
    JRadioButton jRadioButton1 = new JRadioButton();
    JRadioButton jRadioButton2 = new JRadioButton();
    JTextField jTextField8 = new JTextField();
    JScrollPane jScrollPane1 = new JScrollPane();
    QueryDataSet queryDataSet3 = new QueryDataSet();
    JTable jTable1 = new JTable();
    javax.swing.JButton jButton5 = new JButton();
    public Frame1() {
        try {
            //setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(xYLayout1);
        setSize(new Dimension(600, 400));
        setTitle("教师查询");
        jLabel1.setText("姓   名:");
        jPanel1.setLayout(xYLayout2);
        jLabel2.setText("教师号:");
        jLabel3.setText("职   称:");
        jLabel4.setText("工   资:");
        jLabel5.setText("教研室:");
        jLabel6.setText("生     日:");
        jPanel2.setLayout(xYLayout3);
        jLabel7.setText("性     别:");
        jLabel8.setText("授课信息:");
        jPanel3.setLayout(xYLayout4);
        jLabel9.setText("地   址:");
         jLabel10.setText("职     务:");
        jButton1.setText("插入");
        jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
        jButton2.setText("查找");
        jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
        jButton3.setText("清空");
        jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this));
        jButton4.setText("修改");
        jButton4.addActionListener(new Frame1_jButton4_actionAdapter(this));
        database1.setConnection(new ConnectionDescriptor("jdbc:odbc:task",
                "root", "abc", false, "sun.jdbc.odbc.JdbcOdbcDriver"));                   //数据库连接
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connect=DriverManager.getConnection(url,"root","abc");
        }
        catch   (SQLException   sqlex)
        {
                    System.err.println("unable   to   connect");
                   sqlex.printStackTrace();
         }
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jRadioButton1.setToolTipText("");
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("男");
        jRadioButton1.addActionListener(new Frame1_jRadioButton1_actionAdapter(this));
        jRadioButton2.setText("女");
        jRadioButton2.addActionListener(new Frame1_jRadioButton2_actionAdapter(this));
        jTextField4.addActionListener(new Frame1_jTextField4_actionAdapter(this));
        jButton5.setText("删除");
        jButton5.addActionListener(new Frame1_jButton5_actionAdapter(this));
        contentPane.add(jPanel4, new XYConstraints( -2, 318, 603, 85));
        jPanel4.add(jButton1);
        jPanel4.add(jButton2);
        jPanel4.add(jButton3);
        jPanel4.add(jButton4);
        jPanel4.add(jButton5);
        contentPane.add(jPanel3, new XYConstraints( -1, 260, 602, 54));
        jPanel3.add(jLabel9, new XYConstraints(18, 15, 63, 29));
        jPanel2.add(jLabel7, new XYConstraints(26, 94, 42, -1));
        contentPane.add(jPanel1, new XYConstraints( -1, 4, 298, 248));
        jPanel1.add(jLabel1, new XYConstraints(20, 27, 60, 26));
        jPanel1.add(jLabel2, new XYConstraints(20, 65, 60, 26));
        jPanel1.add(jLabel3, new XYConstraints(20, 100, 60, 26));
        jPanel1.add(jLabel4, new XYConstraints(20, 139, 60, 26));
        jPanel1.add(jLabel5, new XYConstraints(20, 178, 60, 26));
        contentPane.add(jPanel2, new XYConstraints(300, 4, 289, 252));
        jPanel2.add(jTextField6, new XYConstraints(85, 31, 149, 27));
        jPanel2.add(jTextField7, new XYConstraints(85, 63, 149, 27));
        jPanel2.add(jRadioButton1, new XYConstraints(79, 96, 83, 27));
        jPanel3.add(jTextField8, new XYConstraints(84, 17, 459, 27));
        jPanel2.add(jRadioButton2, new XYConstraints(166, 96, 83, 27));
        jPanel2.add(jScrollPane1, new XYConstraints(83, 131, 180, 98));
        jScrollPane1.getViewport().add(jTable1);
        jPanel2.add(jLabel8, new XYConstraints(29, 128, 51, 17));
        jPanel2.add(jLabel6, new XYConstraints(28, 37, -1, -1));
        jPanel2.add(jLabel10, new XYConstraints(28, 66, -1, -1));
        jPanel1.add(jTextField1, new XYConstraints(85, 26, 160, 27));
        jPanel1.add(jTextField2, new XYConstraints(85, 64, 160, 27));
        jPanel1.add(jTextField3, new XYConstraints(85, 99, 160, 27));
        jPanel1.add(jTextField4, new XYConstraints(86, 138, 160, 27));
        jPanel1.add(jTextField5, new XYConstraints(85, 177, 160, 27));
    }
       //jButton2的响应事件是输入教师号来查询教师的所有信息

   public void jButton2_actionPerformed(ActionEvent e)
    {
          try
          {
              Statement statement =connect.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);                                        //进行数据库访问
               String query1="select * from teacher where ssn="+"'"+jTextField2.getText()+"'";
               String query2="select salary from pay where position in"+
                             "("+"select position from teacher where ssn="
                             +jTextField2.getText()+")";
               String query3="select class,period from sclass where Nclass in(select Nclass from tclass where ssn="
                             +jTextField2.getText()+" ) ";
               ResultSet rs1 = statement.executeQuery( query1 );                                        //把查询结果复值给游标
               if(rs1.next())
               {
                       jTextField1.setText( rs1.getString( "Tname" ) );
                       jTextField3.setText( rs1.getString( "position" ) );
                       jTextField5.setText( rs1.getString( "Troom" ) );
                       jTextField6.setText( rs1.getString( "Tbirthday" ) );
                       jTextField8.setText( rs1.getString( "address" ) );
                       jTextField7.setText(rs1.getString("headship"));
                       String a=rs1.getString("sex");
                       if( a.equalsIgnoreCase("女"))                                 //字符串比较
                       {
                          jRadioButton1.setSelected(false);
                          jRadioButton2.setSelected(true);
                       }
                       else
                       {
                           jRadioButton1.setSelected(true);
                          jRadioButton2.setSelected(false);}
               }
               else
               {
                  String find="查无此人";
                  message(find);
               }
               rs1 = statement.executeQuery( query2 );
               if(rs1.next())
                   jTextField4.setText(rs1.getString("salary"));
               //if(rs1.next())
                    //jTextField4.setText(rs1.getString("salary"));
               statement=connect.createStatement();
               rs1=statement.executeQuery(query3);                                     //jTable的复值不太理解!!!
               ResultSetMetaData rsmd1=rs1.getMetaData();
               defaultTableModel1.setColumnCount(0);
               defaultTableModel1.setRowCount(0);
               for(int i=1;i<=rsmd1.getColumnCount();i++)
                {
                      defaultTableModel1.addColumn(rsmd1.getColumnName(i));
                }
               int i=0;
               while(rs1.next())
                 {
                       defaultTableModel1.setNumRows(i+1);
                       for (int j=0;j<rsmd1.getColumnCount();j++)
                           {
                               defaultTableModel1.setValueAt(rs1.getString(j+1),i,j);
                           }
                       i++;
               }
              rs1.close();
              jTable1.setModel((TableModel)defaultTableModel1);
              statement.close();
          }
          catch(Exception sqlex)
          {
              System.out.println("unable run ***");
              sqlex.printStackTrace();
          }

    }

//jButton3的响应事件是清空显示信息但不删除
    public void jButton3_actionPerformed(ActionEvent e)
    {
      jTextField1.setText("");
      jTextField2.setText("");
      jTextField3.setText("");
      jTextField4.setText("");
      jTextField5.setText("");
      jTextField6.setText("");
      jTextField7.setText("");
      jTextField8.setText("");
      jRadioButton1.setSelected(true);
      jRadioButton2.setSelected(false);

      defaultTableModel1.setRowCount(0);
    }

//jButton1的响应事件是插入新值
    public void jButton1_actionPerformed(ActionEvent e) {
        try {
            boolean test=true;
            String kong1="";
            Statement statement = connect.createStatement(ResultSet.
                    TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            ////////////////////
            String sex;
            boolean flag=false;
            if(jRadioButton2.isSelected())               //判断插入人的性别,这里应该用设成一个函数,使用更方便些
            {
                 flag= false;
                 sex="女";
            }
            else
                { flag=true;
                 sex="男";}
            ////////////////////////////////
            String query="insert into teacher values ('"+jTextField1.getText() + "', '" +
                                                        jTextField2.getText() + "', '" +
                                                        jTextField6.getText() + "', '" +
                                                        jTextField3.getText() + "', '" +
                                                        jTextField8.getText() + "', '" +
                                                        sex + "', '" +
                                                        jTextField5.getText() + "', '" +
                                                        jTextField7.getText() + "')";
             if(jTextField2!=null)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -