已知 数据库内 有 多条记录 可直接执行 select * from tablename; 可观察到
但 此段代码内的 select * from tablename 只能拿到 第一条记录 切 list.size()也是1个
代码如下:
public class dao { private static dao instance = new dao();
public static dao getInstance(){
return instance;
}
private dao() {
}
private static Connection getConnection() throws Exception{
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/xe");
return ds.getConnection();
}
public List<bean> select(){
Connection conn = null;
String sql = null;
PreparedStatement pstm = null;
ResultSet rs = null;
List<bean> list = null;
try {
conn = getConnection();
sql = "select * from student";
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
if(rs.next()){
System.out.println("hello");
list = new ArrayList<bean>();
bean b = new bean();
b.setStudent_name(rs.getString(2));
b.setStudent_age(rs.getString(3));
b.setStudent_sexy(rs.getString(4));
list.add(b);
}
System.out.println(list.size()+"======");
rs.close(); pstm.close(); conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
问题:
本人纠结在 当在外部执行 List<bean> select 方法时 只能打印出 一次"hello" 切 list.size也是1个 其他一切 运转正常 请求各位 大虾们 解释一二 为什么 rs.next 只能执行一次
但 此段代码内的 select * from tablename 只能拿到 第一条记录 切 list.size()也是1个
代码如下:
public class dao { private static dao instance = new dao();
public static dao getInstance(){
return instance;
}
private dao() {
}
private static Connection getConnection() throws Exception{
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/xe");
return ds.getConnection();
}
public List<bean> select(){
Connection conn = null;
String sql = null;
PreparedStatement pstm = null;
ResultSet rs = null;
List<bean> list = null;
try {
conn = getConnection();
sql = "select * from student";
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
if(rs.next()){
System.out.println("hello");
list = new ArrayList<bean>();
bean b = new bean();
b.setStudent_name(rs.getString(2));
b.setStudent_age(rs.getString(3));
b.setStudent_sexy(rs.getString(4));
list.add(b);
}
System.out.println(list.size()+"======");
rs.close(); pstm.close(); conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
问题:
本人纠结在 当在外部执行 List<bean> select 方法时 只能打印出 一次"hello" 切 list.size也是1个 其他一切 运转正常 请求各位 大虾们 解释一二 为什么 rs.next 只能执行一次
解决方案 »
- request.isSecure()这个方法是什么意思?
- NetBeanS+Tomcat+EJB+OpenEJB 高手帮帮忙,来看下
- 使用wrapper把一个java应用假如到windows服务中以后,启动服务后,它又停止
- JB2005+JBoss-3.2.1问题
- 怎样在中文客户端访问日文环境的应用程序?
- 请教,用weblogic6时Oracle和SqlServer驱动哪里找?
- 如何用Java编写一个有效的XML生成器?
- 新人含泪求教
- Web项目登录接口速度不稳定
- 上线java应用cpu异常通过mat分析后的结果疑问
- android 每点击一次地图 产生一个标注,如何写
- 提交form表单action代码执行两次?!!!!!!!是struts2
这里都list=new ArrayList();
这样肯定不行啊。
每次有记录之后,你都重新创建了一个list
}List<Bean> list=new ArrayList<Bean>();
把这个放在if(rs.next)的前面!
但现在 只能打印一个 根本就是 executeQuery() 只运行了 一次而已
我靠 犯了一个 具土的 错误 不能使用 if 循环 而是 使用while 死循环 一直到 rs.next 没有值为止
该死的 循环 纠结了我 大半天的 时间
LZ,记得散发,呵呵!
将list = new ArrayList<bean>();在控制语句外层New