Advertisement
Guest User

Untitled

a guest
Aug 11th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.21 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package gui;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13.  
  14.  
  15.  
  16.  
  17.  
  18. /**
  19.  *
  20.  * @author Robert Brown
  21.  */
  22. public class addressBook extends javax.swing.JFrame
  23. {  
  24.     Connection conn =null;
  25.     public addressBook()    
  26.     {
  27.         initComponents();
  28.         this.setTitle(" Address Book ");
  29.  
  30.  
  31.         // try{
  32.         //Class.forName("com.mysql.jdbc.Driver");
  33.     }
  34.     //catch (ClassNotFoundException e)
  35.     {  
  36.         //System.out.println("MySQL Driver not found!");
  37.         //return;
  38.         //}
  39.         //System.out.println("MySQL JDBC Driver Registered !");
  40.         // }
  41.         //{
  42.        
  43.         try{
  44.             conn=DriverManager.getConnection("jdbc:mysql://devry.edupe.net:4300/CIS355A_3950","3950","devrystudent");
  45.  
  46.             PreparedStatement myquery = conn.prepareStatement("Select * from contacts");
  47.  
  48.             ResultSet result = myquery.executeQuery();
  49.  
  50.             while(result.next())
  51.             {
  52.                 System.out.println(result.getString(1));
  53.             }
  54.  
  55.  
  56.         }   catch (SQLException e) {
  57.             e.printStackTrace();
  58.             System.out.println("Connection failed");
  59.         }
  60.     }
  61.     /**
  62.      * This method is called from within the constructor to initialize the form.
  63.      * WARNING: Do NOT modify this code. The content of this method is always
  64.      * regenerated by the Form Editor.
  65.      */
  66.     @SuppressWarnings("unchecked")
  67.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  68.     private void initComponents() {
  69.  
  70.         jTabbedPane2 = new javax.swing.JTabbedPane();
  71.         contacts = new javax.swing.JPanel();
  72.         jScrollPane1 = new javax.swing.JScrollPane();
  73.         jList1 = new javax.swing.JList<>();
  74.         removeContact = new javax.swing.JButton();
  75.         addContacts = new javax.swing.JPanel();
  76.         name = new javax.swing.JTextField();
  77.         addContactButton = new javax.swing.JButton();
  78.         phoneNum = new javax.swing.JTextField();
  79.         jLabel1 = new javax.swing.JLabel();
  80.         jLabel2 = new javax.swing.JLabel();
  81.         jLabel3 = new javax.swing.JLabel();
  82.         email = new javax.swing.JTextField();
  83.  
  84.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  85.  
  86.         jList1.setModel(new javax.swing.AbstractListModel<String>() {
  87.             String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
  88.             public int getSize() { return strings.length; }
  89.             public String getElementAt(int i) { return strings[i]; }
  90.         });
  91.         jScrollPane1.setViewportView(jList1);
  92.  
  93.         removeContact.setText("Remove contact");
  94.         removeContact.addActionListener(new java.awt.event.ActionListener() {
  95.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  96.                 removeContactActionPerformed(evt);
  97.             }
  98.         });
  99.  
  100.         javax.swing.GroupLayout contactsLayout = new javax.swing.GroupLayout(contacts);
  101.         contacts.setLayout(contactsLayout);
  102.         contactsLayout.setHorizontalGroup(
  103.                 contactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  104.                 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contactsLayout.createSequentialGroup()
  105.                         .addGap(0, 0, Short.MAX_VALUE)
  106.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE))
  107.                 .addGroup(contactsLayout.createSequentialGroup()
  108.                         .addGap(138, 138, 138)
  109.                         .addComponent(removeContact)
  110.                         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  111.                 );
  112.         contactsLayout.setVerticalGroup(
  113.                 contactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  114.                 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contactsLayout.createSequentialGroup()
  115.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
  116.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  117.                         .addComponent(removeContact)
  118.                         .addContainerGap())
  119.                 );
  120.  
  121.         jTabbedPane2.addTab("Contacts", contacts);
  122.  
  123.         name.addActionListener(new java.awt.event.ActionListener() {
  124.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  125.                 nameActionPerformed(evt);
  126.             }
  127.         });
  128.  
  129.         addContactButton.setText("Add");
  130.         addContactButton.addActionListener(new java.awt.event.ActionListener() {
  131.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  132.                 addContactButtonActionPerformed(evt);
  133.             }
  134.         });
  135.  
  136.         phoneNum.addActionListener(new java.awt.event.ActionListener() {
  137.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  138.                 phoneNumActionPerformed(evt);
  139.             }
  140.         });
  141.  
  142.         jLabel1.setText("Name");
  143.  
  144.         jLabel2.setText("Phone number");
  145.  
  146.         jLabel3.setText("E-mail");
  147.  
  148.         javax.swing.GroupLayout addContactsLayout = new javax.swing.GroupLayout(addContacts);
  149.         addContacts.setLayout(addContactsLayout);
  150.         addContactsLayout.setHorizontalGroup(
  151.                 addContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  152.                 .addGroup(addContactsLayout.createSequentialGroup()
  153.                         .addContainerGap()
  154.                         .addGroup(addContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  155.                                 .addGroup(addContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  156.                                         .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  157.                                         .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  158.                                 .addComponent(jLabel3))
  159.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  160.                         .addGroup(addContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  161.                                 .addComponent(email, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
  162.                                 .addComponent(name)
  163.                                 .addComponent(phoneNum))
  164.                         .addContainerGap())
  165.                 .addGroup(addContactsLayout.createSequentialGroup()
  166.                         .addGap(181, 181, 181)
  167.                         .addComponent(addContactButton)
  168.                         .addContainerGap(187, Short.MAX_VALUE))
  169.                 );
  170.         addContactsLayout.setVerticalGroup(
  171.                 addContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  172.                 .addGroup(addContactsLayout.createSequentialGroup()
  173.                         .addContainerGap()
  174.                         .addGroup(addContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  175.                                 .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  176.                                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
  177.                         .addGap(25, 25, 25)
  178.                         .addGroup(addContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  179.                                 .addComponent(jLabel2)
  180.                                 .addComponent(phoneNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  181.                         .addGap(33, 33, 33)
  182.                         .addGroup(addContactsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  183.                                 .addComponent(jLabel3)
  184.                                 .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  185.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
  186.                         .addComponent(addContactButton)
  187.                         .addGap(39, 39, 39))
  188.                 );
  189.  
  190.         jTabbedPane2.addTab("Add contacts", addContacts);
  191.  
  192.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  193.         getContentPane().setLayout(layout);
  194.         layout.setHorizontalGroup(
  195.                 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  196.                 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  197.                         .addContainerGap()
  198.                         .addComponent(jTabbedPane2)
  199.                         .addContainerGap())
  200.                 );
  201.         layout.setVerticalGroup(
  202.                 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  203.                 .addComponent(jTabbedPane2)
  204.                 );
  205.  
  206.         pack();
  207.     }// </editor-fold>                        
  208.  
  209.     private void nameActionPerformed(java.awt.event.ActionEvent evt) {                                    
  210.         // TODO add your handling code here:
  211.     }                                    
  212.  
  213.     private void phoneNumActionPerformed(java.awt.event.ActionEvent evt) {                                        
  214.         // TODO add your handling code here:
  215.     }                                        
  216.  
  217.     private void addContactButtonActionPerformed(java.awt.event.ActionEvent evt) throws SQLException {                                                
  218.         // TODO add your handling code here:
  219.         String sqlText = "INSERT INTO jdbc_demo " + "VALUES (1, 'One')";
  220.         System.out.println("Executing this command: \n" +
  221.                 sqlText.replaceAll("\\s+", " ") + "\n");
  222.         conn.createStatement().executeUpdate(sqlText);
  223.     }                                                
  224.  
  225.     private void removeContactActionPerformed(java.awt.event.ActionEvent evt) {                                              
  226.         // TODO add your handling code here:
  227.     }                                            
  228.  
  229.     /**
  230.      * @param args the command line arguments
  231.      */
  232.     public static void main(String args[]) {
  233.         /* Set the Nimbus look and feel */
  234.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  235.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  236.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  237.          */
  238.         try {
  239.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  240.                 if ("Nimbus".equals(info.getName())) {
  241.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  242.                     break;
  243.                 }
  244.             }
  245.         } catch (ClassNotFoundException ex) {
  246.             java.util.logging.Logger.getLogger(addressBook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  247.         } catch (InstantiationException ex) {
  248.             java.util.logging.Logger.getLogger(addressBook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  249.         } catch (IllegalAccessException ex) {
  250.             java.util.logging.Logger.getLogger(addressBook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  251.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  252.             java.util.logging.Logger.getLogger(addressBook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  253.         }
  254.         //</editor-fold>
  255.  
  256.         /* Create and display the form */
  257.         java.awt.EventQueue.invokeLater(new Runnable() {
  258.             public void run() {
  259.                 new addressBook().setVisible(true);
  260.             }
  261.         });
  262.     }
  263.  
  264.     // Variables declaration - do not modify                    
  265.     private javax.swing.JButton addContactButton;
  266.     private javax.swing.JPanel addContacts;
  267.     private javax.swing.JPanel contacts;
  268.     private javax.swing.JTextField email;
  269.     private javax.swing.JLabel jLabel1;
  270.     private javax.swing.JLabel jLabel2;
  271.     private javax.swing.JLabel jLabel3;
  272.     private javax.swing.JList<String> jList1;
  273.     private javax.swing.JScrollPane jScrollPane1;
  274.     private javax.swing.JTabbedPane jTabbedPane2;
  275.     private javax.swing.JTextField name;
  276.     private javax.swing.JTextField phoneNum;
  277.     private javax.swing.JButton removeContact;
  278.     // End of variables declaration                  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement