SHARE
TWEET

Untitled

a guest Jun 30th, 2018 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.capgemini.databank;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class Database {
  12.     private Connection connection;
  13.     private final String connectionString = "jdbc:mariadb://localhost:3306/databank?user=root&password=";
  14.    
  15.     public Database() {
  16.         try {
  17.             setConnection(DriverManager.getConnection(connectionString));
  18.         } catch (Exception e) {
  19.             setConnection(null);
  20.         }
  21.     }
  22.    
  23.     public Connection getConnection() {
  24.         return connection;
  25.     }
  26.  
  27.     public void setConnection(Connection connection) {
  28.         this.connection = connection;
  29.     }
  30.    
  31.     public void insertNewContact(Contact c) throws SQLException {
  32.             Statement stmt = getConnection().createStatement();
  33.             stmt.executeQuery("INSERT INTO Contact VALUES ('" + c.getFirstName() + "','" + c.getLastName() + "','" + c.getCity() + "','" + c.getStreet() + "','" + c.getDistrict() + "','" + c.getZipCode() + "'," + c.getLandlineNo() + "," + c.getMobileNo() + ")");
  34.     }
  35.    
  36.     public void deleteContact(int mobileNumber) throws SQLException {
  37.         // we have to verify that contact really exists, because DELETE doesn't throw an exception if it doesn't       
  38.         Contact pom = findContact(mobileNumber);
  39.         if (pom == null) throw new SQLException();
  40.         else {
  41.             Statement stmt = getConnection().createStatement();
  42.             stmt.executeQuery("DELETE FROM Contact WHERE MobileNumber=" + mobileNumber);
  43.         }
  44.     }
  45.    
  46.     public void updateContact (Contact c) throws SQLException {
  47.         Statement stmt = getConnection().createStatement();
  48.         stmt.executeQuery("INSERT INTO Contact VALUES ('" + c.getFirstName() + "','" + c.getLastName() + "','" + c.getCity() + "','" + c.getStreet() + "','" + c.getDistrict() + "','" + c.getZipCode() + "'," + c.getLandlineNo() + "," + c.getMobileNo() + ")");
  49.     }
  50.    
  51.     public Contact findContact (int mobileNumber) throws SQLException {
  52.             Statement stmt = getConnection().createStatement();
  53.             stmt.executeQuery("SELECT * FROM Contact WHERE MobileNumber=" + mobileNumber + " LIMIT 1");
  54.             ResultSet rs = stmt.getResultSet();
  55.             rs.next(); // current position is before the first row
  56.             return new Contact(rs.getString("FirstName"),
  57.                                          rs.getString("LastName"),
  58.                                          rs.getString("City"),
  59.                                          rs.getString("Street"),
  60.                                          rs.getString("District"),
  61.                                          rs.getString("ZipCode"),
  62.                                          rs.getInt("LandlineNumber"),
  63.                                          rs.getInt("MobileNumber"));
  64.     }
  65.    
  66.     public List<Contact> getAllContacts () throws SQLException {
  67.         List<Contact> contacts=new ArrayList<>();
  68.         Statement stmt = getConnection().createStatement();
  69.         stmt.executeQuery("SELECT * FROM Contact");
  70.         ResultSet rs = stmt.getResultSet();
  71.         while (rs.next()) {
  72.             contacts.add(new Contact(rs.getString("FirstName"),
  73.                                      rs.getString("LastName"),
  74.                                      rs.getString("City"),
  75.                                      rs.getString("Street"),
  76.                                      rs.getString("District"),
  77.                                      rs.getString("ZipCode"),
  78.                                      rs.getInt("LandlineNumber"),
  79.                                      rs.getInt("MobileNumber")));
  80.         }
  81.         return contacts;
  82.     }
  83.    
  84.    
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top