daily pastebin goal
90%
SHARE
TWEET

Untitled

a guest Jan 20th, 2017 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 domain.Person;
  13.  
  14. public class PersonDatabase implements PersonRepository {
  15.  
  16.     private Properties properties;
  17.     private String url;
  18.     private PreparedStatement statement;
  19.     private Connection connection;
  20.  
  21.     public PersonDatabase() {
  22.         properties = new Properties();
  23.         url = "jdbc:postgresql://gegevensbanken.khleuven.be:51516/2TX31";
  24.         properties.setProperty("user", "<uw r nummer>");
  25.         properties.setProperty("password", "<uw passwoord>");
  26.         properties.setProperty("ssl", "true");
  27.         properties.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");
  28.  
  29.         try {
  30.             Class.forName("org.postgresql.Driver");
  31.             connection = DriverManager.getConnection(url, properties);
  32.  
  33.         } catch (SQLException e) {
  34.             throw new DatabaseException(e.getMessage(), e);
  35.         } catch (ClassNotFoundException e) {
  36.             throw new DatabaseException(e.getMessage(), e);
  37.         }
  38.  
  39.     }
  40.  
  41.     @Override
  42.     public void add(Person person) {
  43.  
  44.         if(person == null){
  45.             throw new DatabaseException("person not valid");
  46.         }
  47.        
  48.         String sql = "INSERT into <uw schema.uw tabel> (userid, password, email, firstname, lastname)"
  49.                     + "VALUES (?,?,?,?,?)";
  50.         try{
  51.             statement = connection.prepareStatement(sql);
  52.             statement.setString(1, person.getUserid());
  53.             statement.setString(2, person.getPassword());
  54.             statement.setString(3, person.getEmail());
  55.             statement.setString(4, person.getFirstName());
  56.             statement.setString(5, person.getLastName());  
  57.             statement.execute();
  58.         } catch (SQLException e){
  59.             throw new DatabaseException(e.getMessage(), e);
  60.         }
  61.     }
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