使用从其他方法检索到的信息 class
Using retrieved information from method of other class
我已成功从 PostgreSQL 数据库中检索到以下 class 中的数据。 PostgreSQL 中table 行数据被添加到ArrayList<> userlist
中。
我分享了更正后的代码。
class UserList extends ArrayList<User> {
// as in comments said it seems really bad idea is to extend ArrayList. So, avoid it!
}
用户 class 是:
public class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void calculateSimilarityUser(User u){
.... //some codes
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
UserDAO class 是:
public class UserDAO {
static Connection currentCon = null;
static ResultSet rs = null;
private ArrayList<User> userlist = new ArrayList<>();
public ArrayList<User> LoadFromDatabase(){
PreparedStatement userinfoStmt = null;
String loadUsersInfo = "SELECT * FROM userinfo";
try {
currentCon = ConnectionManager.getConnection();
userinfoStmt = currentCon.prepareStatement(loadUsersInfo);
rs = userinfoStmt.executeQuery();
while (rs.next()) {
User us = new User(rs.getString("firstname"), rs.getString("lastname"), rs.getInt("index"));
userlist.add(us);
}
userinfoStmt.close();
currentCon.close();
} catch (Exception e) {
}
return userlist;
}
index.jsp
<%
UserDAO inf= new UserDAO();
ArrayList<User> userList = inf.LoadFromDatabase();
Recommender rc = new Recommender(userList);
%>
扩展 ArrayList 对你的情况没有任何意义。基本上你可以命名你的 class UserDAO
。而且不需要创建class级变量,可以创建局部变量。
public class UserDAO { // no need to extend and changed name to UserDAO
public List<User> retainTopUsersItems(int maxNumOfReturnedResources, List<User> userList) {
List<User> retainList = new ArrayList<User>();
for (int i = 0; i < maxNumOfReturnedResources; i++) {
retainList.add(userList.get(i));
}
return retainList;
}
public List<User> loadFromDatabase() {
Connection userinfoCon = null;
ResultSet userinfoRS = null;
List<User> userlist = new ArrayList<>();
在你 jsp 使用从方法中检索到的列表。
<%
// UserList users = new UserList();
UserDAO inf= new UserDAO();
List<User> userList = inf.loadFromDatabase(); // See the changes
Recommender rc = new Recommender(userList); // Change constructor definition in your Recommender
%>
注意:不要消耗你的异常,要么抛出它,要么记录它?
我已成功从 PostgreSQL 数据库中检索到以下 class 中的数据。 PostgreSQL 中table 行数据被添加到ArrayList<> userlist
中。
我分享了更正后的代码。
class UserList extends ArrayList<User> {
// as in comments said it seems really bad idea is to extend ArrayList. So, avoid it!
}
用户 class 是:
public class User {
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void calculateSimilarityUser(User u){
.... //some codes
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
UserDAO class 是:
public class UserDAO {
static Connection currentCon = null;
static ResultSet rs = null;
private ArrayList<User> userlist = new ArrayList<>();
public ArrayList<User> LoadFromDatabase(){
PreparedStatement userinfoStmt = null;
String loadUsersInfo = "SELECT * FROM userinfo";
try {
currentCon = ConnectionManager.getConnection();
userinfoStmt = currentCon.prepareStatement(loadUsersInfo);
rs = userinfoStmt.executeQuery();
while (rs.next()) {
User us = new User(rs.getString("firstname"), rs.getString("lastname"), rs.getInt("index"));
userlist.add(us);
}
userinfoStmt.close();
currentCon.close();
} catch (Exception e) {
}
return userlist;
}
index.jsp
<%
UserDAO inf= new UserDAO();
ArrayList<User> userList = inf.LoadFromDatabase();
Recommender rc = new Recommender(userList);
%>
扩展 ArrayList 对你的情况没有任何意义。基本上你可以命名你的 class UserDAO
。而且不需要创建class级变量,可以创建局部变量。
public class UserDAO { // no need to extend and changed name to UserDAO
public List<User> retainTopUsersItems(int maxNumOfReturnedResources, List<User> userList) {
List<User> retainList = new ArrayList<User>();
for (int i = 0; i < maxNumOfReturnedResources; i++) {
retainList.add(userList.get(i));
}
return retainList;
}
public List<User> loadFromDatabase() {
Connection userinfoCon = null;
ResultSet userinfoRS = null;
List<User> userlist = new ArrayList<>();
在你 jsp 使用从方法中检索到的列表。
<%
// UserList users = new UserList();
UserDAO inf= new UserDAO();
List<User> userList = inf.loadFromDatabase(); // See the changes
Recommender rc = new Recommender(userList); // Change constructor definition in your Recommender
%>
注意:不要消耗你的异常,要么抛出它,要么记录它?