Java Tutorial/Database/ResultSetMetaData — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:06, 1 июня 2010
Содержание
- 1 Get column count from ResultSetMetaData
- 2 Get the Column Names in a Result Set
- 3 Getting Column Characteristics
- 4 If a table column value is auto-increment?
- 5 ResultSetMetaData Methods for Information about a Result Set"s Columns
- 6 ResultSetMetaData Methods for Property Information about a Result Set"s Columns
Get column count from ResultSetMetaData
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getHSQLConnection();
Statement st = conn.createStatement();
// st.executeUpdate("drop table survey;");
st.executeUpdate("create table survey (id int,name varchar(30));");
st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM survey");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
System.out.println("resultSet MetaData column Count=" + numberOfColumns);
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
Get the Column Names in a Result Set
Given a ResultSet object, it is possible to get the column name, column type, table name (from which table this column came from), and position.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
st.executeUpdate("create table survey (id int,name varchar(30));");
st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
st.executeUpdate("insert into survey (id,name ) values (2,null)");
st.executeUpdate("insert into survey (id,name ) values (3,"Tom")");
st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM survey");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
// get the column names; column indexes start from 1
for (int i = 1; i < numberOfColumns + 1; i++) {
String columnName = rsMetaData.getColumnName(i);
String tableName = rsMetaData.getTableName(i);
System.out.println(columnName);
System.out.println(tableName);
}
rs.close();
st.close();
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
ID SURVEY NAME SURVEY
Getting Column Characteristics
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
Statement st = conn.createStatement();
st.executeUpdate("create table survey (id int,myDate DATE);");
String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";
PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
pstmt.setString(1, "1");
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
pstmt.setDate(2, sqlDate);
pstmt.executeUpdate();
ResultSet rs = st.executeQuery("SELECT * FROM survey");
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
System.out.print("\t");
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.getColumnLabel(i));
}
System.out.print("\nAuto Increment\t");
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.isAutoIncrement(i));
}
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.isCaseSensitive(i));
}
System.out.print("\nSearchable\t");
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.isSearchable(i));
}
System.out.print("\nCurrency\t");
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.isCurrency(i));
}
System.out.print("\nAllows nulls\t");
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.isNullable(i));
}
System.out.print("\nSigned\t");
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.isSigned(i));
}
System.out.print("\nRead only\t");
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.isReadOnly(i));
}
System.out.print("\nWritable\t");
for (int i = 1; i <= numCols; i++) {
System.out.print(rsmd.isWritable(i));
}
System.out.print("\nDefinitely Writable\t");
for (int i = 1; i <= numCols; i++) {
System.out.println(rsmd.isDefinitelyWritable(i));
}
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
If a table column value is auto-increment?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Main {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost/testdb";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) throws Exception {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users");
ResultSetMetaData metadata = resultSet.getMetaData();
if (metadata.isAutoIncrement(1)) {
System.out.println("Column ID is an auto-increment column");
}
}
}
ResultSetMetaData Methods for Information about a Result Set"s Columns
- int getColumnCount();
- int getColumnDisplaySize(int column);
- String getColumnLabel(int column);
- String getColumnName(int column);
- String getSchemaName(int column);
- int getPrecision(int column);
- int getScale(int column);
- String getTableName(int column);
- String getCatalogName(int column);
- int getColumnType(int column);
- String getColumnTypeName(int column);
ResultSetMetaData Methods for Property Information about a Result Set"s Columns
- boolean isAutoIncrement(int column);
- boolean isCaseSensitive(int column);
- boolean isSearchable(int column);
- boolean isCurrency(int column);
- int isNullable(int column);
- boolean isSigned(int column);
- boolean isReadOnly(int column);
- boolean isWritable(int column);
- boolean isDefinitelyWritable(int column);