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

📄 jpanel3.java~14~

📁 学生管理系统
💻 JAVA~14~
字号:
package guanlixitong;

import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.*;
import java.sql.*;

public class JPanel3
    extends JPanel implements ActionListener {
  public JPanel3() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  Student st = new Student();
  private void jbInit() throws Exception {
    this.setLayout(null);
    jTextField1.setBounds(new Rectangle(237, 81, 166, 28));
    jLabel4.setText("班   级 ");
    jLabel4.setBounds(new Rectangle(133, 306, 64, 29));
    jLabel4.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
    jLabel4.setForeground(Color.magenta);
    jLabel5.setText("电   话");
    jLabel5.setBounds(new Rectangle(133, 256, 61, 29));
    jLabel5.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
    jLabel5.setForeground(Color.magenta);
    jLabel6.setBounds(new Rectangle(133, 209, 61, 29));
    jLabel6.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
    jLabel6.setForeground(Color.magenta);
    jLabel6.setText("地   址");
    jLabel7.setText("姓   名");
    jLabel7.setBounds(new Rectangle(133, 165, 61, 29));
    jLabel7.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
    jLabel7.setForeground(Color.magenta);
    jLabel8.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
    jLabel8.setForeground(Color.magenta);
    jLabel8.setText("性   别");
    jLabel8.setBounds(new Rectangle(133, 122, 61, 29));
    jTextField2.setBounds(new Rectangle(237, 165, 166, 28));
    jTextField3.setText(" ");
    jTextField3.setBounds(new Rectangle(237, 209, 253, 28));
    jTextField4.setBounds(new Rectangle(237, 256, 253, 28));
    jTextField5.setText(" ");
    jTextField5.setBounds(new Rectangle(237, 306, 253, 28));
    jButton1.setBounds(new Rectangle(133, 369, 144, 32));
    jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
    jButton1.setForeground(Color.magenta);
    jButton1.setText("录  入");
    jButton1.addActionListener(this);
    jButton2.addActionListener(this);
    jButton2.setText("查  询");
    jButton2.setForeground(Color.magenta);
    jButton2.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
    jButton2.setBounds(new Rectangle(346, 369, 144, 32));
    jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
    jLabel1.setForeground(Color.magenta);
    jLabel1.setText("学   号");
    jLabel1.setBounds(new Rectangle(133, 81, 61, 29));
    jTextField6.setBounds(new Rectangle(237, 122, 166, 28));
    jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 16));
    jLabel2.setForeground(Color.red);

    jLabel2.setBounds(new Rectangle(133, 30, 331, 29));
    this.add(jButton2, null);
    this.add(jButton1);
    this.add(jLabel4);
    this.add(jLabel5);
    this.add(jLabel6);
    this.add(jLabel7);
    this.add(jTextField5);
    this.add(jTextField4);
    this.add(jTextField3);
    this.add(jTextField2);
    this.add(jTextField1);
    this.add(jLabel1);
    this.add(jLabel8);
    this.add(jTextField6);
    this.add(jLabel2);
  }

  JTextField jTextField1 = new JTextField();
  JLabel jLabel4 = new JLabel();
  JLabel jLabel5 = new JLabel();
  JLabel jLabel6 = new JLabel();
  JLabel jLabel7 = new JLabel();
  JLabel jLabel8 = new JLabel();
  JTextField jTextField2 = new JTextField();
  JTextField jTextField3 = new JTextField();
  JTextField jTextField4 = new JTextField();
  JTextField jTextField5 = new JTextField();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JLabel jLabel1 = new JLabel();
  JTextField jTextField6 = new JTextField();
  JLabel jLabel2 = new JLabel();
  public void actionPerformed(ActionEvent e) {
   jLabel2.setText("");
    if (e.getSource() == jButton1) {
      int num = Integer.parseInt(jTextField1.getText());
      String url = "jdbc:odbc:yangAcess";
      Connection con;
      Statement stmt;
      String str = "select 学号,姓名,地址,电话,班级,性别 from yang1 where 学号=" + num;
      ResultSet rs;
      try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection(url);
        stmt = con.createStatement();
        rs = stmt.executeQuery(str);
        while (rs.next()) {
          st.学号 = rs.getInt(1);
          st.姓名 = rs.getString(2);
          st.地址 = rs.getString(3);
          st.电话 = rs.getString(4);
          st.班级 = rs.getString(5);
          st.性别 = rs.getString(6);
        }
      }
      catch (Exception ae) {System.out.print("you");}
      if (num == st.学号&&!st.性别.equals("")) {

      ShowFrame show=new ShowFrame();
      show.setVisible(true);
      }
      else {
        getDate();
      insert();
      }
    }
    else if (e.getSource() == jButton2) {
      int num = Integer.parseInt(jTextField1.getText());
      String url = "jdbc:odbc:yangAcess";
      Connection con;
      Statement stmt;
      String str = "select 学号,姓名,地址,电话,班级,性别 from yang1 where 学号=" + num;
      ResultSet rs;
      try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection(url);
        stmt = con.createStatement();
        rs = stmt.executeQuery(str);
        while (rs.next()) {
          st.学号 = rs.getInt(1);
          st.姓名 = rs.getString(2);
          st.地址 = rs.getString(3);
          st.电话 = rs.getString(4);
          st.班级 = rs.getString(5);
          st.性别 = rs.getString(6);
        }
      }
      catch (Exception ae) {}
      if (st.学号 == num) {
        jTextField1.setText(Integer.toString(st.学号));
        jTextField2.setText(st.姓名);
        jTextField3.setText(st.地址);
        jTextField4.setText(st.电话);
        jTextField5.setText(st.班级);
        jTextField6.setText(st.性别);
      }
      else {
        jLabel2.setText("查  不  到  你  所  输  学  号  信  息");
      }
    }
  }
  private class ShowFrame
       extends JFrame implements ActionListener {
     JButton btn1, btn2;
     public ShowFrame() {
       this.setSize(400, 200);
       this.setLocation(200, 200);
       Container con = this.getContentPane();
       con.setLayout(new FlowLayout());

       JLabel jb = new JLabel("此学号已存在,你想重置吗?");
       jb.setForeground(Color.magenta);
       jb.setFont(new java.awt.Font("Dialog", 0, 25));
       btn1 = new JButton("确    定");
       btn1.setForeground(Color.magenta);
       btn1.setFont(new java.awt.Font("Dialog", 0, 15));
       btn2 = new JButton("取    消");
       btn2.setForeground(Color.magenta);
       btn2.setFont(new java.awt.Font("Dialog", 0, 15));
       con.add(jb);
       con.add(btn1);
       con.add(btn2);
       btn1.addActionListener(this);
       btn2.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == btn1) {

        this.setVisible(false);
        getDate();
        updata();
      }
      else if (e.getSource() == btn2) {

        this.setVisible(false);
      }
    }

  }
    public void updata() {
      if (st.学号 != 0) {
        String str = "jdbc:odbc:yangAcess";
        Connection con;
        // String str1 =
        //"update yang1 set 姓名='"+std.姓名+"',成绩1="+std.成绩1+",成绩2="+std.成绩2+",成绩3="+std.成绩3+
        //",成绩4="+std.成绩4+",成绩5="+std.成绩5+ "where 学号="+std.学号;
        String str1 =
            "update yang1 set 姓名='" + st.姓名 + "',性别='" + st.性别 + "',地址 ='"+
            st.地址 + "',班级='" + st.班级 + "',电话='" + st.电话 +
            "' where 学号=" + st.学号;
        try {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          con = DriverManager.getConnection(str);
          Statement update = con.createStatement();
          /**********************************************
             update.setString(1, std.姓名);
             update.setInt(2, std.成绩1);
             update.setInt(3, std.成绩2);
             update.setInt(4, std.成绩3);
             update.setInt(5, std.成绩4);
             update.setInt(6, std.成绩5);
            update.setInt(7, std.学号);
           ******************************************/

          update.executeUpdate(str1);
          con.close();
          update.close();

        }
        catch (Exception ae) {
          System.out.print("lk;ag;oiuje");
        }
      }
    }

    public void insert() {
      if (st.学号 != 0) {
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String ur1 = "jdbc:odbc:yangAcess";

        Connection conn;
        int age = 0;
        Statement statement;

        try {

          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          conn = DriverManager.getConnection(ur1);

          PreparedStatement pstmt = conn.prepareStatement(
              "insert into yang1 values(?,?,?,0,0,0,0,0,?,?,?,0,0,0,0,0)");
          {
            pstmt.setInt(1, st.学号);

            pstmt.setString(2, st.姓名);
            pstmt.setString(3, st.性别);
            pstmt.setString(4, st.地址);
            pstmt.setString(5, st.电话);
            pstmt.setString(6, st.班级);


          }

          pstmt.executeUpdate();

          pstmt.close();
          conn.close();

        }
        catch (Exception e) {
         System.out.print("I");

        }
      }
    }

    public void getDate() {
      if (!jTextField1.getText().equals("")) {
       st.学号=Integer.parseInt(jTextField1.getText());
      }
      if (!jTextField2.getText().equals("")) {
        st.姓名=jTextField2.getText();
      }
      if (!jTextField3.getText().equals("")) {
       st.地址=jTextField3.getText();
      }
      if (!jTextField4.getText().equals("")) {
       st.电话=jTextField4.getText();
      }
      if (!jTextField5.getText().equals("")) {
       st.班级=jTextField5.getText();
      }
      if (!jTextField6.getText().equals("")) {
        st.性别=jTextField6.getText();
      }


    }

}

⌨️ 快捷键说明

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