Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. public Connection getConn() throws Exception{
  2. Properties properties = new Properties();
  3. try {
  4. properties.load(new FileInputStream("info.properties"));
  5. String user = properties.getProperty("user");
  6. String password = properties.getProperty("password");
  7. String dburl = properties.getProperty("dburl");
  8. String driver = properties.getProperty("driver");
  9. Class.forName(driver);
  10. if (connection == null) {
  11. connection = DriverManager.getConnection(user, password, dburl);
  12. }
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. } catch (ClassNotFoundException e) {
  16. e.printStackTrace();
  17. } catch (SQLException e) {
  18. e.printStackTrace();
  19. }
  20. return connection;
  21. }
  22. private PreparedStatement getPreparedStatement(String sql) throws DAOException{
  23. if(preparedStatement == null) {
  24. try {
  25. preparedStatement = connection.prepareStatement(sql);
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. } return preparedStatement;
  30. }
  31. public void updateProfile(Profile profile) throws DAOException {
  32. String sql = "UPDATE profiles SET id=?, user_name=?," +
  33. " nick_name=?, user_mail=?, password=?;";
  34. try {
  35. getPreparedStatement(sql);
  36. preparedStatement.setInt(1, profile.getId());
  37. preparedStatement.setString(2, profile.getUserName());
  38. preparedStatement.setString(3, profile.getNickName());
  39. preparedStatement.setString(4, profile.getUserMail());
  40. preparedStatement.setString(5, profile.getPassword());
  41. preparedStatement.executeUpdate();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. public void closeConnection() {
  47. try {
  48. if (preparedStatement != null) {
  49. preparedStatement.close();
  50. }
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54. try {
  55. if (connection != null) {
  56. connection.close();
  57. }
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. }
  62.  
  63. public interface ProfileDAO {
  64. public void updateProfile(Profile profile) throws DAOException;
  65. public void closeConnection();
  66.  
  67. public class Main {
  68. public static void main(String[] args) throws DAOException {
  69. ProfileDAO profileDAO = new ProfileDAOImpl();
  70. Profile profile = new Profile(2,"Test","Nick","test@test","123q");
  71. profileDAO.updateProfile(profile);
  72. profileDAO.closeConnection();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement