为什么我的iterator遍历有问题!
[code=Java][/code]package com.dxp;
import com.dxp.*;
import java.sql.*;
import java.util.*;
public class DataSelect {
public static List<String[]> select(String sql)
{
//一行的记录
ResultSet rs = null ;
ArrayList<String[]> l=new ArrayList<String[]>(); //数组al用于存放每
Statement stmt = null ;
// 声明一个结果集对象
// DataBaseConnection为具体的数据库连接及关闭操作类
DataBaseConnection dbc = null;
// 连接数据库
dbc = new DataBaseConnection() ;
try {
stmt=dbc.getConnection().createStatement();
} catch (
SQLException e1) {
System.out.println("创建stmt异常!");
}
// 实例化数据库操作对象
try {
rs = stmt.executeQuery(sql);
String[] rows=new String [6]; //存放每一列的字段值
while(rs.next())
{
int j=0;
for(int i=1;i<7;i++)
{
rows[i-1]=rs.getString(i);
}
l.add(rows);
}
} catch (SQLException e) {
System.out.println("循环异常!");
}
return l;
}
public static void main(String arg[])
{
List<String[]> l=new ArrayList<String[]>();
String sql = null;
int i =1;
sql="select * from infor_note where note_type='记事本'";
l = select(sql);
Iterator it=l.iterator();
while(it.hasNext())
{
String [] str = null;
str = (String [])it.next();
//System.out.println(str[2]);
}
}
}
烦劳懂list的高人帮忙看看!我这个怎么老是只是输出最后一条记录,但是循环的次数是正确的!
不明白,是逻辑有问题!
------解决方案--------------------List循环没有问题,可能是取值的问题。用建议用for循环代码清楚些!
------解决方案--------------------把String[] rows=new String [6]; 放到while循环里