Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. public class NewJFrame extends javax.swing.JFrame {
  2.  
  3. public NewJFrame() {
  4. initComponents();
  5. getConnection ();
  6. }
  7.  
  8. public Connection getConnection (){
  9. Connection con = null;
  10.  
  11. try {
  12. con = DriverManager.getConnection("jdbc:mysql://localhost/products","root","");
  13. JOptionPane.showMessageDialog(null, "Connected");
  14. return con;
  15. } catch (SQLException ex) {
  16. Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
  17. JOptionPane.showMessageDialog(null, "Not Connected");
  18. return null;
  19. }
  20. }
  21.  
  22. // Check input fields
  23.  
  24. public boolean checkInputs (){
  25. if (txt_trackingNumber.getText() == null
  26. || txt_lastName.getText() == null
  27. || txt_firstName.getText() == null
  28. || txt_houseNo.getText() == null
  29. || txt_street.getText() == null
  30. || txt_barangay.getText() == null
  31. || txt_city.getText() == null){
  32. return false;
  33. }
  34. else{
  35. try{
  36. return true;
  37. }catch (Exception ex){
  38. return false;
  39. }
  40.  
  41. }
  42. }
  43.  
  44.  
  45. txt_trackingNumber.addActionListener(new java.awt.event.ActionListener() {
  46. public void actionPerformed(java.awt.event.ActionEvent evt) {
  47. txt_trackingNumberActionPerformed(evt);
  48. }
  49. });
  50.  
  51. jTable1.setModel(new javax.swing.table.DefaultTableModel(
  52. new Object [][] {
  53.  
  54. },
  55. new String [] {
  56. "Tracking No", "Last Name", "First Name", "House No.", "Street", "Barangay", "City"
  57. }
  58. ));
  59. jScrollPane1.setViewportView(jTable1);
  60.  
  61. jLabel4.setText("House No.");
  62.  
  63. jLabel5.setText("Street");
  64.  
  65. jLabel6.setText("Barangay");
  66.  
  67. jLabel8.setText("City");
  68.  
  69. jLabel9.setText("Sender");
  70.  
  71. jButton2.setText("INSERT");
  72. jButton2.addActionListener(new java.awt.event.ActionListener() {
  73. public void actionPerformed(java.awt.event.ActionEvent evt) {
  74. jButton2ActionPerformed(evt);
  75. }
  76. });
  77.  
  78. jButton3.setText("UPDATE");
  79.  
  80. jButton4.setText("DELETE");
  81.  
  82. jMenu1.setText("File");
  83.  
  84. jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
  85. jMenuItem1.setText("New");
  86. jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
  87. public void actionPerformed(java.awt.event.ActionEvent evt) {
  88. jMenuItem1ActionPerformed(evt);
  89. }
  90. });
  91. jMenu1.add(jMenuItem1);
  92.  
  93. jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
  94. jMenuItem2.setText("Save");
  95. jMenu1.add(jMenuItem2);
  96.  
  97. jMenuBar1.add(jMenu1);
  98.  
  99. jMenu2.setText("Edit");
  100.  
  101. jMenuItem3.setText("Add");
  102. jMenu2.add(jMenuItem3);
  103.  
  104. jMenuBar1.add(jMenu2);
  105.  
  106. jMenu3.setText("Sorting");
  107. jMenuBar1.add(jMenu3);
  108.  
  109. setJMenuBar(jMenuBar1);
  110.  
  111.  
  112. private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
  113. System.exit(0);
  114. }
  115.  
  116. private void txt_trackingNumberActionPerformed(java.awt.event.ActionEvent evt) {
  117. // TODO add your handling code here:
  118. }
  119.  
  120. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  121. Object ImgFath = null;
  122. if (checkInputs() && ImgFath != null){
  123.  
  124. try {
  125. Connection con = getConnection ();
  126. PreparedStatement ps = con.prepareStatement("INSERT INTO products(Transaction_No,Last_Name,First_Name,House_No,Street,Barangay,City)"
  127. + "values(?,?,?,?,?,?,?)");
  128. ps.setString(1, txt_trackingNumber.getText());
  129. ps.setString(2, txt_lastName.getText());
  130. ps.setString(3, txt_firstName.getText());
  131. ps.setString(4, txt_houseNo.getText());
  132. ps.setString(5, txt_street.getText());
  133. ps.setString(6, txt_barangay.getText());
  134. ps.setString(7, txt_city.getText());
  135. ps.executeUpdate();
  136.  
  137. JOptionPane.showMessageDialog(null, "DATA INSERTED");
  138. } catch (Exception ex) {
  139. JOptionPane.showMessageDialog(null, ex.getMessage());
  140. }
  141. }
  142. else {
  143. JOptionPane.showMessageDialog(null, "One or");
  144. }
  145.  
  146.  
  147. System.out.println("Tracking Number => " +txt_trackingNumber.getText());
  148. System.out.println("LastName => " +txt_lastName.getText());
  149. System.out.println("FirstName => " +txt_firstName.getText());
  150. System.out.println("House Number => " +txt_houseNo.getText());
  151. System.out.println("Street => " +txt_street.getText());
  152. System.out.println("Barangay => " +txt_barangay.getText());
  153. System.out.println("City => " +txt_city.getText());
  154.  
  155. }
  156.  
  157. /**
  158. * @param args the command line arguments
  159. */
  160. public static void main(String args[]) {
  161. /* Set the Nimbus look and feel */
  162. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  163. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  164. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  165. */
  166. try {
  167. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  168. if ("Nimbus".equals(info.getName())) {
  169. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  170. break;
  171. }
  172. }
  173. } catch (ClassNotFoundException ex) {
  174. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  175. } catch (InstantiationException ex) {
  176. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  177. } catch (IllegalAccessException ex) {
  178. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  179. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  180. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  181. }
  182. //</editor-fold>
  183.  
  184. /* Create and display the form */
  185. java.awt.EventQueue.invokeLater(new Runnable() {
  186. public void run() {
  187. new NewJFrame().setVisible(true);
  188. }
  189. });
  190. }
  191.  
  192. // Variables declaration - do not modify
  193. private javax.swing.JButton jButton2;
  194. private javax.swing.JButton jButton3;
  195. private javax.swing.JButton jButton4;
  196. private javax.swing.JLabel jLabel1;
  197. private javax.swing.JLabel jLabel2;
  198. private javax.swing.JLabel jLabel3;
  199. private javax.swing.JLabel jLabel4;
  200. private javax.swing.JLabel jLabel5;
  201. private javax.swing.JLabel jLabel6;
  202. private javax.swing.JLabel jLabel8;
  203. private javax.swing.JLabel jLabel9;
  204. private javax.swing.JMenu jMenu1;
  205. private javax.swing.JMenu jMenu2;
  206. private javax.swing.JMenu jMenu3;
  207. private javax.swing.JMenuBar jMenuBar1;
  208. private javax.swing.JMenuItem jMenuItem1;
  209. private javax.swing.JMenuItem jMenuItem2;
  210. private javax.swing.JMenuItem jMenuItem3;
  211. private javax.swing.JScrollPane jScrollPane1;
  212. private javax.swing.JTable jTable1;
  213. private javax.swing.JTextField txt_barangay;
  214. private javax.swing.JTextField txt_city;
  215. private javax.swing.JTextField txt_firstName;
  216. private javax.swing.JTextField txt_houseNo;
  217. private javax.swing.JTextField txt_lastName;
  218. private javax.swing.JTextField txt_street;
  219. private javax.swing.JTextField txt_trackingNumber;
  220. // End of variables declaration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement