Connection (Connect) to OBD (java JDBC DAO)



  • Hello, please help with the program malfunction, get it. NullPointerException в строке preparedStatement = connection.prepareStatement(sql); I suspect a problem in connection, in method. getConnbut I'm not sure.

    public class ProfileDAOImpl implements ProfileDAO {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
    
        public Connection getConn() throws Exception{
            Properties properties = new Properties();
            try {
                properties.load(new FileInputStream("info.properties"));
                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(user, password, dburl);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
        private PreparedStatement getPreparedStatement(String sql) throws DAOException{
            if(preparedStatement == null) {
                try {
                    preparedStatement = connection.prepareStatement(sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } return preparedStatement;
        }
        public void updateProfile(Profile profile) throws DAOException {
            String sql = "UPDATE profiles SET id=?, user_name=?," +
                    " nick_name=?, user_mail=?, password=?;";
            try {
                getPreparedStatement(sql);
                preparedStatement.setInt(1, profile.getId());
                preparedStatement.setString(2, profile.getUserName());
                preparedStatement.setString(3, profile.getNickName());
                preparedStatement.setString(4, profile.getUserMail());
                preparedStatement.setString(5, profile.getPassword());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    public void closeConnection() {
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    }

    public interface ProfileDAO {
    public void updateProfile(Profile profile) throws DAOException;
    public void closeConnection();
    

    }

    public class Main {
    public static void main(String[] args) throws DAOException {
        ProfileDAO profileDAO = new ProfileDAOImpl();
        Profile profile = new Profile(2,"Test","Nick","test@test","123q");
        profileDAO.updateProfile(profile);
        profileDAO.closeConnection();
    }
    

    }



  • As noted in the commentaries, you do not produce a method. getConn() - from here. NullPointerException

    If you have connection and preparedStatement It's always reused, but it's better to move their initialization to the designer. Then ProfileDAOImpl This'll work out:

    public class ProfileDAOImpl implements ProfileDAO {
        final Connection connection;
        final PreparedStatement updateProfileStatement;
    
    public ProfileDAOImpl() throws Exception {
        Properties properties = new Properties();
        properties.load(new FileInputStream("info.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String dburl = properties.getProperty("dburl");
        String driver = properties.getProperty("driver");
        Class.forName(driver);
        this.connection = DriverManager.getConnection(user, password, dburl);
        this.updateProfileStatement = connection.prepareStatement("UPDATE profiles SET id=?, user_name=?," +
                " nick_name=?, user_mail=?, password=?");
    }
    
    public void updateProfile(Profile profile) throws DAOException {
        try {
            updateProfileStatement.setInt(1, profile.getId());
            updateProfileStatement.setString(2, profile.getUserName());
            updateProfileStatement.setString(3, profile.getNickName());
            updateProfileStatement.setString(4, profile.getUserMail());
            updateProfileStatement.setString(5, profile.getPassword());
            updateProfileStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public void closeConnection() {
        try {
            if (updateProfileStatement != null) {
                updateProfileStatement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2