Java tutorial
package com.dvd.ui; import java.awt.Color; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import org.json.JSONObject; import com.dvd.hibernate.doa.DVDCopyDao; import com.dvd.prop.PropReader; /* * 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. */ public class UpdateCopy extends javax.swing.JFrame { private boolean isServiceOn = false; /** * Creates new form ShopUI */ public UpdateCopy() { initComponents(); PropReader properties = new PropReader(); isServiceOn = properties.getConnectionProp(); System.out.println(isServiceOn); } /** * 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() { jPanel1 = new javax.swing.JPanel(); label1 = new java.awt.Label(); jLabel1 = new javax.swing.JLabel(); errorLabel = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); label2 = new java.awt.Label(); label4 = new java.awt.Label(); label5 = new java.awt.Label(); copyNumTxt2 = new java.awt.TextField(); dvdCodeTxt2 = new java.awt.TextField(); shelfNumTxt2 = new java.awt.TextField(); label7 = new java.awt.Label(); isleNumTxt2 = new java.awt.TextField(); updateBtn1 = new javax.swing.JButton(); errorTagAddCopyLabel = new javax.swing.JLabel(); errorcopyLabel1 = new javax.swing.JLabel(); errorcopyLabel3 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setBackground(new java.awt.Color(106, 161, 172)); setResizable(false); jPanel1.setBackground(new java.awt.Color(23, 193, 111)); jPanel1.setForeground(new java.awt.Color(254, 238, 238)); label1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); label1.setFont(new java.awt.Font("Ubuntu Medium", 0, 48)); // NOI18N label1.setForeground(new java.awt.Color(254, 254, 254)); label1.setText("DVD Shop"); jLabel1.setFont(new java.awt.Font("Ubuntu Medium", 0, 24)); // NOI18N jLabel1.setForeground(new java.awt.Color(254, 254, 254)); jLabel1.setText("Update DVD Copy"); errorLabel.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N errorLabel.setForeground(new java.awt.Color(226, 33, 33)); jPanel3.setBackground(new java.awt.Color(226, 226, 226)); label2.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N label2.setForeground(new java.awt.Color(1, 1, 1)); label2.setText("Copy Number"); label4.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N label4.setForeground(new java.awt.Color(1, 1, 1)); label4.setName(""); // NOI18N label4.setText("ISLE Number"); label5.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N label5.setForeground(new java.awt.Color(1, 1, 1)); label5.setText("Shelf Number"); copyNumTxt2.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N copyNumTxt2.setForeground(new java.awt.Color(1, 1, 1)); copyNumTxt2.setName("txtCusID"); // NOI18N copyNumTxt2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { copyNumTxt2ActionPerformed(evt); } }); dvdCodeTxt2.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N dvdCodeTxt2.setForeground(new java.awt.Color(1, 1, 1)); dvdCodeTxt2.setName("txtCus"); // NOI18N dvdCodeTxt2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dvdCodeTxt2ActionPerformed(evt); } }); shelfNumTxt2.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N shelfNumTxt2.setForeground(new java.awt.Color(1, 1, 1)); shelfNumTxt2.setName("txtCusID"); // NOI18N shelfNumTxt2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { shelfNumTxt2ActionPerformed(evt); } }); label7.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N label7.setForeground(new java.awt.Color(1, 1, 1)); label7.setText("DVD Code"); isleNumTxt2.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N isleNumTxt2.setForeground(new java.awt.Color(1, 1, 1)); isleNumTxt2.setName("txtCusID"); // NOI18N isleNumTxt2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { isleNumTxt2ActionPerformed(evt); } }); updateBtn1.setText("Update"); updateBtn1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { updateBtn1ActionPerformed(evt); } }); errorTagAddCopyLabel.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N errorTagAddCopyLabel.setForeground(new java.awt.Color(133, 20, 20)); errorcopyLabel1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N errorcopyLabel1.setForeground(new java.awt.Color(226, 33, 33)); errorcopyLabel3.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N errorcopyLabel3.setForeground(new java.awt.Color(226, 33, 33)); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(errorTagAddCopyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(24, 24, 24)) .addGroup(jPanel3Layout.createSequentialGroup().addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(25, 25, 25) .addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(errorcopyLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel3Layout.createSequentialGroup() .addGroup(jPanel3Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(52, 52, 52) .addGroup(jPanel3Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(isleNumTxt2, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE) .addComponent(shelfNumTxt2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(copyNumTxt2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(dvdCodeTxt2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))) .addGroup(jPanel3Layout.createSequentialGroup().addGap(122, 122, 122).addComponent( updateBtn1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(56, Short.MAX_VALUE)) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(52, 52, 52) .addComponent(errorcopyLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(99, Short.MAX_VALUE)))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(29, 29, 29).addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(dvdCodeTxt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(copyNumTxt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(shelfNumTxt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(isleNumTxt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup().addComponent(errorTagAddCopyLabel) .addGap(38, 38, 38)) .addComponent(errorcopyLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(updateBtn1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26)) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(115, 115, 115) .addComponent(errorcopyLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(175, Short.MAX_VALUE)))); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout .createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(114, 114, 114)) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(129, 129, 129) .addComponent(jLabel1)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(101, 101, 101) .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(23, 23, 23) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(25, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(506, 506, 506).addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(276, 276, 276))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void copyNumTxt2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_copyNumTxt2ActionPerformed // TODO add your handling code here: }// GEN-LAST:event_copyNumTxt2ActionPerformed private void dvdCodeTxt2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_dvdCodeTxt2ActionPerformed // TODO add your handling code here: }// GEN-LAST:event_dvdCodeTxt2ActionPerformed private void shelfNumTxt2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_shelfNumTxt2ActionPerformed // TODO add your handling code here: }// GEN-LAST:event_shelfNumTxt2ActionPerformed private void isleNumTxt2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_isleNumTxt2ActionPerformed // TODO add your handling code here: }// GEN-LAST:event_isleNumTxt2ActionPerformed private void updateBtn1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_updateBtn1ActionPerformed String dvdCodeTxt = dvdCodeTxt2.getText(); String copyNumTxt = copyNumTxt2.getText(); String isleNumTxt = isleNumTxt2.getText(); String shelfNumTxt = shelfNumTxt2.getText(); if (!dvdCodeTxt.isEmpty() && !copyNumTxt.isEmpty() && !isleNumTxt.isEmpty() && !shelfNumTxt.isEmpty()) { try { if (isServiceOn) { String result = ""; String url1 = "http://localhost:8080/updateCopyDVD?code=" + dvdCodeTxt + "©Number=" + copyNumTxt + "&isleNumber=" + isleNumTxt + "&shelfNumber=" + shelfNumTxt; // String q = URLEncoder.encode(url1, "UTF-8").replace("+", // "%20"); URL url = new URL(url1); URL obj = url; HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // con.setRequestProperty(CONTENT_LENGTH, // Integer.toString(content.getBytes().length)); con.setRequestProperty("Accept-Charset", "UTF-8"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Content-Language", "en-US"); // optional default is GET con.setRequestMethod("POST"); // add request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // print result result = response.toString(); // Parse to get translated text JSONObject jsonObject = new JSONObject(result); int code = jsonObject.getJSONObject("responseCode").getInt("code"); if (code == 201) { errorcopyLabel3.setForeground(Color.BLACK); errorcopyLabel3.setText("DVD Copy Successfully Updated."); } else { errorcopyLabel3.setForeground(Color.RED); errorcopyLabel3.setText("Updating DVD Copy failed"); } } else { com.dvd.hibernate.repo.DVDRepository repository = new com.dvd.hibernate.repo.DVDRepository(); DVDCopyDao copyDao = new DVDCopyDao(); copyDao.setAvailable("1"); copyDao.setCopyNumber(Integer.parseInt(copyNumTxt)); copyDao.setDvdCode(Integer.parseInt(dvdCodeTxt)); copyDao.setShelfNumber(shelfNumTxt); copyDao.setIsleNumber(isleNumTxt); repository.updateCopyDVD(copyDao); errorcopyLabel3.setForeground(Color.BLACK); errorcopyLabel3.setText("DVD Copy Successfully Updated."); } } catch (Exception e) { errorcopyLabel3.setForeground(Color.RED); errorcopyLabel3.setText("Updating DVD Copy failed"); } } else { errorcopyLabel3.setForeground(Color.RED); errorcopyLabel3.setText("Updating DVD Copy failed"); } // TODO add your handling code here: }// GEN-LAST:event_updateBtn1ActionPerformed /** * @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(UpdateCopy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(UpdateCopy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(UpdateCopy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(UpdateCopy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new UpdateCopy().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private java.awt.TextField copyNumTxt2; private java.awt.TextField dvdCodeTxt2; private javax.swing.JLabel errorLabel; private javax.swing.JLabel errorTagAddCopyLabel; private javax.swing.JLabel errorcopyLabel1; private javax.swing.JLabel errorcopyLabel3; private java.awt.TextField isleNumTxt2; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel3; private java.awt.Label label1; private java.awt.Label label2; private java.awt.Label label4; private java.awt.Label label5; private java.awt.Label label7; private java.awt.TextField shelfNumTxt2; private javax.swing.JButton updateBtn1; // End of variables declaration//GEN-END:variables }