About the application of the ResultSetMetadata interface

xiaoxiao2021-03-06  44

These days are reported systems, there are many reports, and a data sheet for a stored report can have dozens of fields. How to get data from the database into a trouble, because the report can be dynamically added, so the data table is created at runtime. It is more no need to say the field in the table.

The method usually obtains data from the database is to first load the database driver, then establish a connection with the Connection object, and perform the query after obtaining the STATEMENT object. This time the problem is coming. If you have a getList () method to get records in the database, then if you want to return to RESULTSET? Obviously, this approach is not available. Open ResultSet objects inevitably occupy resources, at least one connection, and the default connection to the JBoss server used when developing B / S applications is twenty, executing database operation It is not appropriate to close the ResuleSet and Connection objects. This consequence is conceivable. So the name value of HashMap is usually used when obtaining database data. If you want to get a record collection, you can place the HashMap into the Collection.

First, here you have to pay attention to some resultSet's getxxxxxxxxxxxxx () method, which can be passed into a value, or you can pass a string type field name, but if you don't know the name of this field? Naturally, you won't know what this field means. Therefore, in such a system, it is necessary to use the ResultSetMetadata interface to obtain some information of the result set, such as the field name. In the following text, some programs will explain how to use the ResultSetMetadata interface.

A method for acquiring all fields in the data table, (here I am directly debuging in JBoss, you can see the run results directly at the console, and these run results are also recorded in the log file, if JBoss is running in the default mode running log InstallDir / Server / Default / log / server.log, and other databases are Oracle 9i) Here: RFM_CATALOG (Report Directory Table) Yes, you don't have to install the Oracle database on your machine, so please modify the driver, connect the URL, log in to passwords, and account. The procedure is as follows:

// Import the required package

Import java.sql. *;

Import javax.sql. *;

Import java.io. *;

Import java.lang. *;

Import java.sql. *;

Import java.util. *;

Class mytest

{

// master function

Public static void main (string [] args)

{

// Declare connection object

Connection con = NULL;

String SQL = "";

Try

{

// Load the driver, here you need to pay attention, you need to copy the driver's JAR file to the JRE / lib / ext directory of J2SDK.

Class.Forname ("Oracle.jdbc.driver.OracleDriver");

// Get database connection objects

Con = DriverManager.getConnection ("JDBC: Oracle: Thin: @

10.1.51

.82: 1521: Myortest "," System "," ABC123 ");

System.out.println ("Driver Load, Successful ..."); Statement Stmt = Con.createStatement ();

SQL = "SELECT * from amhrmisdb.rfm_catalog";

// Query get record set

ResultSet RS = Stmt.executeQuery (SQL);

// Get the ResultSetMetadata object

ResultSetmetaData RSMD = rs.getMetadata ();

INT in = = rsmd.getcolumncount ();

// Number of fields

System.out.println ("Number of Fields: RS_LEN);

INT i = 0;

// Get field names and some of relevant data

For (i = 1; i <= 8; i )

{

System.out.println ("field _label _" i ": rsmd.getColumnlabel (i));

System.out.println ("field _name _" i ":" rsmd.getColumnname (i));

System.out.println ("field_type _" i ":" rsmd.getcolumntepe (i) "_" rsmd.getColumnTypename (i));

}

}

Catch (Exception EX)

{

System.out.println ("Database Operation Error:" EX);

}

}

}

This program is for everyone to study, and you can see the J2SDK documentation about ResultSetMetadata.

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

New Post(0)