📄 customuser.java
字号:
import com.jivesoftware.base.*;import javax.sql.DataSource;import java.sql.*;/** * A User implementation. It uses a custom database table ("person"). This table * contains a unique user id, unique username, first name, last name, and email * address for each user in the system. The table doesn't contain Jive specific * fields and properties are loaded from the Jive database.<p> */public class CustomUser extends SimpleUserAdapter { private static final String EXT_FIND_BY_USERNAME = "SELECT id, login, firstName, lastName, email FROM person WHERE username=?"; private static final String EXT_FIND_BY_ID = "SELECT id, login, firstName, lastName, email FROM person WHERE id=?"; /** * Load a user by id. * * @param id the user id. * @throws UserNotFoundException if the user could not be loaded. */ public CustomUser(long id, DataSource dataSource) throws UserNotFoundException { if (id < 1) { throw new UserNotFoundException(); } this.ID = id; loadFromDb(dataSource); } /** * Load a user by username. * * @param username the username. * @throws UserNotFoundException if the user could not be loaded. */ public CustomUser(String username, DataSource dataSource) throws UserNotFoundException { if (username == null) { throw new UserNotFoundException("Username is null"); } this.username = username; loadFromDb(dataSource); } /** * Loads all user fields from the external user database. * * @throws UserNotFoundException */ private void loadFromDb(DataSource dataSource) throws UserNotFoundException { // database code here. Connection conn = null; PreparedStatement pstmt = null; try { conn = dataSource.getConnection(); if (ID > 0){ pstmt = conn.prepareStatement(EXT_FIND_BY_ID); pstmt.setLong(1, ID); } else { pstmt = conn.prepareStatement(EXT_FIND_BY_USERNAME); pstmt.setString(1, username); } ResultSet rs = pstmt.executeQuery(); if (!rs.next()) { throw new UserNotFoundException(); } ID = rs.getLong("id"); username = rs.getString("login"); // Jive uses a single name field. Therefore, we just concatenate // the firstName and lastName fields together. name = rs.getString("firstName") + " " + rs.getString("lastName"); email = rs.getString("email"); } catch (SQLException sqle) { Log.error(sqle); } finally { try { pstmt.close(); } catch (Exception e) { Log.error(e); } try { conn.close(); } catch (Exception e) { Log.error(e); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -