JDBC introduction (Nine)

xiaoxiao2021-03-06  36

Resultset Meta Data

Mete Data is [Record Field Information Description], the resulstset is used to represent the queryed recorded data, and the information recorded by the Resulstset, that is, the record description behind the query record, that is, used to indicate the table name, field name, field type, etc. Wait, this information we can get through ResulstSetMetadata.

The following demonstrates how to get the field information of the query record.

ResulstsetmetedATADEMO.JAVA

Package onlyfun.resultSetDemo

Import java.sql. *;

Public class resultsetmetateadetadetademo {

Public static void main (String [] args) {

String driver = "com.mysql.jdbc.driver";

String url = "JDBC: mysql: // localhost: 3306 / Guestbook?"

"Useunicode = true & characterencoding = BIG5";

String user = "myname";

String Password = "123456";

Try {

Class.Forname (driver);

Connection conn = drivermanager.getConnection

URL, User, Password;

Statement Stmt = conn.createstatement ();

ResultSet Result = Stmt.executeQuery

"SELECT * from message");

ResultSetMetadata metadata =

Result.getMetadata ();

For (int i = 1; i <= metadata.getcolumncount (); i ) {

SYSTEM.out.print

Metadata.gettablename (i) ".");

SYSTEM.OUT.PRINT

Metadata.getColumnName (i) "/ t | / t");

System.out.println (

Metadata.getColumnPename (i));

}

Stmt.close ();

CONN.CLOSE ();

}

Catch (classnotfoundexception e) {

System.out.println ("Can't find driver");

E.PrintStackTrace ();

}

Catch (SQLException E) {

E.PrintStackTrace ();

}

}

}

Detailed API description

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultsetMetadata.html#gettableName (INT)

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

New Post(0)