当前位置: 代码迷 >> J2SE >> 遇到一个开发有关问题,寻求解决方法
  详细解决方案

遇到一个开发有关问题,寻求解决方法

热度:204   发布时间:2016-04-24 01:26:51.0
遇到一个开发问题,寻求解决办法
有如下程序,我想把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();
}


------解决方案--------------------
探讨

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);
}
Sys……
  相关解决方案