Convert the recording set to two-dimensional array or special string

xiaoxiao2021-03-06  152

Import java.sql.resultset; import java.sql.resultsetmetata; import java.util.arrayList; / ** * This class packaged the operation of the record set * Time: 2004-07-07 * @Author: Dong Zhengli * @ Version 1.0 * / public class {// Record set private resultset q; // Record set details Private resultsetMetadata rsmd; // Record the number of private int numcols; // Record the number of lines of the list Price;

/ ** * Constructor * / public {} / ** * Initialization record set * / public void set (resultset = r; try {r = rs.getMetadata (); Numcols = rsmd .getColumncount ();} catch (exception ex) {system.out.println ("DZL: RS -> setrs:" ex.toT7tring ());}} / ** * Return to the number of columns * / Public int getColcount () {Return Numcols;

/ ** * Get recording collection line * / public int achievecount () {nuMrows = -1; try {r.last (); nuMrows = rs.getrow (); rs.beforefirst ();} catch (Exception EX) {System.out.println ("DZL: RS -> getColcount:" EX.TOSTRING ());} Return NumRows;} / ** * Returns all column names of the specified record set * / public string [] getColname ) {String md [] = new string [numcols]; try {for (int i = 1; i

{

MD [i-1] = rsmd.getColumnname (i);

}

}

Catch (Exception EX)

{

System.out.println ("DZL: RS -> getColname:" ex.totring ());

}

Return MD;

}

/ **

* Copy the contents of the line to the array

* /

Private string [] getrowValue ()

{

String [] row = new string [numcols];

Try

{

For (int i = 1; i

{

ROW [i-1] = rs.getstring (i);

}

}

Catch (Exception EX)

{

System.out.println ("DZL: RS -> GetrowValue:" EX.TOSTRING ());

}

Return row;

}

/ **

* Copy the data in the record set to an array

* /

Public String [] [] getArray (Boolean Hastitle)

{

ArrayList Al = New ArrayList ();

String [] [] allvalue = null;

Try

{

IF (Hastitle)

{

Al.Add (getColname ());}

While (rs.next ())

{

Al.Add (GetrowValue ());

}

Rs.beforefirst ();

Object [] ArryObject = (Object []) al.toArray ();

Int rowlen = arryobject.length;

INT collen = Numcols;

Allvalue = new string [rowlen] [collen];

String [] RowValue = NULL;

For (int i = 0; i

{

RowValue = (String []) ArryObject [i];

For (int J = 0; J

{

Allvalue [i] [j] = rowvalue [j];

}

}

Return AllValue;

}

Catch (Exception EX)

{

System.out.println ("DZL: RS -> GetArray:" EX.TOSTRING ());

}

Return AllValue;

}

/ **

* Convert the recording set into strings

* /

Public String Tostring (String RowToken, String Coltoken, Boolean Hastitle "Throws Exception

{

StringstrallValue = ""

String [] AllValue = GetArray (Hastitle);

For (int i = 0; i

{

For (int J = 0; J

{

StrallValue = AllValue [I] [J] COLTOKEN

}

StrallValue = strallue.substring (0, strallue.length () - 1) rowtoke;

}

Strallue = strallue.substring (0, Strallue.Length () - 1);

Return StrallValue;

}

/ ** * Close the record set * / public void closers () {try {r.close (); rs = null;} catch (exception ex) {system.out.println ("DZL: RS -> Close:" EX.TOSTRING ());}}} The above code mainly implements some advanced methods for ResultSet, there are many deficiencies, I hope everyone will advise.

转载请注明原文地址:https://www.9cbs.com/read-128012.html

New Post(0)