?? aqugetrows.java
字號:
package com.v246;
/*
*該類將得到所有以指定JDBC聯接,指定數據表的所有的記錄的值,以二維數級返回xxx[字段代號][字段所有值]
**/
public class AquGetRows
{
private java.sql.Statement stm=null;
private java.sql.ResultSet rs=null;
private java.sql.ResultSetMetaData rsm=null;
private int columnCount;
private int rowCount;
private String rows[][];
public String[][] getRows(java.sql.Connection conn,String table)throws java.sql.SQLException
{
try
{
stm=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs=stm.executeQuery("select * from "+table);
rsm=rs.getMetaData();
}
catch(java.sql.SQLException e)
{
throw new java.sql.SQLException("Initialize Statement or ResultSet or ResultSetMeta error in AquGetRows.class"+e.getMessage());
}
try
{
rs.last();//到記錄最后
rowCount=rs.getRow();//得到記錄數(行數)
rs.first();//移動到記錄最頭前
columnCount=rsm.getColumnCount();//和到該表所有有效字段的數目總和
rows=new String[columnCount][rowCount];//以列數和行數初始化二維數組
}
catch(java.sql.SQLException e1)
{
throw new java.sql.SQLException("Initialize planar arrray error or rs.last or rs.first error in AquGetRows.class"+e1.getMessage());
}
try
{
for(int i=0;i<rowCount;i++)//遍歷所有行
{
for(int j=0;j<columnCount;j++)//遍歷所有例
{
if(rsm.getColumnClassName(j+1).equals("java.lang.Integer"))//如果該例的當前字段為Integer的話(INT型)
{
rows[j][i]=String.valueOf(rs.getInt(j+1));//存儲該字段當前行數的值
}
else if(rsm.getColumnClassName(j+1).equals("java.lang.String"))//如果該例字段為String型的話
{
rows[j][i]=rs.getString(j+1);
}
else if(rsm.getColumnClassName(j+1).equals("java.lang.Boolean"))
{
rows[j][i]=String.valueOf(rs.getBoolean(j+1));
}
}
rs.next();//移動到下一條記錄,然后再分別存儲各字段的值
}
}
catch(java.sql.SQLException e2)
{
throw new java.sql.SQLException("Can't achieve planar Array initialize in AquGetRows.class"+e2.getMessage());
}
finally //關閉數據庫相關聯接
{
if(rs!=null)rs.close();
if(stm!=null)stm.close();
if(conn!=null)conn.close();
}
this.rows=rows;
return rows;
}
public int getRowsCount()
{
return this.rowCount;//返回記錄數(行數)
}
public static void main(String args[])
{
AquGetRows r=new AquGetRows();
String [][] rows=null;
java.sql.Connection conn=null;
Connections connTmp=new Connections();
try
{
conn=connTmp.getConnection();
rows=r.getRows(conn,"class");
}
catch(Exception e)
{
}
for(int i=0;i<rows.length;i++)
{
for(int j=0;j<rows[0].length;j++)
{
System.out.println(rows[i][j]);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -