有如下程序,我想把rs.next之后的值放到list中,使list打印出的值为:[{PASSWORD=www123, USERNAME=www}]
public static void main(String[] args) {
List list = null;
String sql = "select * from test";
Connection conn = null;
PreparedStatement psmt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jsdb","test","test");
psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
while(rs.next()) {
rs.getString(1));
rs.getString(2));
System.out.println("*********"+list);
}
}catch(Exception e) {
e.printStackTrace();
}
使list打印出的值为:[{PASSWORD=www123, USERNAME=www}]
请问该如何实现,在此不胜感激!
------解决方案--------------------
写个类 比如
public class User{
private String password;
private STring username;
//对应的set和get方法
}
while(rs.next()) {
User user=new User();
user.setPassword(rs.getString(1));
user.setUsername(rs.getString(2));
list.add(user);
System.out.println("*********"+list);
}
------解决方案--------------------
- Java code
List<String[]> list = new ArrayList<String[]>();String[] strs =;while(rs.next()) { strs = new String[2]; str[0] = rs.getString(1); str[1] = rs.getString(2); list.add(str);} System.out.println("*********"+list);
------解决方案--------------------
public static void main(String[] args) {
List list = null;
String sql = "select * from test";
Connection conn = null;
PreparedStatement psmt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jsdb","test","test");
psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
List<String[]> list = new ArrayList<String[]>();
String[] strs = null;
while(rs.next()) {
strs = new String[2];
strs[0] = rs.getString(1);
strs[1] = rs.getString(2);
list.add(strs);
}
System.out.println("*********"+list);
}catch(Exception e) {
e.printStackTrace();
}
------解决方案--------------------