/** */ /****************************************
* The problem illustrated by this example: JDBC development process--using ResultSetMetaData to display the names of fields and other information
* Note: The driver used in this example is JDBC-ODBC Bridge drive
****************************************/
Class Hello
... {
public static void Main (String args[])
... {
Try
... {
/** *//**
* First step: load JDBC driver;
*/
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
/** *//**
* Second Step: establish the connection;
* Note: This step requires the creation of a data source name for the SQL Server database book BOOKDSN
*/
String url= "Jdbc:odbc:bookdsn";
Connection con=drivermanager.getconnection (URL, "sa", "");
/** *//**
* Step Three: Create the statement, execute the query and get the result set;
*/
Statement stmt=con.createstatement ();
ResultSet rs=stmt.executequery ("Select id,book_name,book_price,book_quantity from Book_stock");
/** *//**
* Step Fourth: Process the result set;
* Note: (1) in the circular reading of the field contents, must be read in the order of the field Recordset;
* Order is the order in the SELECT statement, otherwise an index exception is generated;
* (2) A field can only be read once on the current line;
*/
System.out.println ("Number book name book quantity");
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 name of the field and other information
*/
ResultSetMetaData Rsmeta=rs.getmetadata ();
System.out.print ("Number of fields:");
int Colcount=rsmeta.getcolumncount ();
System.out.println (ColCount);
System.out.println ("Output field name and type below");
for (int i=1;i<=colcount;i++)
... {
System.out.println (Rsmeta.getcolumnname (i) + "type:" +rsmeta.getcolumntypename (i));
}
/** *//**
* Step Fifth: Close the connection, result set;
* Note: First off the result set and then off the connection;
*/
Rs.close ();
Stmt.close ();
Con.close ();
}
catch (Exception e)
... {
SYSTEM.OUT.PRINTLN ("Abnormal occurrence:" +e);
}
}
}
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.