Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ProfileDAOImpl implements ProfileDAO {
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- public void getConn() throws DAOException {
- try (FileReader reader = new FileReader("db.properties")) {
- Properties properties = new Properties();
- properties.load(reader);
- String user = properties.getProperty("user");
- String password = properties.getProperty("password");
- String dburl = properties.getProperty("dburl");
- String driver = properties.getProperty("driver");
- Class.forName(driver);
- if (connection == null) {
- connection = DriverManager.getConnection(dburl, user, password);
- }
- } catch (IOException e) {
- throw new DAOException("file error ",e);
- } catch (ClassNotFoundException e) {
- throw new DAOException("class error ",e);
- } catch (SQLException e) {
- throw new DAOException("conn error ",e);
- }
- }
- private PreparedStatement getPreparedStatement(String sql) throws DAOException{
- if(preparedStatement == null) {
- try {
- preparedStatement = connection.prepareStatement(sql);
- } catch (Exception e) {
- throw new DAOException("create ps error ",e);
- }
- } return preparedStatement;
- }
- public void updateProfile(Profile profile) throws DAOException {
- String sql = "UPDATE Profile SET user_name=?," +
- " nick_name=?, user_mail=?, password=? WHERE id=?";
- try {
- getPreparedStatement(sql);
- preparedStatement.setString(1, profile.getUserName());
- preparedStatement.setString(2, profile.getNickName());
- preparedStatement.setString(3, profile.getUserMail());
- preparedStatement.setString(4, profile.getPassword());
- preparedStatement.setInt(5, profile.getId());
- preparedStatement.executeUpdate();
- } catch (Exception e) {
- throw new DAOException("update error ",e);
- }
- }
- public void closeConnection() throws DAOException{
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- } catch (Exception e) {
- throw new DAOException("Close (ps) error ",e);
- }
- try {
- if (connection != null) {
- connection.close();
- }
- } catch (Exception e) {
- throw new DAOException("Close (conn) error",e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement