Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Contact Form.java
- Design
- https://i.imgur.com/rgdnRSN.png
- ContactForm.java
- Source
- /*
- * 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.
- */
- package mdjavas12018;
- import java.util.ArrayList;
- import javax.swing.JOptionPane;
- /**
- *
- * @author sgg321-05
- */
- public class ContactForm extends javax.swing.JFrame {
- /**
- * Creates new form ContactForm
- */
- ArrayList<Contact> contacts;
- int maxRows;
- int curRow;
- public ContactForm() {
- initComponents();
- Contact contact=new Contact();
- contacts=contact.getAllContacts();
- maxRows=contacts.size()-1;
- getFirst();
- }
- /**
- * 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">
- private void initComponents() {
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- txtID = new javax.swing.JTextField();
- txtName = new javax.swing.JTextField();
- txtDOB = new javax.swing.JTextField();
- txtCity = new javax.swing.JTextField();
- btnFirst = new javax.swing.JButton();
- btnPrevious = new javax.swing.JButton();
- btnNext = new javax.swing.JButton();
- btnLast = new javax.swing.JButton();
- btnAdd = new javax.swing.JButton();
- btnConnect = new javax.swing.JButton();
- btnCancel = new javax.swing.JButton();
- btnUpdate = new javax.swing.JButton();
- btnDelete = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- jLabel1.setText("Contact Form");
- jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
- jLabel2.setText("ContactID");
- jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
- jLabel3.setText("Name");
- jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
- jLabel4.setText("DOB");
- jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
- jLabel5.setText("City");
- txtID.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- txtIDActionPerformed(evt);
- }
- });
- btnFirst.setText("|<");
- btnFirst.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnFirstActionPerformed(evt);
- }
- });
- btnPrevious.setText("<");
- btnPrevious.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnPreviousActionPerformed(evt);
- }
- });
- btnNext.setText(">");
- btnNext.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnNextActionPerformed(evt);
- }
- });
- btnLast.setText(">|");
- btnLast.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnLastActionPerformed(evt);
- }
- });
- btnAdd.setText("Start Add");
- btnAdd.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnAddActionPerformed(evt);
- }
- });
- btnConnect.setText("Connect Add");
- btnConnect.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnConnectActionPerformed(evt);
- }
- });
- btnCancel.setText("Cancel");
- btnCancel.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnCancelActionPerformed(evt);
- }
- });
- btnUpdate.setText("Update");
- btnUpdate.setToolTipText("");
- btnUpdate.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnUpdateActionPerformed(evt);
- }
- });
- btnDelete.setText("Delete");
- btnDelete.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnDeleteActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(62, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addGap(33, 33, 33))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jLabel5)
- .addComponent(jLabel4)
- .addComponent(jLabel3)
- .addComponent(jLabel2))
- .addGap(45, 45, 45)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(txtDOB, javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(txtID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(txtName, javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(txtCity, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(55, 55, 55))
- .addGroup(layout.createSequentialGroup()
- .addGap(39, 39, 39)
- .addComponent(btnFirst)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btnPrevious)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btnNext)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btnLast)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(btnDelete)
- .addComponent(btnAdd)
- .addComponent(btnConnect)
- .addComponent(btnCancel)
- .addComponent(btnUpdate))
- .addGap(62, 62, 62))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(32, 32, 32)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btnAdd))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel3)
- .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btnConnect))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel4)
- .addComponent(txtDOB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btnCancel))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel5)
- .addComponent(txtCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGroup(layout.createSequentialGroup()
- .addGap(9, 9, 9)
- .addComponent(btnUpdate)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(btnDelete)
- .addGap(2, 2, 2)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btnFirst)
- .addComponent(btnPrevious)
- .addComponent(btnNext)
- .addComponent(btnLast))
- .addContainerGap(60, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void txtIDActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- getFirst();
- }
- private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- clearTextBoxes();
- btnFirst.setEnabled(false);
- btnNext.setEnabled(false);
- btnPrevious.setEnabled(false);
- btnLast.setEnabled(false);
- btnAdd.setEnabled(false);
- btnConnect.setEnabled(true);
- btnCancel.setEnabled(true);
- btnUpdate.setEnabled(false);
- btnDelete.setEnabled(false);
- }
- private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- try{
- Contact contact = contacts.get(curRow);
- contact.deleteContact(contact);
- JOptionPane.showMessageDialog(null, "Current Contact has been deleted");
- contacts.clear();
- contacts=contact.getAllContacts();
- getFirst();
- }catch(Exception e){
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- curRow=maxRows;
- fillDetails();
- }
- private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- if(curRow==maxRows){
- JOptionPane.showMessageDialog
- (null, "This is the Last Record");
- }else{
- curRow++;
- fillDetails();
- }
- }
- private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- if(curRow==0){
- JOptionPane.showMessageDialog
- (null, "This is the First Record");
- }else{
- curRow--;
- fillDetails();
- }
- }
- private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- try{
- Contact contact = contacts.get(curRow);
- contact.setCname(txtName.getText());
- contact.setCcity(txtCity.getText());
- contact.setCdob(txtDOB.getText());
- contact.updateContact(contact);
- JOptionPane.showMessageDialog(null, "Current Contact has been updated");
- contacts.clear();
- contacts=contact.getAllContacts();
- }catch(Exception e){
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- btnFirst.setEnabled(true);
- btnNext.setEnabled(true);
- btnPrevious.setEnabled(true);
- btnLast.setEnabled(true);
- btnAdd.setEnabled(true);
- btnConnect.setEnabled(false);
- btnCancel.setEnabled(true);
- btnUpdate.setEnabled(true);
- btnDelete.setEnabled(true);
- try{
- Contact contact = new Contact();
- int lastID=contacts.get(maxRows).getCid();
- int newID=lastID+1;
- contact.setCid(newID);
- contact.setCname(txtName.getText());
- contact.setCcity(txtCity.getText());
- contact.setCdob(txtDOB.getText());
- contact.addContact(contact);
- JOptionPane.showMessageDialog(null, "New Contact saved");
- contacts.clear();
- contacts=contact.getAllContacts();
- maxRows=contacts.size()-1;
- curRow=maxRows;
- fillDetails();
- }catch(Exception e){
- JOptionPane.showMessageDialog(null, e.getMessage());
- }
- }
- private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- clearTextBoxes();
- btnFirst.setEnabled(true);
- btnNext.setEnabled(true);
- btnPrevious.setEnabled(true);
- btnLast.setEnabled(true);
- btnAdd.setEnabled(true);
- btnConnect.setEnabled(false);
- btnCancel.setEnabled(true);
- btnUpdate.setEnabled(true);
- btnDelete.setEnabled(true);
- getFirst();
- }
- /**
- * @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(ContactForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(ContactForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(ContactForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(ContactForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new ContactForm().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnAdd;
- private javax.swing.JButton btnCancel;
- private javax.swing.JButton btnConnect;
- private javax.swing.JButton btnDelete;
- private javax.swing.JButton btnFirst;
- private javax.swing.JButton btnLast;
- private javax.swing.JButton btnNext;
- private javax.swing.JButton btnPrevious;
- private javax.swing.JButton btnUpdate;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JTextField txtCity;
- private javax.swing.JTextField txtDOB;
- private javax.swing.JTextField txtID;
- private javax.swing.JTextField txtName;
- // End of variables declaration
- private void getFirst() {
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- curRow=0;
- fillDetails();
- }
- private void fillDetails() {
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- Contact contact = contacts.get(curRow);
- txtID.setText(String.valueOf(contact.getCid()));
- txtName.setText(contact.getCname());
- txtCity.setText(contact.getCcity());
- txtDOB.setText(contact.getCdob());
- }
- private void clearTextBoxes() {
- //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- txtName.setText("");
- txtDOB.setText("");
- txtCity.setText("");
- txtID.setText("");
- }
- }
- DBConnect.java
- /*
- * 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.
- */
- package mdjavas12018;
- import java.sql.Statement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- *
- * @author sgg321-05
- */
- public class DBConnect {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Connection con=null;
- ResultSet rst=null;
- Statement stmt=null;
- String DBUrl=
- "jdbc:derby://localhost:1527/";
- String DBName="sample";
- String userName="app";
- String password="app";
- try{
- con=DriverManager
- .getConnection(DBUrl+DBName,userName,password);
- System.out.println("Connected with Sample");
- stmt=con.createStatement();
- String sql="SELECT * FROM APP.CUSTOMER";
- rst=stmt.executeQuery(sql);
- while(rst.next()==true){
- System.out.println
- ("ID: "+rst.getInt("CUSTOMER_ID"));
- System.out.println
- ("Name: "+rst.getString("Name"));
- System.out.println("-----------");
- }
- }
- catch(SQLException e){
- System.err.println("Error: "+e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement