DOC

ResultSetMetaData

By Darlene Ramos,2014-10-09 10:14
15 views 0
ResultSetMetaData

asp|jsp|php|mysql

    Java Gossip: ResultSetMetaData

    Meta Data即“数据的数据”,Data about dataResultSet用来表示查询到的数据,而ResultSet数据的数据,即描述查询到的数据背后的数据描述,即用来表示表格名称、栏位名称、栏位类型等等,这些讯息您可以透过ResultSetMetaData来取得。

    下面这个范例直接示范如何取得查询到的数据栏位数、表格名称、栏位名称与栏位数据类型(

    ; ResultSetMetaDataDemo.java package onlyfun.caterpillar;

import java.sql.*;

    public class ResultSetMetaDataDemo {

     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 = "caterpillar";

     String password = "123456";

     Connection conn = null;

     Statement stmt = null;

     try {

     Class.forName(driver);

     conn =

    DriverManager.getConnection(

     url, user, password);

     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.getColumnTypeName(i));

     }

     }

     catch(ClassNotFoundException e) {

     System.out.println("找不到驱动程序");

     e.printStackTrace();

     }

     catch(SQLException e) {

     e.printStackTrace();

     }

     finally {

     if(stmt != null) {

     try {

     stmt.close();

     }

     catch(SQLException e) {

     e.printStackTrace();

     }

     }

     if(conn != null) {

     try {

     conn.close();

     }

     catch(SQLException e) {

     e.printStackTrace();

     }

     }

     }

     }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com