Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.thelinh.gui; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; import static com.itextpdf.text.pdf.BaseFont.IDENTITY_H; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.thelinh.model.Answer; import com.thelinh.controller.Controller; import com.thelinh.controller.LoadTable; import com.thelinh.model.Question; import java.awt.event.ItemEvent; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.text.ParseException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.table.TableModel; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; /** * * @author Admin */ public class UpdateQuestion extends javax.swing.JFrame { int k = 1, q = 1; String sql = "SELECT Questions.*, Answers.Number, Answers.Answer,Answers.YesNo FROM Questions,Answers WHERE Questions.QuestionId = Answers.QuestionId"; String sqlQuestion = "SELECT * FROM Questions"; /** * Creates new form UpdateQuestion */ public UpdateQuestion() { initComponents(); LoadTable.loadDataQuestion(sql, tbQuestion); LoadTable.loadDataQuestions(sqlQuestion, tbQuestions); } /** * 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(); 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(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); txtQuestionId = new javax.swing.JTextField(); txtQuestionContent = new javax.swing.JTextField(); txtLevel = new javax.swing.JTextField(); txtSubjectId = new javax.swing.JTextField(); txtChapter = new javax.swing.JTextField(); txtAnswer = new javax.swing.JTextField(); txtAnswerContent = new javax.swing.JTextField(); txtCorrect = new javax.swing.JTextField(); btnAdd = new javax.swing.JButton(); btnAddFile = new javax.swing.JButton(); btnEdit = new javax.swing.JButton(); btnDelete = new javax.swing.JButton(); btnSearch = new javax.swing.JButton(); txtSearch = new javax.swing.JTextField(); jLabel10 = new javax.swing.JLabel(); cbQuestion = new javax.swing.JComboBox<>(); jScrollPane1 = new javax.swing.JScrollPane(); tbQuestion = new javax.swing.JTable(); jLabel11 = new javax.swing.JLabel(); btnAddQuestion = new javax.swing.JButton(); btnEditQuestion = new javax.swing.JButton(); btnDeleteQuestion = new javax.swing.JButton(); btnSearchQuestion = new javax.swing.JButton(); txtSearchQuestion = new javax.swing.JTextField(); jScrollPane2 = new javax.swing.JScrollPane(); tbQuestions = new javax.swing.JTable(); jLabel12 = new javax.swing.JLabel(); cbQuestions = new javax.swing.JComboBox<>(); btnAddFileQuestion = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); jLabel13 = new javax.swing.JLabel(); jLabel14 = new javax.swing.JLabel(); jLabel15 = new javax.swing.JLabel(); jLabel16 = new javax.swing.JLabel(); jLabel17 = new javax.swing.JLabel(); jLabel18 = new javax.swing.JLabel(); jLabel19 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jLabel1.setText("M cu h?i"); jLabel2.setText("Ni dung cu h?i"); jLabel3.setText("? kh"); jLabel4.setText("M mn h?c"); jLabel5.setText("Chng s:"); jLabel6.setText("?p n s :"); jLabel7.setText("Ni dung p n"); jLabel9.setText("?ng sai"); btnAdd.setText("Thm"); btnAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddActionPerformed(evt); } }); btnAddFile.setText("Thm file"); btnAddFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddFileActionPerformed(evt); } }); btnEdit.setText("Sa"); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); btnDelete.setText("Xa"); btnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteActionPerformed(evt); } }); btnSearch.setText("Tm kim"); btnSearch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSearchActionPerformed(evt); } }); jLabel10.setText("Tm kim theo"); cbQuestion.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "QuestionId", "QuestionContent", "Level", "SubjectId", "Answers", "None", " " })); cbQuestion.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cbQuestionItemStateChanged(evt); } }); cbQuestion.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cbQuestionActionPerformed(evt); } }); tbQuestion .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" })); tbQuestion.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tbQuestionMouseClicked(evt); } }); jScrollPane1.setViewportView(tbQuestion); jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel11.setText("Thm p n cho cu h?i"); btnAddQuestion.setText("Thm"); btnAddQuestion.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddQuestionActionPerformed(evt); } }); btnEditQuestion.setText("Sa"); btnEditQuestion.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditQuestionActionPerformed(evt); } }); btnDeleteQuestion.setText("Xa"); btnDeleteQuestion.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteQuestionActionPerformed(evt); } }); btnSearchQuestion.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N btnSearchQuestion.setText("Tm kim"); btnSearchQuestion.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSearchQuestionActionPerformed(evt); } }); tbQuestions .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" })); tbQuestions.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tbQuestionsMouseClicked(evt); } }); jScrollPane2.setViewportView(tbQuestions); jLabel12.setText("Tm kim theo"); cbQuestions.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "QuestionId", "QuestionContent", "Level", "SubjectId", "None", " " })); cbQuestions.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cbQuestionsItemStateChanged(evt); } }); btnAddFileQuestion.setText("Thm t file"); btnAddFileQuestion.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddFileQuestionActionPerformed(evt); } }); jButton1.setText("Xut ra PDF"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel13.setText("Tm kim Cu h?i"); jLabel14.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel14.setText("Bng cu h?i"); jLabel15.setText("Ni dung tm kim"); jLabel16.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel16.setText("Cu h?i"); jLabel17.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel17.setText("Tm kim cu tr l?i"); jLabel18.setText("Ni dung tm kim"); jLabel19.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel19.setText("Bng cu tr l?i"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel5).addComponent(jLabel9) .addComponent(jLabel7).addComponent(jLabel6))) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jLabel2)) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jLabel3)) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent( jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(8, 8, 8) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtCorrect).addComponent(txtAnswerContent) .addComponent(txtAnswer).addComponent(txtChapter).addComponent(txtSubjectId) .addComponent(txtLevel).addComponent(txtQuestionContent) .addComponent(txtQuestionId)) .addGap(21, 21, 21)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnAdd).addComponent(btnAddQuestion))) .addGroup(layout.createSequentialGroup().addGap(99, 99, 99).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnEdit).addComponent(btnEditQuestion)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnDelete) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnAddFile, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(btnDeleteQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10).addComponent(btnAddFileQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10).addComponent(jLabel17)) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel18).addComponent(jLabel10) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10) .addComponent(jLabel19))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtSearch) .addComponent(cbQuestion, 0, 166, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(20, 20, 20) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel11).addComponent(jLabel16)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel12).addComponent(jLabel15) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10) .addComponent(jLabel14))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtSearchQuestion) .addComponent(cbQuestions, 0, 204, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSearchQuestion)) .addComponent(jLabel13)).addGap(10, 10, 10)) .addGroup(layout.createSequentialGroup().addComponent(jButton1) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { btnAdd, btnAddQuestion, btnDelete, btnDeleteQuestion, btnEdit, btnEditQuestion }); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(8, 8, 8).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel13) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtSearchQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel15)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel12).addComponent(cbQuestions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(btnSearchQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel14).addGap(7, 7, 7) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1).addGap(107, 107, 107)) .addGroup(layout.createSequentialGroup().addComponent(jLabel16).addGap(1, 1, 1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(txtQuestionId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(txtQuestionContent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(5, 5, 5) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(txtLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(txtSubjectId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(txtChapter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnAddQuestion).addComponent(btnEditQuestion) .addComponent(btnDeleteQuestion).addComponent(btnAddFileQuestion)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel11) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6).addComponent(txtAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7).addComponent(txtAnswerContent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9).addComponent(txtCorrect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(14, 14, 14) .addComponent(jLabel8)) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnAdd).addComponent(btnEdit) .addComponent(btnDelete).addComponent(btnAddFile)))) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel17) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel18).addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel10).addComponent(cbQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(btnSearch, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel19) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void tbQuestionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbQuestionMouseClicked int row = tbQuestion.getSelectedRow(); String rowId = (tbQuestion.getModel().getValueAt(row, 0)).toString(); String rowId1 = (tbQuestion.getModel().getValueAt(row, 5)).toString(); String sql1 = "SELECT Questions.*, Answers.Number, Answers.Answer,Answers.YesNo " + "FROM Questions,Answers " + "WHERE Questions.QuestionId = Answers.QuestionID " + "AND Questions.QuestionId = '" + rowId + "' " + "AND Answers.Number = " + rowId1; ResultSet rs = LoadTable.Display(sql1); try { if (rs.next()) { txtQuestionId.setText(rs.getString("QuestionId")); txtQuestionContent.setText(rs.getString("Question")); txtLevel.setText(rs.getString("Levels")); txtSubjectId.setText(rs.getString("SubjectId")); txtChapter.setText(rs.getString("Chapter")); txtAnswer.setText(rs.getString("Number")); txtAnswerContent.setText(rs.getString("Answer")); txtCorrect.setText(rs.getString("YesNo")); } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "ERROR"); } }//GEN-LAST:event_tbQuestionMouseClicked private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed String sql1 = "SELECT Questions.*, Answers.Number, Answers.Answer,Answers.YesNo FROM Questions,Answers WHERE Questions.QuestionId = Answers.QuestionId"; String sql2 = "SELECT Questions.*, Answers.Number, Answers.Answer,Answers.YesNo FROM Questions,Answers WHERE Questions.QuestionId = Answers.QuestionId AND Questions.QuestionId LIKE '%" + txtSearch.getText() + "%'"; String sql3 = "SELECT Questions.*, Answers.Number, Answers.Answer,Answers.YesNo FROM Questions,Answers WHERE Questions.QuestionId = Answers.QuestionId AND Questions.Question LIKE '%" + txtSearch.getText() + "%'"; String sql4 = "SELECT Questions.*, Answers.Number, Answers.Answer,Answers.YesNo FROM Questions,Answers WHERE Questions.QuestionId = Answers.QuestionId AND Questions.Levels LIKE '%" + txtSearch.getText() + "%'"; String sql5 = "SELECT Questions.*, Answers.Number, Answers.Answer,Answers.YesNo FROM Questions,Answers WHERE Questions.QuestionId = Answers.QuestionId AND Questions.SubjectId LIKE '%" + txtSearch.getText() + "%'"; String sql6 = "SELECT Questions.*, Answers.Number, Answers.Answer,Answers.YesNo FROM Questions,Answers WHERE Questions.QuestionId = Answers.QuestionId AND Answers.Answer LIKE '%" + txtSearch.getText() + "%'"; switch (q) { case 1: LoadTable.loadDataQuestion(sql2, tbQuestion); break; case 2: LoadTable.loadDataQuestion(sql3, tbQuestion); break; case 3: LoadTable.loadDataQuestion(sql4, tbQuestion); break; case 4: LoadTable.loadDataQuestion(sql5, tbQuestion); break; case 5: LoadTable.loadDataQuestions(sql6, tbQuestion); break; case 6: LoadTable.loadDataQuestion(sql1, tbQuestion); break; } }//GEN-LAST:event_btnSearchActionPerformed private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed if (txtQuestionId.getText().length() == 0) { JOptionPane.showMessageDialog(null, "You have to enter QuestionId", "Notification", JOptionPane.INFORMATION_MESSAGE); } else if (txtQuestionId.getText().length() > 10) { JOptionPane.showMessageDialog(null, "QuestionId have to less more than 10 characters", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { Answer answer = new Answer(txtQuestionId.getText(), txtAnswerContent.getText(), Boolean.parseBoolean(txtCorrect.getText()), Integer.parseInt(txtAnswer.getText())); Controller.insertAnswer(answer); btnSearch.doClick(); } }//GEN-LAST:event_btnAddActionPerformed private void tbQuestionsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbQuestionsMouseClicked int row = tbQuestions.getSelectedRow(); String rowId = (tbQuestions.getModel().getValueAt(row, 0)).toString(); String sql1 = "SELECT * FROM Questions WHERE QuestionId = '" + rowId + "'"; ResultSet rs = LoadTable.Display(sql1); try { if (rs.next()) { txtQuestionId.setText(rs.getString("QuestionId")); txtQuestionContent.setText(rs.getString("Question")); txtLevel.setText(rs.getString("Levels")); txtSubjectId.setText(rs.getString("SubjectId")); txtChapter.setText(rs.getString("Chapter")); } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "ERROR"); } }//GEN-LAST:event_tbQuestionsMouseClicked private void cbQuestionsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbQuestionsItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "QuestionId") { k = 1; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "QuestionContent") { k = 2; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "Level") { k = 3; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "SubjectId") { k = 4; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "None") { k = 5; } }//GEN-LAST:event_cbQuestionsItemStateChanged private void btnAddQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddQuestionActionPerformed if (txtQuestionId.getText().length() == 0) { JOptionPane.showMessageDialog(null, "You have to enter QuestionId", "Notification", JOptionPane.INFORMATION_MESSAGE); } else if (txtQuestionId.getText().length() > 10) { JOptionPane.showMessageDialog(null, "QuestionId have to less more than 10 characters", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { String sql = "SELECT QuestionId FROM Questions"; ResultSet rs = LoadTable.Display(sql); try { while (rs.next()) { if (rs.getString("QuestionId") == txtQuestionId.getText()) { JOptionPane.showMessageDialog(null, "This QuestionId already exists", "Notification", JOptionPane.INFORMATION_MESSAGE); } } // xu li chua chuan lam! :( Question question = new Question(txtQuestionId.getText(), txtQuestionContent.getText(), txtLevel.getText(), txtSubjectId.getText(), Integer.parseInt(txtChapter.getText())); Controller.insertQuestion(question); btnSearchQuestion.doClick(); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "ERROR"); } } }//GEN-LAST:event_btnAddQuestionActionPerformed private void btnSearchQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchQuestionActionPerformed String sql1 = "SELECT * FROM Questions"; String sql2 = "SELECT * FROM Questions WHERE QuestionId LIKE '%" + txtSearchQuestion.getText() + "%'"; String sql3 = "SELECT * FROM Questions WHERE Question LIKE '%" + txtSearchQuestion.getText() + "%'"; String sql4 = "SELECT * FROM Questions WHERE Levels LIKE '%" + txtSearchQuestion.getText() + "%'"; String sql5 = "SELECT * FROM Questions WHERE SubjectId LIKE '%" + txtSearchQuestion.getText() + "%'"; switch (k) { case 1: LoadTable.loadDataQuestions(sql2, tbQuestions); break; case 2: LoadTable.loadDataQuestions(sql3, tbQuestions); break; case 3: LoadTable.loadDataQuestions(sql4, tbQuestions); break; case 4: LoadTable.loadDataQuestions(sql5, tbQuestions); break; case 5: LoadTable.loadDataQuestions(sql1, tbQuestions); break; } }//GEN-LAST:event_btnSearchQuestionActionPerformed private void btnEditQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditQuestionActionPerformed if (txtQuestionId.getText().length() == 0) { JOptionPane.showMessageDialog(null, "You have to enter QuestionId", "Notification", JOptionPane.INFORMATION_MESSAGE); } else if (txtQuestionId.getText().length() > 10) { JOptionPane.showMessageDialog(null, "QuestionId have to less more than 10 characters", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { int click = JOptionPane.showConfirmDialog(null, "Do you want to edit?"); if (click == JOptionPane.YES_OPTION) { Question question = new Question(txtQuestionId.getText(), txtQuestionContent.getText(), txtLevel.getText(), txtSubjectId.getText(), Integer.parseInt(txtChapter.getText())); if (Controller.updateQuestion(question)) { JOptionPane.showMessageDialog(null, "Edit Success", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "ERROR", "ERROR", JOptionPane.ERROR_MESSAGE); } } btnSearchQuestion.doClick(); } }//GEN-LAST:event_btnEditQuestionActionPerformed private void btnAddFileQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFileQuestionActionPerformed JFileChooser jfc = new JFileChooser(); if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { jfc.setDialogTitle("Open File"); File file = jfc.getSelectedFile(); try { Workbook wb = Workbook.getWorkbook(file); Sheet sheet = wb.getSheet(0); int rows = sheet.getRows(); int columns = sheet.getColumns(); for (int i = 0; i < rows; i++) { Question question = new Question(sheet.getCell(0, i).getContents(), sheet.getCell(1, i).getContents(), sheet.getCell(2, i).getContents(), sheet.getCell(3, i).getContents(), Integer.parseInt(sheet.getCell(4, i).getContents())); Controller.insertQuestion(question); } wb.close(); btnSearchQuestion.doClick(); } catch (IOException ex) { System.out.println("File not found\n" + ex.toString()); } catch (BiffException ex) { ex.printStackTrace(); } } }//GEN-LAST:event_btnAddFileQuestionActionPerformed private void btnDeleteQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteQuestionActionPerformed if (txtQuestionId.getText().length() == 0) { JOptionPane.showMessageDialog(null, "You have to enter QuestionId", "Notification", JOptionPane.INFORMATION_MESSAGE); } else if (txtQuestionId.getText().length() > 10) { JOptionPane.showMessageDialog(null, "QuestionId have to less more than 10 characters", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { int click = JOptionPane.showConfirmDialog(null, "Do you want to delete?"); if (click == JOptionPane.YES_OPTION) { if (Controller.deleteQuestion(txtQuestionId.getText())) { JOptionPane.showMessageDialog(null, "Delete Success", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "QuestionId does not exist", "ERROR", JOptionPane.ERROR_MESSAGE); } btnSearchQuestion.doClick(); } } }//GEN-LAST:event_btnDeleteQuestionActionPerformed private void cbQuestionItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbQuestionItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "QuestionId") { q = 1; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "QuestionContent") { q = 2; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "Level") { q = 3; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "SubjectId") { q = 4; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "Answer") { q = 5; } if (evt.getStateChange() == ItemEvent.SELECTED && evt.getItem().toString() == "None") { q = 6; } }//GEN-LAST:event_cbQuestionItemStateChanged private void btnAddFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFileActionPerformed JFileChooser jfc = new JFileChooser(); if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { jfc.setDialogTitle("Open File"); File file = jfc.getSelectedFile(); try { Workbook wb = Workbook.getWorkbook(file); Sheet sheet = wb.getSheet(0); int rows = sheet.getRows(); int columns = sheet.getColumns(); System.out.println("Rows: " + rows); System.out.println("Columns: " + columns); for (int i = 0; i < rows; i++) { Answer answer = new Answer(sheet.getCell(0, i).getContents(), sheet.getCell(1, i).getContents(), Boolean.parseBoolean(sheet.getCell(2, i).getContents()), Integer.parseInt(sheet.getCell(3, i).getContents())); Controller.insertAnswer(answer); } wb.close(); btnSearch.doClick(); } catch (IOException ex) { System.out.println("File not found\n" + ex.toString()); } catch (BiffException ex) { ex.printStackTrace(); } } }//GEN-LAST:event_btnAddFileActionPerformed private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed if (txtQuestionId.getText().length() == 0) { JOptionPane.showMessageDialog(null, "You have to enter QuestionId", "Notification", JOptionPane.INFORMATION_MESSAGE); } else if (txtQuestionId.getText().length() > 10) { JOptionPane.showMessageDialog(null, "QuestionId have to less more than 10 characters", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { int click = JOptionPane.showConfirmDialog(null, "Do you want to edit?"); if (click == JOptionPane.YES_OPTION) { Answer answer = new Answer(txtQuestionId.getText(), txtAnswerContent.getText(), Boolean.parseBoolean(txtCorrect.getText()), Integer.parseInt(txtAnswer.getText())); if (Controller.updateAnswer(answer)) { JOptionPane.showMessageDialog(null, "Edit Success", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "ERROR", "ERROR", JOptionPane.ERROR_MESSAGE); } } btnSearch.doClick(); } }//GEN-LAST:event_btnEditActionPerformed private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed if (txtQuestionId.getText().length() == 0) { JOptionPane.showMessageDialog(null, "You have to enter QuestionId", "Notification", JOptionPane.INFORMATION_MESSAGE); } else if (txtQuestionId.getText().length() > 10) { JOptionPane.showMessageDialog(null, "QuestionId have to less more than 10 characters", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { int click = JOptionPane.showConfirmDialog(null, "Do you want to delete?"); if (click == JOptionPane.YES_OPTION) { if (Controller.deleteAnswer(txtQuestionId.getText(), Integer.parseInt(txtAnswer.getText()))) { JOptionPane.showMessageDialog(null, "Delete Success", "Notification", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "QuestionId does not exist", "ERROR", JOptionPane.ERROR_MESSAGE); } btnSearchQuestion.doClick(); } } }//GEN-LAST:event_btnDeleteActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed Document document = new Document() { }; try { JFileChooser jfc = new JFileChooser("Save File"); if (jfc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { jfc.setDialogTitle("Save File"); FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile()); PdfWriter.getInstance(document, fos); document.open(); Font rfont = FontFactory.getFont("C:\\Windows\\Fonts\\Calibri.ttf", IDENTITY_H, true); document.add(new Paragraph( " TRNG ?I HC B?CH KHOA H NI", rfont)); document.add(new Paragraph( "\t\t KT QU TM KIM CU HI\n", rfont)); switch (k) { case 1: document.add(new Paragraph( " Tm kim theo QuestionId : " + txtSearch.getText() + "\n\n", rfont)); break; case 2: document.add(new Paragraph(" Tm kim theo ni dung cu h?i : " + txtSearch.getText() + "\n\n", rfont)); break; case 3: document.add(new Paragraph( " Tm kim theo level : " + txtSearch.getText() + "\n\n", rfont)); break; case 4: document.add(new Paragraph( " Tm kim theo m mn h?c : " + txtSearch.getText() + "\n\n", rfont)); break; case 5: document.add(new Paragraph( " Tm kim theo cu tr l?i : " + txtSearch.getText() + "\n\n", rfont)); break; } TableModel tableModel = tbQuestion.getModel(); int question = 1; for (int i = 0; i < tableModel.getRowCount(); i++) { String id = (String) tableModel.getValueAt(i, 0); document.add(new Paragraph("\nCu " + question + " : ", rfont)); document.add(new Paragraph((String) tableModel.getValueAt(i, 1) + "\n\n", rfont)); PdfPTable table = new PdfPTable(3); PdfPCell header1 = new PdfPCell(new Paragraph("?p n s", rfont)); PdfPCell header2 = new PdfPCell(new Paragraph("Ni dung p n", rfont)); PdfPCell header3 = new PdfPCell(new Paragraph("?ng sai", rfont)); table.addCell(header1); table.addCell(header2); table.addCell(header3); String id1 = id; char answer = 'A'; while (id.equals(id1) && i < tableModel.getRowCount()) { table.addCell(new PdfPCell(new Paragraph("" + answer))); table.addCell(new PdfPCell(new Paragraph((String) tableModel.getValueAt(i, 6), rfont))); table.addCell( new PdfPCell(new Paragraph(String.valueOf(tableModel.getValueAt(i, 7)), rfont))); i++; if (i < tableModel.getRowCount()) id1 = (String) tableModel.getValueAt(i, 0); answer++; } document.add(table); question++; } document.add(new Paragraph( "\n Ha Noi, November 4th, 2016\n", rfont)); document.add(new Paragraph( " Teacher\n", rfont)); document.add(new Paragraph( " (Signed and Sealed)\n", rfont)); document.close(); JOptionPane.showMessageDialog(null, "Save success"); } } catch (FileNotFoundException ex) { Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex); } catch (DocumentException ex) { Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton1ActionPerformed private void cbQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbQuestionActionPerformed // TODO add your handling code here: }//GEN-LAST:event_cbQuestionActionPerformed /** * @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(UpdateQuestion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(UpdateQuestion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(UpdateQuestion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(UpdateQuestion.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 UpdateQuestion().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAdd; private javax.swing.JButton btnAddFile; private javax.swing.JButton btnAddFileQuestion; private javax.swing.JButton btnAddQuestion; private javax.swing.JButton btnDelete; private javax.swing.JButton btnDeleteQuestion; private javax.swing.JButton btnEdit; private javax.swing.JButton btnEditQuestion; private javax.swing.JButton btnSearch; private javax.swing.JButton btnSearchQuestion; private javax.swing.JComboBox<String> cbQuestion; private javax.swing.JComboBox<String> cbQuestions; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel15; private javax.swing.JLabel jLabel16; private javax.swing.JLabel jLabel17; private javax.swing.JLabel jLabel18; private javax.swing.JLabel jLabel19; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable tbQuestion; private javax.swing.JTable tbQuestions; private javax.swing.JTextField txtAnswer; private javax.swing.JTextField txtAnswerContent; private javax.swing.JTextField txtChapter; private javax.swing.JTextField txtCorrect; private javax.swing.JTextField txtLevel; private javax.swing.JTextField txtQuestionContent; private javax.swing.JTextField txtQuestionId; private javax.swing.JTextField txtSearch; private javax.swing.JTextField txtSearchQuestion; private javax.swing.JTextField txtSubjectId; // End of variables declaration//GEN-END:variables }