Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
package qlsach;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author USER
 */
public final class frmQuanLySach extends javax.swing.JFrame {
 DbConnect con;
 private int flag = 0;
    /**
     * Creates new form frmQuanLySach
     * @throws java.sql.SQLException
     */
    public frmQuanLySach() throws SQLException {
       
        con = new DbConnect();
        showData();
    }
    public void ClearText(){
        txtMaSach.setText("");
        txtTenSach.setText("");
        rbTieuThuyet.setSelected(true);
        txtNXB.setText("");
        cbSoTrang.setSelectedIndex(0);
        txtGiaTien.setText("");
    }
    public void showData(){
        String[] columnNames = { "Ma Sach","Ten Sach","The Loai","NXB","So Trang","Gia Tien"};
        DefaultTableModel model = new DefaultTableModel();
        model.setColumnIdentifiers(columnNames);
        tblSach.setModel(model);
        String MaSach = "";
        String TenSach = "";
        String TheLoai = "";
        String NXB = "";
        String SoTrang = "";
        String GiaTien = "";
        
        ResultSet rs = (ResultSet) con.getData("SELECT * FROM Sach");
        while (rs.next()){
            MaSach = rs.getString("MaSach");
            TenSach = rs.getString("TenSach");
            TheLoai = rs.getString("TheLoai");
            NXB = rs.getString("NXB");
            SoTrang = rs.getString("SoTrang");
            GiaTien = rs.getString("GiaTien");
        }
        model.addRow(new Object[]{MaSach, TenSach, TheLoai, NXB, SoTrang, GiaTien});
    }
    public void InsertData(){
        String TheLoai="";
        ButtonModel buttonModel = buttonGroup1.getselection();
        if(rbTieuThuyet.isSelected()){
            TheLoai = rbTieuThuyet.getText();
        }
        else if(rbToanHoc.isSelected()){
         TheLoai = rbToanHoc.getText();
        }
        else{
            TheLoai = rbVanHoc.getText();
        }
        String SoTrang = "";
        SoTrang = cbSoTrang.getSelectedItem().toString();
        String[]stringsSQL = {txtTenSach.getText() , TheLoai , txtNXB.getText(), SoTrang, txtGiaTien.getText(),txtMaSach.getText()};
        int isInsert = con.ExcuteSQLInsert(stringsSQL);
        if(isInsert > 0){
            JOptionPane.showMessageDialog(this,"Bạn Đã Thêm Dữ Liệu Thành Cônng");
        }
        else{
            JOptionPane.showMessageDialog(this,"Bạn Chưa Thêm Được Dữ Liệu");      
        }
        showData();
        ClearText();
        
    }
    public void UpdateData(){
        String TheLoai="";
        ButtonModel buttonModel = buttonGroup1.getselection();
        if(rbTieuThuyet.isSelected()){
            TheLoai = rbTieuThuyet.getText();
        }
        else if(rbToanHoc.isSelected()){
         TheLoai = rbToanHoc.getText();
        }
        else{
            TheLoai = rbVanHoc.getText();
        }
        String SoTrang = "";
        SoTrang = cbSoTrang.getSelectedItem().toString();
        String[]stringsSQL = { txtMaSach.getText(), txtTenSach.getText() , TheLoai , txtNXB.getText(), SoTrang, txtGiaTien.getText()};
        int isUpdate = con.ExcuteSQLUpdate(stringsSQL);
        if(isUpdate > 0){
            JOptionPane.showMessageDialog(this,"Bạn Đã Sửa Dữ Liệu Thành Cônng");
        }
        else{
            JOptionPane.showMessageDialog(this,"Bạn Chưa Sửa Được Dữ Liệu");      
        }
        showData();
        ClearText();
        
    }
      public void DeleteData(){
        String[]stringsSQL = { txtMaSach.getText()};
        int isDelete = con.ExcuteSQLDelete(stringsSQL);
        if(isDelete > 0){
            JOptionPane.showMessageDialog(this,"Bạn Đã Xoá Dữ Liệu Thành Cônng");
        }
        else{
            JOptionPane.showMessageDialog(this,"Bạn Chưa Xóa Được Dữ Liệu");      
        }
        showData();
        ClearText();
        
    }
      
      public void getSelectedData(){
          int selectedRow = tblSach.getSelectedRow();
          
          txtMaSach.setText(tblSach.getValueAt(selectedRow, 0).toString());
          txtTenSach.setText(tblSach.getValueAt(selectedRow, 1).toString());
     switch (tblSach.getValueAt(selectedRow, 2).toString()) {
         case "Tiểu Thuyết":
             rbTieuThuyet.setSelected(true);
             break;
         case "Toán Học":
             rbToanHoc.setSelected(true);
             break;
         default:
             rbVanHoc.setSelected(true);
             break;
     }
                  
          txtNXB.setText(tblSach.getValueAt(selectedRow, 3).toString());
          cbSoTrang.setSelectedItem(tblSach.getValueAt(selectedRow, 4));
          txtGiaTien.setText(tblSach.getValueAt(selectedRow, 5).toString());
          
      }
      public void getSelecttrangdau(){
          int selectedRow = 0;
          txtMaSach.setText(tblSach.getValueAt(selectedRow, 0).toString());
          txtTenSach.setText(tblSach.getValueAt(selectedRow, 1).toString());
     switch (tblSach.getValueAt(selectedRow, 2).toString()) {
         case "Tiểu Thuyết":
             rbTieuThuyet.setSelected(true);
             break;
         case "Toán Học":
             rbToanHoc.setSelected(true);
             break;
         default:
             rbVanHoc.setSelected(true);
             break;
     }
          txtNXB.setText(tblSach.getValueAt(selectedRow, 3).toString());
          cbSoTrang.setSelectedItem(tblSach.getValueAt(selectedRow, 4).toString());
          txtGiaTien.setText(tblSach.getValueAt(selectedRow, 5).toString()); 
          tblSach.setRowSelectionInterval(0, 0);
      }
      public void getSelecttrangtruoc(){
          if(tblSach.getSelectedRow() >=1){
              int selectedRow = tblSach.getSelectedRow()-1;
               txtMaSach.setText(tblSach.getValueAt(selectedRow, 0).toString());
          txtTenSach.setText(tblSach.getValueAt(selectedRow, 1).toString());
              switch (tblSach.getValueAt(selectedRow, 2).toString()) {
                  case "Tiểu Thuyết":
                      rbTieuThuyet.setSelected(true);
                      break;
                  case "Toán Học":
                      rbToanHoc.setSelected(true);
                      break;
                  default:
                      rbVanHoc.setSelected(true);
                      break;
              }
          txtNXB.setText(tblSach.getValueAt(selectedRow, 3).toString());
          cbSoTrang.setSelectedItem(tblSach.getValueAt(selectedRow, 4).toString());
          txtGiaTien.setText(tblSach.getValueAt(selectedRow, 5).toString()); 
          tblSach.setRowSelectionInterval(selectedRow, selectedRow);
          }
      }
      public void getSelecttrangtieptheo(){
          if(tblSach.getSelectedRow() < tblSach.getRowCount()-1){
          int selectedRow = tblSach.getSelectedRow()-1;
          txtMaSach.setText(tblSach.getValueAt(selectedRow, 0).toString());
          txtTenSach.setText(tblSach.getValueAt(selectedRow, 1).toString());
              switch (tblSach.getValueAt(selectedRow, 2).toString()) {
                  case "Tiểu Thuyết":
                      rbTieuThuyet.setSelected(true);
                      break;
                  case "Toán Học":
                      rbToanHoc.setSelected(true);
                      break;
                  default:
                      rbVanHoc.setSelected(true);
                      break;
              }
          txtNXB.setText(tblSach.getValueAt(selectedRow, 3).toString());
          cbSoTrang.setSelectedItem(tblSach.getValueAt(selectedRow, 4).toString());
          txtGiaTien.setText(tblSach.getValueAt(selectedRow, 5).toString()); 
          tblSach.setRowSelectionInterval(selectedRow, selectedRow);
      }
     }
          public void getSelecttrangcuoi(){
          int selectedRow = tblSach.getRowCount()-1;
          txtMaSach.setText(tblSach.getValueAt(selectedRow, 0).toString());
          txtTenSach.setText(tblSach.getValueAt(selectedRow, 1).toString());
     switch (tblSach.getValueAt(selectedRow, 2).toString()) {
         case "Tiểu Thuyết":
             rbTieuThuyet.setSelected(true);
             break;
         case "Toán Học":
             rbToanHoc.setSelected(true);
             break;
         default:
             rbVanHoc.setSelected(true);
             break;
     }
          txtNXB.setText(tblSach.getValueAt(selectedRow, 3).toString());
          cbSoTrang.setSelectedItem(tblSach.getValueAt(selectedRow, 4).toString());
          txtGiaTien.setText(tblSach.getValueAt(selectedRow, 5).toString()); 
          tblSach.setRowSelectionInterval(selectedRow, selectedRow);
          }
      
    /**
     * 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">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtMaSach = new javax.swing.JTextField();
        txtTenSach = new javax.swing.JTextField();
        txtNXB = new javax.swing.JTextField();
        txtGiaTien = new javax.swing.JTextField();
        cbSoTrang = new javax.swing.JComboBox<>();
        rbTieuThuyet = new javax.swing.JRadioButton();
        rbToanHoc = new javax.swing.JRadioButton();
        btntrangdau = new javax.swing.JButton();
        btntrangtruoc = new javax.swing.JButton();
        btntrangtt = new javax.swing.JButton();
        btntrangcuoi = new javax.swing.JButton();
        btnluu = new javax.swing.JButton();
        btnxoa = new javax.swing.JButton();
        btncapnhap = new javax.swing.JButton();
        btnthem = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSach = new javax.swing.JTable();
        rbVanHoc = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("THÔNG TIN QUẢN LÝ SÁCH");

        jLabel2.setText("Mã Sách:");

        jLabel3.setText("Tên Sách:");

        jLabel4.setText("Thể Loại:");

        jLabel5.setText("NXB:");

        jLabel6.setText("Số Trang:");

        jLabel7.setText("Giá Tiền:");

        cbSoTrang.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5", "6", "7" }));

        rbTieuThuyet.setText("Tiểu Thuyết");

        rbToanHoc.setText("Toán Học");

        btntrangdau.setText("Trang Đầu");
        btntrangdau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntrangdauActionPerformed(evt);
            }
        });

        btntrangtruoc.setText("Trang Trước");
        btntrangtruoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntrangtruocActionPerformed(evt);
            }
        });

        btntrangtt.setText("Trang Tiếp Theo");
        btntrangtt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntrangttActionPerformed(evt);
            }
        });

        btntrangcuoi.setText("Trang Cuối");
        btntrangcuoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntrangcuoiActionPerformed(evt);
            }
        });

        btnluu.setText("Lưu");
        btnluu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnluuActionPerformed(evt);
            }
        });

        btnxoa.setText("Xóa");
        btnxoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnxoaActionPerformed(evt);
            }
        });

        btncapnhap.setText("Cập Nhập");
        btncapnhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncapnhapActionPerformed(evt);
            }
        });

        btnthem.setText("Thêm");
        btnthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthemActionPerformed(evt);
            }
        });

        tblSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblSach.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSachMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblSach);

        rbVanHoc.setText("Văn Học");

        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(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(33, 33, 33)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(123, 123, 123)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                    .addComponent(txtNXB)
                                                    .addComponent(txtGiaTien)
                                                    .addComponent(cbSoTrang, 0, 356, Short.MAX_VALUE)))
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(28, 28, 28)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jLabel1)
                                                        .addGap(105, 105, 105))
                                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                            .addGroup(layout.createSequentialGroup()
                                                                .addComponent(btntrangtruoc, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(35, 35, 35)
                                                                .addComponent(btntrangtt))
                                                            .addGroup(layout.createSequentialGroup()
                                                                .addComponent(btnxoa, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                .addComponent(btncapnhap, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                        .addGap(49, 49, 49)))))
                                        .addComponent(txtTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(txtMaSach, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(154, 154, 154)
                                .addComponent(rbVanHoc)
                                .addGap(18, 18, 18)
                                .addComponent(rbTieuThuyet)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                                .addComponent(rbToanHoc))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btntrangdau, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnthem, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btntrangcuoi, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                            .addComponent(btnluu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jLabel1)
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtMaSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(rbTieuThuyet)
                            .addComponent(rbToanHoc)
                            .addComponent(rbVanHoc))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtNXB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(cbSoTrang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(txtGiaTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(57, 57, 57)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btntrangdau)
                            .addComponent(btntrangtruoc)
                            .addComponent(btntrangtt)
                            .addComponent(btntrangcuoi))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnluu)
                            .addComponent(btnxoa)
                            .addComponent(btncapnhap)
                            .addComponent(btnthem))))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void btntrangdauActionPerformed(java.awt.event.ActionEvent evt) {                                            
      getSelecttrangdau();
    }                                           

    private void btntrangtruocActionPerformed(java.awt.event.ActionEvent evt) {                                              
      getSelecttrangtruoc();
    }                                             

    private void btntrangttActionPerformed(java.awt.event.ActionEvent evt) {                                           
       getSelecttrangtieptheo();
    }                                          

    private void btntrangcuoiActionPerformed(java.awt.event.ActionEvent evt) {                                             
        getSelecttrangcuoi();
    }                                            

    private void btnthemActionPerformed(java.awt.event.ActionEvent evt) {                                        
       ClearText();
       btnluu.setEnabled(true);
       flag = 1;
       btnthem.setEnabled(false);
       btncapnhap.setEnabled(false);
       btnxoa.setEnabled(false);
    }                                       

    private void btncapnhapActionPerformed(java.awt.event.ActionEvent evt) {                                           
       ClearText();
       btnluu.setEnabled(true);
       flag = 2;
       btnthem.setEnabled(false);
       btncapnhap.setEnabled(false);
       btnxoa.setEnabled(false);
    }                                          

    private void btnxoaActionPerformed(java.awt.event.ActionEvent evt) {                                       
       ClearText();
       btnluu.setEnabled(true);
       flag = 3;
       btnthem.setEnabled(false);
       btncapnhap.setEnabled(false);
       btnxoa.setEnabled(false);
    }                                      

    private void btnluuActionPerformed(java.awt.event.ActionEvent evt) {                                       
       if (flag == 1){
           InsertData();
           btnthem.setEnabled(true);
           btncapnhap.setEnabled(true);
           btnxoa.setEnabled(true);
       }
        if (flag == 1){
           UpdateData();
           btnthem.setEnabled(true);
           btncapnhap.setEnabled(true);
           btnxoa.setEnabled(true);
       }
       else{
           DeleteData();
           btnthem.setEnabled(true);
           btncapnhap.setEnabled(true);
           btnxoa.setEnabled(true);
       }
       ClearText();
       btnluu.setEnabled(false);
    }                                      

    private void tblSachMouseClicked(java.awt.event.MouseEvent evt) {                                     
        getSelectedData();
    }                                    

    /**
     * @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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmQuanLySach.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
     //</editor-fold>
     
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            try {
                new frmQuanLySach().setVisible(true);
            } catch (SQLException ex) {
                Logger.getLogger(frmQuanLySach.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
    }


What I have tried:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at qlsach.frmQuanLySach.showData(frmQuanLySach.java:43)
	at qlsach.frmQuanLySach.<init>(frmQuanLySach.java:29)
	at qlsach.frmQuanLySach.lambda$main$0(frmQuanLySach.java:585)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Posted
Updated 24-Sep-20 20:04pm

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Mostly, it seems a repost[^].

You need to understand what this error means.

Reference: How to resolve the java.lang.NullPointerException[^]
Quote:
In Java, the java.lang.NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.


Your case, error and line of code is pretty clear:
i.e. while accessing data for method qlsach.frmQuanLySach.showData(frmQuanLySach.java:43)


Debug and see why that is null. Set it up correctly and handle null condition. If it is still the connection variable issue, solve it.


If needed, look:
jdb - The Java Debugger[^]
Debugging the Eclipse IDE for Java Developers | The Eclipse Foundation[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900