Return 从数据库查询到 LinkedHashMap 的值
Return values from database queries into a LinkedHashMap
我不断从这个 LinkedHashMap 中获取一个值,它是第一个 [if (resultset.next()) ] 或最后一个 [while(resultset.next()) ],只有一个结果出现回来,但我想要完整的地图。如何 return table 中符合我的条件的所有行?任何帮助将不胜感激。
/** A method to list all previous statuses for a certain user given the userID */
public StringBuilder showStatusHistory(int userID) {
try {
Date date;
String status;
preparedStatement = createStatement("select statusDate,statusText from statusTable where userID = ?");
preparedStatement.setInt(1,userID);
resultSet = preparedStatement.executeQuery();
StringBuilder allStatus = new StringBuilder("");
statusesList = new LinkedHashMap<>();
while (resultSet.next()){
date = resultSet.getDate(1);
status = resultSet.getString(2);
statusesList.put(date,status);
}
Set<Date> statusTime = statusesList.keySet();
for(Date k:statusTime){
allStatus.append(k+" "+statusesList.get(k));
}
return allStatus;
}
catch (SQLException sqlE){
sqlE.printStackTrace();
}
return null;
/** 辅助方法用于最小化代码重复。它肯定有效 */
private PreparedStatement createStatement(String query) {
try {
connection = DB_ConnectionConfiguration.getConnection();
this.query = query;
preparedStatement = connection.prepareStatement(query);
return preparedStatement;
}
catch (SQLException sqlE) {
sqlE.printStackTrace();
return null;
}
}
下面是示例代码中的一些建议...
public StringBuilder showStatusHistory(int userID) {
// define at top and return "" if no results
StringBuilder allStatus = new StringBuilder("");
try {
Date date;
String status;
// define local in the method
PreparedStatement preparedStatement = createStatement("select statusDate,statusText from statusTable where userID = ?");
preparedStatement.setInt(1, userID);
// define local in the method
ResultSet resultSet = preparedStatement.executeQuery();
// append to the all Status here and skip the other loop
// if you need a distinct list (like you had fro the Set) you could use a DISTINCT on the SQL statement.
while (resultSet.next()) {
date = resultSet.getDate(1);
status = resultSet.getString(2);
// append to the allStatus here
allStatus.append(date).append(" ").append(status); // add a newline to the end? append(System.lineSeparator())
}
} catch (SQLException sqlE) {
sqlE.printStackTrace();
// do you want to throw this if something bad happened?
} finally {
// close up your jdbc resources
}
// will contain "" if no records or concatenated statuses from the resultset
return allStatus;
}
我不断从这个 LinkedHashMap 中获取一个值,它是第一个 [if (resultset.next()) ] 或最后一个 [while(resultset.next()) ],只有一个结果出现回来,但我想要完整的地图。如何 return table 中符合我的条件的所有行?任何帮助将不胜感激。
/** A method to list all previous statuses for a certain user given the userID */
public StringBuilder showStatusHistory(int userID) {
try {
Date date;
String status;
preparedStatement = createStatement("select statusDate,statusText from statusTable where userID = ?");
preparedStatement.setInt(1,userID);
resultSet = preparedStatement.executeQuery();
StringBuilder allStatus = new StringBuilder("");
statusesList = new LinkedHashMap<>();
while (resultSet.next()){
date = resultSet.getDate(1);
status = resultSet.getString(2);
statusesList.put(date,status);
}
Set<Date> statusTime = statusesList.keySet();
for(Date k:statusTime){
allStatus.append(k+" "+statusesList.get(k));
}
return allStatus;
}
catch (SQLException sqlE){
sqlE.printStackTrace();
}
return null;
/** 辅助方法用于最小化代码重复。它肯定有效 */
private PreparedStatement createStatement(String query) {
try {
connection = DB_ConnectionConfiguration.getConnection();
this.query = query;
preparedStatement = connection.prepareStatement(query);
return preparedStatement;
}
catch (SQLException sqlE) {
sqlE.printStackTrace();
return null;
}
}
下面是示例代码中的一些建议...
public StringBuilder showStatusHistory(int userID) {
// define at top and return "" if no results
StringBuilder allStatus = new StringBuilder("");
try {
Date date;
String status;
// define local in the method
PreparedStatement preparedStatement = createStatement("select statusDate,statusText from statusTable where userID = ?");
preparedStatement.setInt(1, userID);
// define local in the method
ResultSet resultSet = preparedStatement.executeQuery();
// append to the all Status here and skip the other loop
// if you need a distinct list (like you had fro the Set) you could use a DISTINCT on the SQL statement.
while (resultSet.next()) {
date = resultSet.getDate(1);
status = resultSet.getString(2);
// append to the allStatus here
allStatus.append(date).append(" ").append(status); // add a newline to the end? append(System.lineSeparator())
}
} catch (SQLException sqlE) {
sqlE.printStackTrace();
// do you want to throw this if something bad happened?
} finally {
// close up your jdbc resources
}
// will contain "" if no records or concatenated statuses from the resultset
return allStatus;
}