Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. public class ProfileDAOImpl implements ProfileDAO {
  2. Connection connection = null;
  3. PreparedStatement preparedStatement = null;
  4.  
  5. public void getConn() throws DAOException {
  6. try (FileReader reader = new FileReader("db.properties")) {
  7. Properties properties = new Properties();
  8. properties.load(reader);
  9. String user = properties.getProperty("user");
  10. String password = properties.getProperty("password");
  11. String dburl = properties.getProperty("dburl");
  12. String driver = properties.getProperty("driver");
  13. Class.forName(driver);
  14. if (connection == null) {
  15. connection = DriverManager.getConnection(dburl, user, password);
  16. }
  17. } catch (IOException e) {
  18. throw new DAOException("file error ",e);
  19. } catch (ClassNotFoundException e) {
  20. throw new DAOException("class error ",e);
  21. } catch (SQLException e) {
  22. throw new DAOException("conn error ",e);
  23. }
  24. }
  25. private PreparedStatement getPreparedStatement(String sql) throws DAOException{
  26. if(preparedStatement == null) {
  27. try {
  28. preparedStatement = connection.prepareStatement(sql);
  29. } catch (Exception e) {
  30. throw new DAOException("create ps error ",e);
  31. }
  32. } return preparedStatement;
  33. }
  34. public void updateProfile(Profile profile) throws DAOException {
  35. String sql = "UPDATE Profile SET user_name=?," +
  36. " nick_name=?, user_mail=?, password=? WHERE id=?";
  37. try {
  38. getPreparedStatement(sql);
  39. preparedStatement.setString(1, profile.getUserName());
  40. preparedStatement.setString(2, profile.getNickName());
  41. preparedStatement.setString(3, profile.getUserMail());
  42. preparedStatement.setString(4, profile.getPassword());
  43. preparedStatement.setInt(5, profile.getId());
  44. preparedStatement.executeUpdate();
  45. } catch (Exception e) {
  46. throw new DAOException("update error ",e);
  47. }
  48. }
  49. public void closeConnection() throws DAOException{
  50. try {
  51. if (preparedStatement != null) {
  52. preparedStatement.close();
  53. }
  54. } catch (Exception e) {
  55. throw new DAOException("Close (ps) error ",e);
  56. }
  57. try {
  58. if (connection != null) {
  59. connection.close();
  60. }
  61. } catch (Exception e) {
  62. throw new DAOException("Close (conn) error",e);
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement