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 AddCopy extends javax.swing.JFrame { private boolean isServiceOn = false; /** * Creates new form ShopUI */ public AddCopy() { 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(); copyNumTxt1 = new java.awt.TextField(); dvdCodeTxt1 = new java.awt.TextField(); shelfNumTxt1 = new java.awt.TextField(); label7 = new java.awt.Label(); isleNumTxt1 = new java.awt.TextField(); addCopyDVDbtn = new javax.swing.JButton(); errorTagAddCopyLabel = new javax.swing.JLabel(); errorcopyLabel1 = 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("Add 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"); copyNumTxt1.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N copyNumTxt1.setForeground(new java.awt.Color(1, 1, 1)); copyNumTxt1.setName("txtCusID"); // NOI18N copyNumTxt1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { copyNumTxt1ActionPerformed(evt); } }); dvdCodeTxt1.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N dvdCodeTxt1.setForeground(new java.awt.Color(1, 1, 1)); dvdCodeTxt1.setName("txtCus"); // NOI18N dvdCodeTxt1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dvdCodeTxt1ActionPerformed(evt); } }); shelfNumTxt1.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N shelfNumTxt1.setForeground(new java.awt.Color(1, 1, 1)); shelfNumTxt1.setName("txtCusID"); // NOI18N shelfNumTxt1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { shelfNumTxt1ActionPerformed(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"); isleNumTxt1.setFont(new java.awt.Font("Ubuntu Medium", 0, 20)); // NOI18N isleNumTxt1.setForeground(new java.awt.Color(1, 1, 1)); isleNumTxt1.setName("txtCusID"); // NOI18N isleNumTxt1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { isleNumTxt1ActionPerformed(evt); } }); addCopyDVDbtn.setText("Add"); addCopyDVDbtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addCopyDVDbtnActionPerformed(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)); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .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.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(isleNumTxt1, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE) .addComponent(shelfNumTxt1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(copyNumTxt1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(dvdCodeTxt1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(errorcopyLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(errorTagAddCopyLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanel3Layout.createSequentialGroup().addGap(152, 152, 152) .addComponent(addCopyDVDbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, 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(dvdCodeTxt1, 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(copyNumTxt1, 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(shelfNumTxt1, 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(isleNumTxt1, 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) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup().addComponent(errorTagAddCopyLabel) .addGap(69, 69, 69)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout .createSequentialGroup() .addComponent(errorcopyLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE) .addComponent(addCopyDVDbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))))); 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, 442, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(34, 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(510, 510, 510).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, 405, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void copyNumTxt1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_copyNumTxt1ActionPerformed // TODO add your handling code here: }// GEN-LAST:event_copyNumTxt1ActionPerformed private void dvdCodeTxt1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_dvdCodeTxt1ActionPerformed // TODO add your handling code here: }// GEN-LAST:event_dvdCodeTxt1ActionPerformed private void shelfNumTxt1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_shelfNumTxt1ActionPerformed // TODO add your handling code here: }// GEN-LAST:event_shelfNumTxt1ActionPerformed private void isleNumTxt1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_isleNumTxt1ActionPerformed // TODO add your handling code here: }// GEN-LAST:event_isleNumTxt1ActionPerformed private void addCopyDVDbtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addCopyDVDbtnActionPerformed String dvdCodeTxt = dvdCodeTxt1.getText(); String copyNumTxt = copyNumTxt1.getText(); String isleNumTxt = isleNumTxt1.getText(); String shelfNumTxt = shelfNumTxt1.getText(); if (!dvdCodeTxt.isEmpty() && !copyNumTxt.isEmpty() && !isleNumTxt.isEmpty() && !shelfNumTxt.isEmpty()) { try { if (isServiceOn) { String result = ""; String url1 = "http://localhost:8080/addCopyDVD?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) { errorcopyLabel1.setForeground(Color.BLACK); errorcopyLabel1.setText("DVD Copy Successfully Added."); } else { errorcopyLabel1.setForeground(Color.RED); errorcopyLabel1.setText("Adding DVD Copy failed"); } } else { com.dvd.hibernate.repo.DVDRepository repository = new com.dvd.hibernate.repo.DVDRepository(); DVDCopyDao dvd = new DVDCopyDao(Integer.parseInt(copyNumTxt), isleNumTxt, shelfNumTxt, Integer.parseInt(dvdCodeTxt), "1"); repository.createCopyDVD(dvd); errorcopyLabel1.setForeground(Color.BLACK); errorcopyLabel1.setText("DVD Copy Successfully Added."); } } catch (Exception e) { errorcopyLabel1.setForeground(Color.RED); errorcopyLabel1.setText("Adding DVD Copy failed"); } } else { errorcopyLabel1.setForeground(Color.RED); errorcopyLabel1.setText("Adding DVD Copy failed"); } }// GEN-LAST:event_addCopyDVDbtnActionPerformed /** * @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(AddCopy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AddCopy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AddCopy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AddCopy.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> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AddCopy().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addCopyDVDbtn; private java.awt.TextField copyNumTxt1; private java.awt.TextField dvdCodeTxt1; private javax.swing.JLabel errorLabel; private javax.swing.JLabel errorTagAddCopyLabel; private javax.swing.JLabel errorcopyLabel1; private java.awt.TextField isleNumTxt1; 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 shelfNumTxt1; // End of variables declaration//GEN-END:variables }