当前位置: 代码迷 >> Eclipse >> ResultSet转二维数组,getString时出现SQL错误。(已经rs.next()了)
  详细解决方案

ResultSet转二维数组,getString时出现SQL错误。(已经rs.next()了)

热度:15   发布时间:2016-04-23 13:32:31.0
ResultSet转二维数组,getString时出现SQL异常。(已经rs.next()了)
各位大虾们请帮我分析一下我的问题:
我写了一个方法,输入是ResultSet,输出Sting[][]。
然后我写了一个测试方法进行测试,发现getString()为空值。
我在getString之前已经rs.next()过了。
测试方法中,输入的rs有25列,258条记录。
请看代码:

/*
 *@我写的由ResultSet转成String[][]的方法,为了构造JTable的Modle
*/
public static String[][] getDataArrayFromResultSet(ResultSet rs){
int columnCount = getFieldCountFromResultSet(rs);
int rowCount = getRecordCountFromResultSet(rs);
String[][] rtn = new String[rowCount][columnCount];
try{
System.out.println("字段数为:" + columnCount);
System.out.println("记录数为:" + rowCount);
System.out.println("ResultSet是否为空:" + (rs==null));

for(int row_index = 0;row_index < rowCount;row_index++){
rs.next();
 
System.out.println("进入行循环");  
 
for( int column_index = 0;column_index < columnCount;column_index++){ 
 
System.out.println("进入列循环");
 
String temp = rs.getString(column_index + 1);
 
System.out.println("temp的值为:" + temp);
 
rtn[row_index][column_index] = temp;
}
}
return rtn;
}
catch(SQLException sqle){System.out.println(sqle.getMessage());return null;}
}



/*
 *@以下是测试代码,与上述方法在同一类中。
*/
public static void testDanielClassSQL(){
String driver = "net.sourceforge.jtds.jdbc.Driver";
String url = "jdbc:jtds:sqlserver://localhost/DemoDB";
String user = "sa";
String pass = "mima";
String sqlString = "select * from sysobjects where type = 'U' order by name";

Connection con = getConnection(driver,url,user,pass);
ResultSet rset = getResultSetFromSelectStatement(sqlString,con);

String[][] data = getDataArrayFromResultSet(rset);
System.out.println(data);
}


Connection和ResultSet都正常获得。我用以上测试方法进行测试之后输出结果为:

字段数为:25
记录数为:258
ResultSet是否为空:false
进入行循环
进入列循环
No current row in the ResultSet.
null

也就是说,在getString上出了异常“No current row in the ResultSet.”。
但是我getRecordCountFromResultSet(rs)却是258,说明有纪录啊。我就奇怪了。
 "select * from sysobjects where type = 'U' order by name" 肯定是有值的,SQLServer里可以证明。

各位朋友能给我解释一下我如何取得这个二维数组吗?万分感谢!!

中秋快乐!

------解决方案--------------------
在企业中,没有人像你这样搞
一般 是将数据库查出来的数据 放入,Collection
中,
public Vector seleteProductInfo(String proName) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String strSql = "select * from productInfo where productName like'" +
proName + "'";
Vector voAll = new Vector();
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);
rs = pstmt.executeQuery();
while (rs.next()) {
ProductInfoBean product = new ProductInfoBean();
product.setProductId(rs.getInt("productId"));
product.setProductName(rs.getString("productName"));
product.setPrice(rs.getFloat("price"));
product.setAmount(rs.getInt("amount"));
product.setUserId(rs.getInt("userId"));
product.setImageName(rs.getString("imageName"));
product.setImage(rs.getString("image"));
product.setRemark(rs.getString("remark"));
voAll.add(product);
}
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt, rs);
return voAll;
}
}
}
  相关解决方案