By GokiSoft.com| 20:20 19/06/2024|
Java Advanced

[Share Code] Phần mềm quản lý sinh viên MySQL + Java - Chương trình quản lý sinh viên MySQL + Java - Lập Trình Java - C2307L

Phần mềm quản lý sinh viên MySQL + Java - Chương trình quản lý sinh viên MySQL + Java - Lập Trình Java


#BaseEntity.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson08;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author teacher
 */
public abstract class BaseEntity<T> {
    final String HOST = "jdbc:mysql://localhost:3308/c2307l";
    final String USERNAME = "root";
    final String PASSWORD = "";
    
    Connection con = null;
    PreparedStatement statement = null;

    public void openConnection() {
        try {
            //KET NOI CSDL
            //B1. Tao ket noi toi CSDL
            con = DriverManager.getConnection(HOST, USERNAME, PASSWORD);
        } catch (SQLException ex) {
            Logger.getLogger(com.gokisoft.lesson06.BaseEntity.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void closeConnection() {
        //B3. Dong ket noi toi CSDL
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
                Logger.getLogger(com.gokisoft.lesson06.BaseEntity.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(com.gokisoft.lesson06.BaseEntity.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public abstract List<T> findAll();
    public abstract void insert(T item);
    public abstract void update(T item);
    public abstract void delete(T item);
    public abstract T findById(T item);
}


#readme.txt


1) Tao CSDL
create table students (
	id int primary key auto_increment,
	rollno varchar(20),
	fullname varchar(50),
	gender varchar(12),
	email varchar(150),
	address varchar(200)
)

2) Tao model
3) Tao CRUD


#Student.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson08;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author teacher
 */
public class Student {
    int id;
    String rollno;
    String fullname;
    String gender;
    String email;
    String address;

    public Student() {
    }

    public Student(int id, String rollno, String fullname, String gender, String email, String address) {
        this.id = id;
        this.rollno = rollno;
        this.fullname = fullname;
        this.gender = gender;
        this.email = email;
        this.address = address;
    }

    public Student(ResultSet resultSet) {
        try {
            this.id = resultSet.getInt("id");
            this.rollno = resultSet.getString("rollno");
            this.fullname = resultSet.getString("fullname");
            this.gender = resultSet.getString("gender");
            this.email = resultSet.getString("email");
            this.address = resultSet.getString("address");
        } catch (SQLException ex) {
            Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRollno() {
        return rollno;
    }

    public void setRollno(String rollno) {
        this.rollno = rollno;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", rollno=" + rollno + ", fullname=" + fullname + ", gender=" + gender + ", email=" + email + ", address=" + address + '}';
    }
    
    public void display() {
        System.out.println(this);
    }
}


#StudentEntity.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.lesson08;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author teacher
 */
public class StudentEntity extends BaseEntity<Student> {

    static StudentEntity instance = null;

    private StudentEntity() {
    }

    public synchronized static StudentEntity getInstance() {
        if (instance == null) {
            instance = new StudentEntity();
        }
        return instance;
    }

    @Override
    public List<Student> findAll() {
        return null;
    }

    @Override
    public void insert(Student item) {
    }

    @Override
    public void update(Student item) {
        openConnection();

        String sql = "update students set fullname=?,gender=?,email=?,address=?,rollno=? where id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setString(1, item.getFullname());
            statement.setString(2, item.getGender());
            statement.setString(3, item.getEmail());
            statement.setString(4, item.getAddress());
            statement.setString(5, item.getRollno());
            statement.setInt(6, item.getId());
            
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(StudentEntity.class.getName()).log(Level.SEVERE, null, ex);
        }

        closeConnection();
    }

    @Override
    public void delete(Student item) {
    }

    @Override
    public Student findById(Student item) {
        return null;
    }

    public Student findByRollno(String rollno) {
        openConnection();

        Student std = null;
        String sql = "select * from students where rollno = ?";
        try {
            statement = con.prepareStatement(sql);
            statement.setString(1, rollno);

            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                std = new Student(resultSet);
                break;
            }
        } catch (SQLException ex) {
            Logger.getLogger(StudentEntity.class.getName()).log(Level.SEVERE, null, ex);
        }

        closeConnection();

        return std;
    }
}


#StudentForm.form


<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
  <Properties>
    <Property name="defaultCloseOperation" type="int" value="3"/>
  </Properties>
  <SyntheticProperties>
    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
    <SyntheticProperty name="generateCenter" type="boolean" value="false"/>
  </SyntheticProperties>
  <AuxValues>
    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
  </AuxValues>

  <Layout>
    <DimensionLayout dim="0">
      <Group type="103" groupAlignment="0" attributes="0">
          <Group type="102" alignment="0" attributes="0">
              <EmptySpace min="-2" pref="20" max="-2" attributes="0"/>
              <Group type="103" groupAlignment="0" attributes="0">
                  <Group type="103" alignment="1" groupAlignment="0" attributes="0">
                      <Group type="102" attributes="0">
                          <Component id="btnSave" min="-2" max="-2" attributes="0"/>
                          <EmptySpace type="separate" max="-2" attributes="0"/>
                          <Component id="btnReset" min="-2" max="-2" attributes="0"/>
                      </Group>
                      <Component id="txtAddress" min="-2" pref="240" max="-2" attributes="0"/>
                  </Group>
                  <Group type="103" groupAlignment="1" max="-2" attributes="0">
                      <Group type="102" attributes="0">
                          <Component id="jLabel2" min="-2" max="-2" attributes="0"/>
                          <EmptySpace min="-2" pref="44" max="-2" attributes="0"/>
                          <Component id="txtRollno" min="-2" pref="240" max="-2" attributes="0"/>
                      </Group>
                      <Group type="102" attributes="0">
                          <Component id="jLabel1" min="-2" max="-2" attributes="0"/>
                          <EmptySpace min="-2" pref="44" max="-2" attributes="0"/>
                          <Component id="txtFullname" min="-2" pref="240" max="-2" attributes="0"/>
                      </Group>
                      <Group type="102" alignment="0" attributes="0">
                          <Group type="103" groupAlignment="1" attributes="0">
                              <Component id="jLabel4" min="-2" max="-2" attributes="0"/>
                              <Component id="jLabel3" min="-2" max="-2" attributes="0"/>
                              <Component id="jLabel5" alignment="1" min="-2" max="-2" attributes="0"/>
                          </Group>
                          <EmptySpace min="48" max="32767" attributes="0"/>
                          <Group type="103" groupAlignment="0" attributes="0">
                              <Component id="txtEmail" min="-2" pref="240" max="-2" attributes="0"/>
                              <Component id="txtGender" min="-2" max="-2" attributes="0"/>
                          </Group>
                      </Group>
                  </Group>
              </Group>
              <EmptySpace pref="30" max="32767" attributes="0"/>
              <Component id="btnSearch" min="-2" max="-2" attributes="0"/>
              <EmptySpace min="-2" pref="23" max="-2" attributes="0"/>
          </Group>
      </Group>
    </DimensionLayout>
    <DimensionLayout dim="1">
      <Group type="103" groupAlignment="0" attributes="0">
          <Group type="102" alignment="0" attributes="0">
              <EmptySpace min="-2" pref="21" max="-2" attributes="0"/>
              <Group type="103" groupAlignment="3" attributes="0">
                  <Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
                  <Component id="txtFullname" alignment="3" min="-2" max="-2" attributes="0"/>
                  <Component id="btnSearch" alignment="3" min="-2" max="-2" attributes="0"/>
              </Group>
              <EmptySpace type="separate" max="-2" attributes="0"/>
              <Group type="103" groupAlignment="3" attributes="0">
                  <Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
                  <Component id="txtRollno" alignment="3" min="-2" max="-2" attributes="0"/>
              </Group>
              <EmptySpace type="separate" max="-2" attributes="0"/>
              <Group type="103" groupAlignment="3" attributes="0">
                  <Component id="jLabel3" alignment="3" min="-2" max="-2" attributes="0"/>
                  <Component id="txtGender" alignment="3" min="-2" max="-2" attributes="0"/>
              </Group>
              <EmptySpace type="separate" max="-2" attributes="0"/>
              <Group type="103" groupAlignment="3" attributes="0">
                  <Component id="jLabel4" alignment="3" min="-2" max="-2" attributes="0"/>
                  <Component id="txtEmail" alignment="3" min="-2" max="-2" attributes="0"/>
              </Group>
              <EmptySpace type="separate" max="-2" attributes="0"/>
              <Group type="103" groupAlignment="3" attributes="0">
                  <Component id="jLabel5" alignment="3" min="-2" max="-2" attributes="0"/>
                  <Component id="txtAddress" alignment="3" min="-2" max="-2" attributes="0"/>
              </Group>
              <EmptySpace type="separate" max="-2" attributes="0"/>
              <Group type="103" groupAlignment="3" attributes="0">
                  <Component id="btnSave" alignment="3" min="-2" max="-2" attributes="0"/>
                  <Component id="btnReset" alignment="3" min="-2" max="-2" attributes="0"/>
              </Group>
              <EmptySpace pref="16" max="32767" attributes="0"/>
          </Group>
      </Group>
    </DimensionLayout>
  </Layout>
  <SubComponents>
    <Component class="javax.swing.JLabel" name="jLabel1">
      <Properties>
        <Property name="text" type="java.lang.String" value="Ho &amp; Ten:"/>
      </Properties>
    </Component>
    <Component class="javax.swing.JTextField" name="txtFullname">
    </Component>
    <Component class="javax.swing.JLabel" name="jLabel2">
      <Properties>
        <Property name="text" type="java.lang.String" value="MSV:"/>
      </Properties>
    </Component>
    <Component class="javax.swing.JTextField" name="txtRollno">
    </Component>
    <Component class="javax.swing.JLabel" name="jLabel3">
      <Properties>
        <Property name="text" type="java.lang.String" value="Gioi Tinh:"/>
      </Properties>
    </Component>
    <Component class="javax.swing.JLabel" name="jLabel4">
      <Properties>
        <Property name="text" type="java.lang.String" value="Email:"/>
      </Properties>
    </Component>
    <Component class="javax.swing.JTextField" name="txtEmail">
    </Component>
    <Component class="javax.swing.JLabel" name="jLabel5">
      <Properties>
        <Property name="text" type="java.lang.String" value="Dia Chi:"/>
      </Properties>
    </Component>
    <Component class="javax.swing.JTextField" name="txtAddress">
    </Component>
    <Component class="javax.swing.JButton" name="btnSearch">
      <Properties>
        <Property name="text" type="java.lang.String" value="Tim Kiem"/>
      </Properties>
      <Events>
        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnSearchActionPerformed"/>
      </Events>
    </Component>
    <Component class="javax.swing.JButton" name="btnSave">
      <Properties>
        <Property name="text" type="java.lang.String" value="Luu"/>
      </Properties>
      <Events>
        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnSaveActionPerformed"/>
      </Events>
    </Component>
    <Component class="javax.swing.JButton" name="btnReset">
      <Properties>
        <Property name="text" type="java.lang.String" value="Xoa Form"/>
      </Properties>
      <Events>
        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnResetActionPerformed"/>
      </Events>
    </Component>
    <Component class="javax.swing.JComboBox" name="txtGender">
      <Properties>
        <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
          <StringArray count="4">
            <StringItem index="0" value="-- Gioi Tinh --"/>
            <StringItem index="1" value="Nam"/>
            <StringItem index="2" value="Nu"/>
            <StringItem index="3" value="Khac"/>
          </StringArray>
        </Property>
      </Properties>
      <AuxValues>
        <AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
      </AuxValues>
    </Component>
  </SubComponents>
</Form>


#StudentForm.java


/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
 */
package com.gokisoft.lesson08;

import javax.swing.JOptionPane;

/**
 *
 * @author teacher
 */
public class StudentForm extends javax.swing.JFrame {
    Student std = null;
    
    /**
     * Creates new form StudentForm
     */
    public StudentForm() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtFullname = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtRollno = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        txtGender = new javax.swing.JComboBox<>();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Ho & Ten:");

        jLabel2.setText("MSV:");

        jLabel3.setText("Gioi Tinh:");

        jLabel4.setText("Email:");

        jLabel5.setText("Dia Chi:");

        btnSearch.setText("Tim Kiem");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnSave.setText("Luu");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnReset.setText("Xoa Form");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        txtGender.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "-- Gioi Tinh --", "Nam", "Nu", "Khac" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnSave)
                            .addGap(18, 18, 18)
                            .addComponent(btnReset))
                        .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(44, 44, 44)
                            .addComponent(txtRollno, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(44, 44, 44)
                            .addComponent(txtFullname, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3)
                                .addComponent(jLabel5))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(btnSearch)
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFullname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtRollno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnReset))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        if(std == null) {
            JOptionPane.showMessageDialog(rootPane, "Khong co ban ghi nao sua");
            return;
        }
        
        std.setFullname(txtFullname.getText());
        std.setRollno(txtRollno.getText());
        std.setAddress(txtAddress.getText());
        std.setEmail(txtEmail.getText());
        std.setGender(txtGender.getSelectedItem().toString());
        
        StudentEntity.getInstance().update(std);
        
        JOptionPane.showMessageDialog(rootPane, "cap nhat du lieu thanh cong");
        btnResetActionPerformed(evt);
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        txtFullname.setText("");
        txtEmail.setText("");
        txtRollno.setText("");
        txtAddress.setText("");
        txtGender.setSelectedIndex(0);
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        String rollno = txtRollno.getText();
        if(rollno.trim().isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Nhap MSV can tim kiem");
            return;
        }
        
        std = StudentEntity.getInstance().findByRollno(rollno);
        if(std == null) {
            JOptionPane.showMessageDialog(rootPane, "Khong tim thay sinh vien co msv = " + rollno);
            return;
        }
        
        txtFullname.setText(std.getFullname());
        txtEmail.setText(std.getEmail());
        txtGender.setSelectedItem(std.getGender());
        txtAddress.setText(std.getAddress());
    }//GEN-LAST:event_btnSearchActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StudentForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullname;
    private javax.swing.JComboBox<String> txtGender;
    private javax.swing.JTextField txtRollno;
    // End of variables declaration//GEN-END:variables
}


Tags:



Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó