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.domain.User; import com.dvd.hibernate.repo.UserRepository; 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 AddUser extends javax.swing.JFrame { private boolean isServiceOn = false; /** * Creates new form ShopUI */ public AddUser() { 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() { errorLabel = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); label1 = new java.awt.Label(); label2 = new java.awt.Label(); label4 = new java.awt.Label(); label5 = new java.awt.Label(); userIDTxt = new java.awt.TextField(); label6 = new java.awt.Label(); userNameTxt = new java.awt.TextField(); typeFlag = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); addUserBtn = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); userPassword = new javax.swing.JPasswordField(); errorUserTag = new javax.swing.JLabel(); errorLabel.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N errorLabel.setForeground(new java.awt.Color(226, 33, 33)); errorLabel.setText("sss"); 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"); label2.setFont(new java.awt.Font("Ubuntu Medium", 0, 16)); // NOI18N label2.setForeground(new java.awt.Color(254, 254, 254)); label2.setText("User ID"); label4.setFont(new java.awt.Font("Ubuntu Medium", 0, 16)); // NOI18N label4.setForeground(new java.awt.Color(254, 254, 254)); label4.setName(""); // NOI18N label4.setText("User Name"); label5.setFont(new java.awt.Font("Ubuntu Medium", 0, 16)); // NOI18N label5.setForeground(new java.awt.Color(254, 254, 254)); label5.setText("Password"); userIDTxt.setFont(new java.awt.Font("Ubuntu Medium", 1, 16)); // NOI18N userIDTxt.setName("txtCus"); // NOI18N userIDTxt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { userIDTxtActionPerformed(evt); } }); label6.setFont(new java.awt.Font("Ubuntu Medium", 0, 16)); // NOI18N label6.setForeground(new java.awt.Color(254, 254, 254)); label6.setText("Type"); userNameTxt.setFont(new java.awt.Font("Ubuntu Medium", 1, 16)); // NOI18N userNameTxt.setName("txtCusID"); // NOI18N userNameTxt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { userNameTxtActionPerformed(evt); } }); typeFlag.setFont(new java.awt.Font("Ubuntu Medium", 0, 16)); // NOI18N typeFlag.setForeground(new java.awt.Color(34, 25, 25)); typeFlag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Member", "Employee" })); typeFlag.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { typeFlagActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Ubuntu Medium", 0, 24)); // NOI18N jLabel1.setForeground(new java.awt.Color(254, 254, 254)); jLabel1.setText("Add User"); addUserBtn.setForeground(new java.awt.Color(1, 1, 1)); addUserBtn.setText("Add User"); addUserBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addUserBtnActionPerformed(evt); } }); jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/user.png"))); // NOI18N errorUserTag.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N errorUserTag.setForeground(new java.awt.Color(226, 33, 33)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel2) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(89, 89, 89) .addComponent(jLabel1)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(44, 44, 44) .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(jPanel1Layout.createSequentialGroup().addGap(31, 31, 31) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .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) .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(118, 118, 118) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(addUserBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(userNameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(userIDTxt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(typeFlag, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(userPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)))) .addGroup(jPanel1Layout.createSequentialGroup().addGap(182, 182, 182).addComponent( errorUserTag, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(71, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .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)) .addComponent(jLabel2)) .addGap(19, 19, 19) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(userIDTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(27, 27, 27) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(userNameTxt, 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(23, 23, 23) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(4, 4, 4) .addComponent(userPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(typeFlag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE) .addComponent(errorUserTag, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(addUserBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); 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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void typeFlagActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_typeFlagActionPerformed // TODO add your handling code here: }// GEN-LAST:event_typeFlagActionPerformed private void userNameTxtActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_userNameTxtActionPerformed // TODO add your handling code here: }// GEN-LAST:event_userNameTxtActionPerformed private void userIDTxtActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_userIDTxtActionPerformed // TODO add your handling code here: }// GEN-LAST:event_userIDTxtActionPerformed private void addUserBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addUserBtnActionPerformed String userID = userIDTxt.getText(); String userName = userNameTxt.getText(); String password = userPassword.getText(); String type = typeFlag.getSelectedItem().toString(); if (userID != null && !userID.isEmpty() && userName != null && !userName.isEmpty() && password != null && !password.isEmpty() && type != null && !type.isEmpty()) { try { int id = Integer.parseInt(userID); String userType = ""; if (type.equals("Member")) { userType = "mem"; } else if (type.equals("Employee")) { userType = "emp"; } if (isServiceOn) { String result = ""; String url1 = "http://localhost:8080/addUser?userID=" + id + "&UserName=" + URLEncoder.encode(userName, "UTF-8").replace("+", "%20") + "&password=" + password + "&type=" + userType; // 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) { errorUserTag.setForeground(Color.BLACK); errorUserTag.setText("User Successfully Added."); } else { errorUserTag.setForeground(Color.RED); errorUserTag.setText("User Registration failed"); } } else { UserRepository repository = new UserRepository(); User user = new User(userName, id, userType); repository.createUser(user); errorUserTag.setForeground(Color.BLACK); errorUserTag.setText("User Successfully Added."); } } catch (Exception e) { errorUserTag.setForeground(Color.RED); errorUserTag.setText("Adding User failed"); } } else { errorUserTag.setForeground(Color.RED); errorUserTag.setText("Please Enter Valid data"); } }// GEN-LAST:event_addUserBtnActionPerformed /** * @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(AddUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AddUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // </editor-fold> // </editor-fold> // </editor-fold> // </editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AddUser().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addUserBtn; private javax.swing.JLabel errorLabel; private javax.swing.JLabel errorUserTag; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private java.awt.Label label1; private java.awt.Label label2; private java.awt.Label label4; private java.awt.Label label5; private java.awt.Label label6; private javax.swing.JComboBox typeFlag; private java.awt.TextField userIDTxt; private java.awt.TextField userNameTxt; private javax.swing.JPasswordField userPassword; // End of variables declaration//GEN-END:variables }