Java Mysql的数据存入List<T>,怎么在其他类读取?
publicstaticList<UserInfo>checkLoginInfo(Stringusrename){ConnectiongetConnetcion=(Con...
public static List<UserInfo> checkLoginInfo(String usrename) {
Connection getConnetcion = (Connection) jdbc.getConnection();
String sql = "SELECT* from user where User=" + "'" + usrename + "'";
PreparedStatement pstmt;
List<UserInfo> getUserInfoList = new ArrayList<UserInfo>();
try {
pstmt = getConnetcion.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
UserInfo User = new UserInfo();
User.setUsername(rs.getString("User"));
User.setPassword(rs.getString("password"));
getUserInfoList.add(User);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return getUserInfoList;
}
Test类
UserInfo list = new UserInfo();
List<UserInfo> User = getUserInfo.checkLoginInfo("root");
for (int i = 0; i < User.size(); i++) {
list.getPassword();
System.out.println(User.get(i));
}
输出的结果
20:58:04,062 INFO 数据库加载成功(..
20:58:04,280 INFO 数据库加载成功()..
entity.UserInfo@1175e2db
记得是 User.getUsername();啥的。 展开
Connection getConnetcion = (Connection) jdbc.getConnection();
String sql = "SELECT* from user where User=" + "'" + usrename + "'";
PreparedStatement pstmt;
List<UserInfo> getUserInfoList = new ArrayList<UserInfo>();
try {
pstmt = getConnetcion.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
UserInfo User = new UserInfo();
User.setUsername(rs.getString("User"));
User.setPassword(rs.getString("password"));
getUserInfoList.add(User);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return getUserInfoList;
}
Test类
UserInfo list = new UserInfo();
List<UserInfo> User = getUserInfo.checkLoginInfo("root");
for (int i = 0; i < User.size(); i++) {
list.getPassword();
System.out.println(User.get(i));
}
输出的结果
20:58:04,062 INFO 数据库加载成功(..
20:58:04,280 INFO 数据库加载成功()..
entity.UserInfo@1175e2db
记得是 User.getUsername();啥的。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询