Use resultsetmetadata to display the field name and other information

Source: Internet
Author: User
Package com. test;
Import java. SQL .*;

/***//********************************** ******
* This example illustrates the problem: JDBC development process -- display the field name and other information using resultsetmetadata
* Note: The driver used in this example is a JDBC-ODBC bridge driver.
****************************************/

Class hello
...{
Public static void main (string ARGs [])
...{
Try
...{
/***//**
* Step 1: load the JDBC driver;
*/
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");

/***//**
* Step 2: establish a connection;
* Note: the data source name bookdsn needs to be created for the sqlserver database book in this step.
*/
String url = "JDBC: ODBC: bookdsn ";
Connection con = drivermanager. getconnection (URL, "sa ","");

/***//**
* Step 3: Create a statement, execute the query, and obtain the result set;
*/
Statement stmt = con. createstatement ();
Resultset rs1_stmt.exe cutequery ("select ID, book_name, book_price, book_quantity from book_stock ");

/***//**
* Step 4: process the result set;
* Note: (1) when reading the field content cyclically, it must be read in the order of the field record set;
* Order is the order in the SELECT statement. Otherwise, an index exception occurs;
* (2) A field can only be read once in the current row;
*/
System. Out. println ("No. Name of a book, unit price of a book ");
While (Rs. Next ())
...{
Int id = Rs. getint ("ID ");
String book_name = Rs. getstring ("book_name ");
Double book_price = Rs. getdouble ("book_price ");
Int book_quantity = Rs. getint ("book_quantity ");
System. Out. println (ID + "" + book_name + "" + book_price + "" + book_quantity );
}

/***//**
* Use resultsetmetadata to display the field name and other information
*/
Resultsetmetadata rsmeta = Rs. getmetadata ();
System. Out. Print ("number of fields :");
Int colcount = rsmeta. getcolumncount ();
System. Out. println (colcount );
System. Out. println ("name and type of the output field below ");
For (INT I = 1; I <= colcount; I ++)
...{
System. Out. println (rsmeta. getcolumnname (I) + "type:" + rsmeta. getcolumntypename (I ));
}

/***//**
* Step 5: Close the connection and result set;
* Note: First close the result set and then close the connection;
*/
Rs. Close ();
Stmt. Close ();
Con. Close ();
}
Catch (exception E)
...{
System. Out. println ("exception:" + E );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.