日期:2014-05-18  浏览次数:20680 次

如何将数据库中的数据提取出来,并赋值给二维数组
??求完整代码,谢谢大家
答案明白了,立即结分

------解决方案--------------------
这个不会,得好好看书了...
------解决方案--------------------
循环嘛
------解决方案--------------------
你能不能给我具体点的要求 呢?

------解决方案--------------------
楼主说的需求很含糊,不是很清楚什么意思。以前在公司做过,把数据从数据库中取出来,放到一个只有两列的二维数组,用于在页面上生成下拉列表框。大概是这个样子的:

先把数据从数据库中取到一个名叫list的List中,然后,

if (list == null || list.isEmpty()) return null;

String[][] arr = new String[list.size()][2];

for (int i = 0, j = list.size(); i < j; i++ ) {
DTO dto = (DTO) iterator.next();

arr[i][0] = dto.getLable();//label、name都是dto的属性,String类型的
arr[i][1] = dto.getName();

}
------解决方案--------------------
public String[][] ListData(String sql,int fieldnum) throws SQLException{

String result[][] = null;
Connection dbconn = null;
Statement stmt = null;
ResultSet rsResult = null;
String strSql = null;
StrCvs strTo = new StrCvs();

if(sql.length()==0 || fieldnum ==0){
result = null;
}
else
{
strSql = sql;
try
{
dbconn = DbOperation.getConnection();
stmt = dbconn.createStatement(1004, 1007);

//strSql = "select * from sysconfig where syscon_sign=0 order by syscon_explain ";

System.out.println( "sql: " +strTo.UnToGB(strSql));
rsResult = stmt.executeQuery(strSql);

System.out.println(strSql+ "数据查询已完成! ");
if(rsResult.next())
{
rsResult.absolute(-1);
int iRowNum = rsResult.getRow();
result = new String[iRowNum][fieldnum];
rsResult.first();
for(int i = 0; i < iRowNum; i++)
{
for(int j = 0;j <fieldnum;j++)
{
if(rsResult.getString(j+1)==null || rsResult.getString(j+1).length() == 0 || rsResult.getString(j+1).equals( "null ")){
result[i][j] = " ";
}else{
//result[i][j] = strTo.UnToGB(rsResult.getString(j+1));
result[i][j] = rsResult.getString(j+1);
}
}
rsResult.next();
}
}

}
catch(Exception e)
{
result = null;
System.out.println(e.getMessage());
}
finally
{
if (rsResult!=null){
rsResult.close();
rsResult=null;
}
if (stmt!=null){
stmt.close();
stmt=null;
}
if (dbconn!=null){
dbconn.close();
dbconn=null;
}
}
}
return result;
}
------解决方案--------------------
偶实现了,VB.NET的.
从数据库读出数据放在DATASET上,
再对DATASET循环,读出的数据就能放在数组上了..

------解决方案--------------------