SHARE
TWEET

Untitled

a guest Jun 18th, 2017 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package myjava;
  2. import java.sql.*;
  3.  
  4. public class GuiInsert extends javax.swing.JFrame {
  5.     private Connection conn;
  6.     public GuiInsert() {
  7.         initComponents();
  8.         try
  9.         {
  10.         Class.forName("com.mysql.jdbc.Driver");
  11.         conn = DriverManager.getConnection ("jdbc:mysql://localhost/my3csd","root","12345");
  12.         statusTextArea.setText("Connection SUCCESSFUL!");
  13.         }
  14.         catch (Exception e)
  15.         {
  16.             statusTextArea.setText( e.getMessage());
  17.         }
  18.     }
  19.  
  20.     @SuppressWarnings("unchecked")
  21.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  22.     private void initComponents() {
  23.  
  24.         jPanel1 = new javax.swing.JPanel();
  25.         sectionCombo = new javax.swing.JComboBox();
  26.         studnoField = new javax.swing.JTextField();
  27.         nameField = new javax.swing.JTextField();
  28.         tuitionField = new javax.swing.JTextField();
  29.         birthdayField = new javax.swing.JTextField();
  30.         jLabel1 = new javax.swing.JLabel();
  31.         jLabel2 = new javax.swing.JLabel();
  32.         jLabel3 = new javax.swing.JLabel();
  33.         jLabel4 = new javax.swing.JLabel();
  34.         jLabel5 = new javax.swing.JLabel();
  35.         jPanel3 = new javax.swing.JPanel();
  36.         jScrollPane1 = new javax.swing.JScrollPane();
  37.         statusTextArea = new javax.swing.JTextArea();
  38.         jPanel2 = new javax.swing.JPanel();
  39.         saveButton = new javax.swing.JButton();
  40.  
  41.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  42.         setTitle("Insert a Record");
  43.         addWindowListener(new java.awt.event.WindowAdapter() {
  44.             public void windowClosing(java.awt.event.WindowEvent evt) {
  45.                 formWindowClosing(evt);
  46.             }
  47.         });
  48.  
  49.         jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Data", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
  50.  
  51.         sectionCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-", "3CSA", "3CSB", "3ISC", "3ISD", "3ISA" }));
  52.         sectionCombo.addActionListener(new java.awt.event.ActionListener() {
  53.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  54.                 sectionComboActionPerformed(evt);
  55.             }
  56.         });
  57.  
  58.         jLabel1.setText("Student Number");
  59.  
  60.         jLabel2.setText("Name");
  61.  
  62.         jLabel3.setText("Section");
  63.  
  64.         jLabel4.setText("Tuition");
  65.  
  66.         jLabel5.setText("Birthday (YYYY-MM-DD))");
  67.  
  68.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  69.         jPanel1.setLayout(jPanel1Layout);
  70.         jPanel1Layout.setHorizontalGroup(
  71.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  72.             .addGroup(jPanel1Layout.createSequentialGroup()
  73.                 .addGap(20, 20, 20)
  74.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75.                     .addComponent(jLabel1)
  76.                     .addComponent(jLabel2)
  77.                     .addComponent(jLabel3)
  78.                     .addComponent(jLabel4)
  79.                     .addComponent(jLabel5))
  80.                 .addGap(28, 28, 28)
  81.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  82.                     .addComponent(birthdayField)
  83.                     .addComponent(nameField)
  84.                     .addComponent(studnoField, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
  85.                     .addComponent(sectionCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  86.                     .addComponent(tuitionField))
  87.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  88.         );
  89.         jPanel1Layout.setVerticalGroup(
  90.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  91.             .addGroup(jPanel1Layout.createSequentialGroup()
  92.                 .addGap(4, 4, 4)
  93.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  94.                     .addComponent(jLabel1)
  95.                     .addComponent(studnoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  96.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  97.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  98.                     .addComponent(jLabel2)
  99.                     .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  100.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  101.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  102.                     .addComponent(jLabel3)
  103.                     .addComponent(sectionCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  104.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  105.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  106.                     .addComponent(jLabel4)
  107.                     .addComponent(tuitionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  108.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  109.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  110.                     .addComponent(jLabel5)
  111.                     .addComponent(birthdayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  112.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  113.         );
  114.  
  115.         jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Insert Status", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
  116.  
  117.         statusTextArea.setColumns(20);
  118.         statusTextArea.setLineWrap(true);
  119.         statusTextArea.setRows(5);
  120.         jScrollPane1.setViewportView(statusTextArea);
  121.  
  122.         javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
  123.         jPanel3.setLayout(jPanel3Layout);
  124.         jPanel3Layout.setHorizontalGroup(
  125.             jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  126.             .addGroup(jPanel3Layout.createSequentialGroup()
  127.                 .addContainerGap()
  128.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
  129.                 .addContainerGap())
  130.         );
  131.         jPanel3Layout.setVerticalGroup(
  132.             jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  133.             .addGroup(jPanel3Layout.createSequentialGroup()
  134.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  135.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  136.         );
  137.  
  138.         jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Save", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
  139.  
  140.         saveButton.setText("Insert Record");
  141.         saveButton.addActionListener(new java.awt.event.ActionListener() {
  142.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  143.                 saveButtonActionPerformed(evt);
  144.             }
  145.         });
  146.  
  147.         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  148.         jPanel2.setLayout(jPanel2Layout);
  149.         jPanel2Layout.setHorizontalGroup(
  150.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  151.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
  152.                 .addContainerGap(162, Short.MAX_VALUE)
  153.                 .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
  154.                 .addContainerGap())
  155.         );
  156.         jPanel2Layout.setVerticalGroup(
  157.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  158.             .addGroup(jPanel2Layout.createSequentialGroup()
  159.                 .addComponent(saveButton)
  160.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  161.         );
  162.  
  163.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  164.         getContentPane().setLayout(layout);
  165.         layout.setHorizontalGroup(
  166.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  167.             .addGroup(layout.createSequentialGroup()
  168.                 .addContainerGap()
  169.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  170.                     .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  171.                     .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  172.                     .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  173.                 .addContainerGap())
  174.         );
  175.         layout.setVerticalGroup(
  176.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  177.             .addGroup(layout.createSequentialGroup()
  178.                 .addContainerGap()
  179.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  180.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  181.                 .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  182.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  183.                 .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  184.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  185.         );
  186.  
  187.         pack();
  188.     }// </editor-fold>
  189.  
  190.     private void formWindowClosing(java.awt.event.WindowEvent evt) {                                  
  191.         try{
  192.     conn.close();
  193.     }
  194.     catch(Exception e) {};
  195.     }                                  
  196.  
  197.     private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  198. String studnof = studnoField.getText();
  199.         String namef = nameField.getText();
  200.         String sectionf = (String) sectionCombo.getSelectedItem();
  201.         String tuitionf = tuitionField.getText();
  202.         String birthdayf = birthdayField.getText();
  203.  
  204.         try{
  205.  
  206.             String SQL = "INSERT INTO students VALUES("
  207.                     + "'" + studnof + "'" + ","
  208.                     + "'" + namef + "'" + ","
  209.                     + "'" + sectionf + "'" + ","
  210.                     + tuitionf + ","
  211.                     + "'" + birthdayf + "'"
  212.                     + ")";
  213.  
  214.             PreparedStatement stmt = conn.prepareStatement(SQL);
  215.             stmt.executeUpdate();
  216.  
  217.             getContentPane().removeAll();       //removes all the components
  218.             initComponents();                   //display back the components
  219.  
  220.             statusTextArea.setText("Insert Successful!");
  221.         }
  222.         catch(Exception e)
  223.         {
  224.             statusTextArea.setText( e.getMessage() );
  225.         }
  226.     }                                          
  227.  
  228.     private void sectionComboActionPerformed(java.awt.event.ActionEvent evt) {
  229.         // TODO add your handling code here:
  230.     }
  231.  
  232.     public static void main(String args[]) {
  233.         java.awt.EventQueue.invokeLater(new Runnable() {
  234.             public void run() {
  235.                 new GuiInsert().setVisible(true);
  236.             }
  237.         });
  238.     }
  239.  
  240.     // Variables declaration - do not modify
  241.     private javax.swing.JTextField birthdayField;
  242.     private javax.swing.JLabel jLabel1;
  243.     private javax.swing.JLabel jLabel2;
  244.     private javax.swing.JLabel jLabel3;
  245.     private javax.swing.JLabel jLabel4;
  246.     private javax.swing.JLabel jLabel5;
  247.     private javax.swing.JPanel jPanel1;
  248.     private javax.swing.JPanel jPanel2;
  249.     private javax.swing.JPanel jPanel3;
  250.     private javax.swing.JScrollPane jScrollPane1;
  251.     private javax.swing.JTextField nameField;
  252.     private javax.swing.JButton saveButton;
  253.     private javax.swing.JComboBox sectionCombo;
  254.     private javax.swing.JTextArea statusTextArea;
  255.     private javax.swing.JTextField studnoField;
  256.     private javax.swing.JTextField tuitionField;
  257.     // End of variables declaration
  258.  
  259. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top