Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package contactapp;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. public class ContactTable {
  12.     static final String DB_URL = "jdbc:derby://localhost:1527/ContactApp";
  13.     static final String USERNAME = "root";
  14.     static final String PASSWORD = "root";
  15.     private static ResultSet _rs=null;
  16.     int currentSelectedItem;
  17.    
  18.     ContactTable()
  19.     {
  20.         try
  21.         {
  22.             Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
  23.             PreparedStatement selectAllContact = connection.prepareStatement("SELECT * FROM Contact");
  24.             _rs = selectAllContact.executeQuery();
  25.         }
  26.         catch (SQLException sqlException)
  27.         {
  28.            sqlException.printStackTrace();
  29.            System.exit(1);
  30.         }
  31.     }
  32.    
  33.     public void addContact(String firstnameTextField, String lastnameTextField, String emailTextField, String phoneTextField, String notesTextField)
  34.     {
  35.         try
  36.         {
  37.             Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
  38.             PreparedStatement insertContact = connection.prepareStatement("INSERT INTO Contact (FIRSTNAME, LASTNAME, EMAIL, PHONE, NOTES) VALUES (?,?,?,?,?)");
  39.             insertContact.setString(1, firstnameTextField);
  40.             insertContact.setString(2,lastnameTextField);
  41.             insertContact.setString(3,emailTextField);
  42.             insertContact.setString(4,phoneTextField);
  43.             insertContact.setString(5,notesTextField);
  44.             System.out.println(insertContact.toString());
  45.             insertContact.executeUpdate();
  46.             connection.commit();
  47.         }
  48.         catch(SQLException sqlException)
  49.         {
  50.             sqlException.printStackTrace();
  51.             System.exit(2);
  52.         }
  53.     }
  54.    
  55.         public void updateContact(int row_id, String firstnameTextField, String lastnameTextField, String emailTextField, String phoneTextField, String notesTextField)
  56.     {
  57.         try
  58.         {
  59.            
  60.             Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
  61.             PreparedStatement updateContact = connection.prepareStatement("UPDATE Contact SET FIRSTNAME = ?, LASTNAME = ?, EMAIL = ?, PHONE = ?, NOTES = ? WHERE ID = ?");
  62.             updateContact.setString(1,firstnameTextField);
  63.             updateContact.setString(2,lastnameTextField);
  64.             updateContact.setString(3,emailTextField);
  65.             updateContact.setString(4,phoneTextField);
  66.             updateContact.setString(5,notesTextField);
  67.             updateContact.getTableRow(6, row_id);
  68.             updateContact.executeUpdate();
  69.             connection.commit();
  70.         }
  71.         catch(SQLException sqlException)
  72.         {
  73.             sqlException.printStackTrace();
  74.             System.exit(3);
  75.         }
  76.     }
  77.        
  78.        public void deleteContact(int row_id)
  79.     {
  80.         try
  81.         {
  82.             Connection connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
  83.             PreparedStatement deleteContact = connection.prepareStatement("DELETE FROM Contact WHERE ID=?");
  84.             deleteContact.setInt(1, row_id);
  85.             deleteContact.executeUpdate();            
  86.         }
  87.         catch(SQLException sqlException)
  88.         {
  89.             sqlException.printStackTrace();
  90.             System.exit(4);
  91.         }
  92.     }
  93.            
  94.     public ResultSet getResults()
  95.     {
  96.         if (_rs != null) return _rs;
  97.         return new ContactTable()._rs;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement