⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbuser.java

📁 Jive Forums 1.0 src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void setProperty(String name, String value) {
        properties.put(name, value);
        saveProperties();
    }

    public ForumPermissions getPermissions(Authorization authorization) {
        if (authorization.getUserID() == userID || userID==-1 || userID==0) {
            return new ForumPermissions(false,false,false,true,false,false,false,false);
        }
        else {
            return ForumPermissions.none();
        }
    }

    public boolean hasPermission(int type) {
        return true;
    }

    public String toString() {
        return username;
    }

    /**
     * Loads user properties from the database.
     */
    private void loadProperties() {
        //If "anonymous" or "all users", do nothing.
        if (userID == -1 || userID == 0) {
            properties = new Properties();
            return;
        }
        synchronized(propertyLock) {
            Properties newProps = new Properties();
            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = DbConnectionManager.getConnection();
                pstmt = con.prepareStatement(LOAD_PROPERTIES);
                pstmt.setInt(1, userID);
                ResultSet rs = pstmt.executeQuery();
                while(rs.next()) {
                    String name = rs.getString("name");
                    String value = rs.getString("propValue");
                    newProps.put(name, value);
                }
            }
            catch( SQLException sqle ) {
                System.err.println("Error in DbUser:loadProperties():" + sqle);
                sqle.printStackTrace();
            }
            finally {
                try {  pstmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            this.properties = newProps;
        }
    }

    /**
     * Saves user properties to the database.
     */
    private void saveProperties() {
        //If "anonymous" or "all users", do nothing.
        if (userID == -1 || userID == 0) {
            return;
        }
        synchronized(propertyLock) {
            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = DbConnectionManager.getConnection();
                //Delete all old values.
                pstmt = con.prepareStatement(DELETE_PROPERTIES);
                pstmt.setInt(1, userID);
                pstmt.execute();
                pstmt.close();
                //Now insert new values.
                pstmt = con.prepareStatement(INSERT_PROPERTY);
                Enumeration enum = properties.keys();
                while (enum.hasMoreElements()) {
                    String name = (String)enum.nextElement();
                    String value = (String)properties.get(name);
                    pstmt.setInt(1, userID);
                    pstmt.setString(2, name);
                    pstmt.setString(3, value);
                    pstmt.executeUpdate();
                }
            }
            catch( SQLException sqle ) {
                System.err.println(sqle);
            }
            finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        }
    }

    /**
     * Load the user data from the database.
     */
    private void loadFromDb() throws UserNotFoundException {
        //If the user is anonymous or "all users", do nothing.
        if (userID == -1 || userID == 0) {
            return;
        }
        // Otherwise, select user data from User table and fill in relevant fields.
        String query;
        //We may want to do a username lookup.
        if (username != null) {
            query = LOAD_USER_BY_USERNAME;
        }
        //Otherwise, a lookup by id
        else {
            query = LOAD_USER_BY_ID;
        }
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(query);
            if (username != null) {
                pstmt.setString(1, username);
            }
            else {
                pstmt.setInt(1, userID);
            }

            ResultSet rs = pstmt.executeQuery();
            if (!rs.next()) {
                throw new UserNotFoundException(
                    "Failed to read user " + userID + " from database."
                );
            }
            this.userID = rs.getInt("userID");
            this.username = rs.getString("username");
            this.passwordHash = rs.getString("passwordHash");
            this.name = rs.getString("name");
            this.nameVisible = (rs.getInt("nameVisible") == 1);
            this.email = rs.getString("email");
            this.emailVisible = (rs.getInt("emailVisible") == 1);
        }
        catch( SQLException sqle ) {
            throw new UserNotFoundException(
                "Failed to read user " + userID + " from database.", sqle
            );
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Inserts a new user record into the database.
     */
    private void insertIntoDb() {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_USER);
            pstmt.setInt(1, userID);
            pstmt.setString(2, username);
            pstmt.setString(3, passwordHash);
            pstmt.setString(4, email);
            pstmt.setInt(5, emailVisible?1:0);
            pstmt.setInt(6, nameVisible?1:0);
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbUser:insertIntoDb()-" + sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Save the user data to the database.
     */
    private void saveToDb() {
        if ( userID == -1 || userID == 0 ) {
            //"anonymous" or "all users", do nothing
            return;
        }
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SAVE_USER);
            pstmt.setString(1, passwordHash);
            pstmt.setString(2, email);
            pstmt.setInt(3, emailVisible?1:0);
            pstmt.setString(4, name);
            pstmt.setInt(5, nameVisible?1:0);
            pstmt.setInt(6, userID);
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println( "SQLException in DbUser.java:saveToDb(): " + sqle );
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -