ResultSet Meta Data
Mete Data即[记录字段信息描述],ResulstSet用来表示查询到的记录数据,而ResulstSet记录的资料,即描述查询到的记录背后的记录描述,即用来表示表名称,字段名称,字段类型等等,这些信息我们可以通过ResulstSetMetaData来取得.
下面示范如何取得查询记录的字段信息.
ResulstSetMeteDataDemo.java
package onlyfun.resultsetdemo
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 = "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.getColumnTypeName(i));
}
stmt.close();
conn.close();
}
catch(ClassNotFoundException e) {
System.out.println("找不到驱动程序");
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
详细API 描述参照 http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSetMetaData.html#getTableName(int)
Mete Data即[记录字段信息描述],ResulstSet用来表示查询到的记录数据,而ResulstSet记录的资料,即描述查询到的记录背后的记录描述,即用来表示表名称,字段名称,字段类型等等,这些信息我们可以通过ResulstSetMetaData来取得.
下面示范如何取得查询记录的字段信息.
ResulstSetMeteDataDemo.java
package onlyfun.resultsetdemo
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 = "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.getColumnTypeName(i));
}
stmt.close();
conn.close();
}
catch(ClassNotFoundException e) {
System.out.println("找不到驱动程序");
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
详细API 描述参照 http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSetMetaData.html#getTableName(int)