Advertisement
Bawaw

JDBC

May 6th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. package db;
  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.ArrayList;
  9. import java.util.List;
  10. import java.util.Properties;
  11.  
  12. import model.Person;
  13.  
  14. public class UserDb {
  15.     private Connection connection;
  16.     private PreparedStatement statement;
  17.     private Properties properties;
  18.  
  19.     public UserDb(Properties properties) {
  20.         try {
  21.             Class.forName("org.postgresql.Driver");
  22.             setProperties(properties);
  23.         } catch (Exception e) {
  24.             throw new DbException(e.getMessage(), e);
  25.         }
  26.     }
  27.  
  28.     public void delete(String email) {
  29.         if (email == null) {
  30.             throw new DbException("Nothing to delete.");
  31.         }
  32.         String sql = "DELETE FROM web3.person WHERE email=?";
  33.         try {
  34.             initializeStatement(sql);
  35.             statement.setString(1, email);
  36.             statement.execute();
  37.         } catch (SQLException e) {
  38.             throw new DbException(e);
  39.         } finally {
  40.             closeConnection();
  41.         }
  42.     }
  43.  
  44.     public void update(Person person) {
  45.         if (person == null) {
  46.             throw new DbException("Nothing to update.");
  47.         }
  48.         String sql = "UPDATE web3.person SET password=?, name=? WHERE email=?";
  49.         try {
  50.             initializeStatement(sql);
  51.             statement.setString(1, person.getPassword());
  52.             statement.setString(2, person.getName());
  53.             statement.setString(3, person.getEmail());
  54.             statement.execute();
  55.         } catch (SQLException e) {
  56.             throw new DbException(e);
  57.         } finally {
  58.             closeConnection();
  59.         }
  60.     }
  61.  
  62.     public void add(Person person) {
  63.         if (person == null) {
  64.             throw new DbException("Nothing to add.");
  65.         }
  66.         String sql = "INSERT INTO web3.person (name, email, password) VALUES (?, ?, ?)";
  67.         try {
  68.             initializeStatement(sql);
  69.             statement.setString(1, person.getName());
  70.             statement.setString(2, person.getEmail());
  71.             statement.setString(3, person.getPassword());
  72.             statement.execute();
  73.         } catch (SQLException e) {
  74.             throw new DbException(e);
  75.         } finally {
  76.             closeConnection();
  77.         }
  78.     }
  79.  
  80.     public Person get(String email) {
  81.         Person person = null;
  82.         try {
  83.             String sql = "SELECT * FROM web3.person WHERE email=?";
  84.             initializeStatement(sql);
  85.             statement.setString(1, email);
  86.             ResultSet result = statement.executeQuery();
  87.             while (result.next()) {
  88.                 String name = result.getString("name");
  89.                 String password = result.getString("password");
  90.                 person = new Person(name, email, password);
  91.             }
  92.         } catch (SQLException e) {
  93.             throw new DbException(e.getMessage(), e);
  94.         } finally {
  95.             closeConnection();
  96.         }
  97.         return person;
  98.     }
  99.  
  100.     public List<Person> getAll() {
  101.         List<Person> persons = new ArrayList<Person>();
  102.         try {
  103.             String sql = "SELECT * FROM web3.person";
  104.             initializeStatement(sql);
  105.             ResultSet result = statement.executeQuery();
  106.             while (result.next()) {
  107.                 String name = result.getString("name");
  108.                 String email = result.getString("email");
  109.                 String password = result.getString("password");
  110.                 Person person = new Person(name, email, password);
  111.                 persons.add(person);
  112.             }
  113.         } catch (SQLException e) {
  114.             throw new DbException(e.getMessage(), e);
  115.         } finally {
  116.             closeConnection();
  117.         }
  118.         return persons;
  119.     }
  120.  
  121.     private void initializeStatement(String sql) throws SQLException {
  122.         String url = getProperties().getProperty("url");
  123.         connection = DriverManager.getConnection(url, getProperties());
  124.         statement = connection.prepareStatement(sql);
  125.     }
  126.  
  127.     private void closeConnection() {
  128.         try {
  129.             statement.close();
  130.             connection.close();
  131.         } catch (SQLException e) {
  132.             throw new DbException(e.getMessage(), e);
  133.         }
  134.     }
  135.  
  136.     private Properties getProperties() {
  137.         return properties;
  138.     }
  139.  
  140.     private void setProperties(Properties properties) {
  141.         if(properties == null){
  142.             throw new DbException("No properties given");
  143.         }
  144.         this.properties = properties;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement