Java by API/java.sql/ResultSetMetaData
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
- 1 ResultSetMetaData.columnNoNulls
- 2 ResultSetMetaData.columnNullable
- 3 ResultSetMetaData.columnNullableUnknown
- 4 ResultSetMetaData: getColumnClassName(int column)
- 5 ResultSetMetaData: getColumnCount()
- 6 ResultSetMetaData: getColumnDisplaySize(int column)
- 7 ResultSetMetaData: getColumnLabel(int column)
- 8 ResultSetMetaData: getColumnName(int column)
- 9 ResultSetMetaData: getColumnType(int column)
- 10 ResultSetMetaData: getPrecision(int column)
- 11 ResultSetMetaData: getScale(int column)
- 12 ResultSetMetaData: getSchemaName(int column)
- 13 ResultSetMetaData: getTableName(int column)
- 14 ResultSetMetaData: isAutoIncrement(int column)
- 15 ResultSetMetaData: isCaseSensitive(int column)
- 16 ResultSetMetaData: isCurrency(int column)
- 17 ResultSetMetaData: isDefinitelyWritable(int column)
- 18 ResultSetMetaData: isNullable(int column)
- 19 ResultSetMetaData: isReadOnly(int column)
- 20 ResultSetMetaData: isSearchable(int column)
- 21 ResultSetMetaData: isSigned(int column)
- 22 ResultSetMetaData: isWritable(int column)
ResultSetMetaData.columnNoNulls
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();
int nullability = metadata.isNullable(1);
if (nullability == ResultSetMetaData.columnNullable) {
System.out.println("Columns ID can have a null value");
} else if (nullability == ResultSetMetaData.columnNoNulls) {
System.out.println("Columns ID does not allowed to have a null value");
} else if (nullability == ResultSetMetaData.columnNullableUnknown) {
System.out.println("Nullability unknown");
}
}
}
ResultSetMetaData.columnNullable
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();
int nullability = metadata.isNullable(1);
if (nullability == ResultSetMetaData.columnNullable) {
System.out.println("Columns ID can have a null value");
} else if (nullability == ResultSetMetaData.columnNoNulls) {
System.out.println("Columns ID does not allowed to have a null value");
} else if (nullability == ResultSetMetaData.columnNullableUnknown) {
System.out.println("Nullability unknown");
}
}
}
ResultSetMetaData.columnNullableUnknown
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();
int nullability = metadata.isNullable(1);
if (nullability == ResultSetMetaData.columnNullable) {
System.out.println("Columns ID can have a null value");
} else if (nullability == ResultSetMetaData.columnNoNulls) {
System.out.println("Columns ID does not allowed to have a null value");
} else if (nullability == ResultSetMetaData.columnNullableUnknown) {
System.out.println("Nullability unknown");
}
}
}
ResultSetMetaData: getColumnClassName(int column)
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 = getMySqlConnection();
System.out.println("Got Connection.");
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);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnDisplaySize(i));
System.out.println(rsMetaData.getColumnLabel(i));
System.out.println(rsMetaData.getColumnName(i));
System.out.println(rsMetaData.getColumnType(i));
System.out.println(rsMetaData.getColumnTypeName(i));
System.out.println(rsMetaData.getColumnClassName(i));
System.out.println(rsMetaData.getTableName(i));
System.out.println(rsMetaData.getPrecision(i));
System.out.println(rsMetaData.getScale(i));
System.out.println(rsMetaData.isAutoIncrement(i));
System.out.println(rsMetaData.isCurrency(i));
System.out.println(rsMetaData.isWritable(i));
System.out.println(rsMetaData.isDefinitelyWritable(i));
System.out.println(rsMetaData.isNullable(i));
System.out.println(rsMetaData.isReadOnly(i));
System.out.println(rsMetaData.isCaseSensitive(i));
System.out.println(rsMetaData.isSearchable(i));
System.out.println(rsMetaData.isSigned(i));
System.out.println(rsMetaData.getCatalogName(i));
System.out.println(rsMetaData.getSchemaName(i));
}
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/demo2s";
String username = "oost";
String password = "oost";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:caspian";
String username = "mp";
String password = "mp2";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
ResultSetMetaData: getColumnCount()
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("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);
rs.close();
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
ResultSetMetaData: getColumnDisplaySize(int column)
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 = getMySqlConnection();
System.out.println("Got Connection.");
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);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnDisplaySize(i));
System.out.println(rsMetaData.getColumnLabel(i));
System.out.println(rsMetaData.getColumnName(i));
System.out.println(rsMetaData.getColumnType(i));
System.out.println(rsMetaData.getColumnTypeName(i));
System.out.println(rsMetaData.getColumnClassName(i));
System.out.println(rsMetaData.getTableName(i));
System.out.println(rsMetaData.getPrecision(i));
System.out.println(rsMetaData.getScale(i));
System.out.println(rsMetaData.isAutoIncrement(i));
System.out.println(rsMetaData.isCurrency(i));
System.out.println(rsMetaData.isWritable(i));
System.out.println(rsMetaData.isDefinitelyWritable(i));
System.out.println(rsMetaData.isNullable(i));
System.out.println(rsMetaData.isReadOnly(i));
System.out.println(rsMetaData.isCaseSensitive(i));
System.out.println(rsMetaData.isSearchable(i));
System.out.println(rsMetaData.isSigned(i));
System.out.println(rsMetaData.getCatalogName(i));
System.out.println(rsMetaData.getSchemaName(i));
}
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/demo2s";
String username = "oost";
String password = "oost";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:caspian";
String username = "mp";
String password = "mp2";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
ResultSetMetaData: getColumnLabel(int column)
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", "");
}
}
ResultSetMetaData: getColumnName(int column)
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();
conn.setAutoCommit(false);
Statement st = conn.createStatement();
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");
outputResultSet(rs);
rs.close();
st.close();
conn.close();
}
private static void outputResultSet(ResultSet rs) throws Exception {
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
for (int i = 1; i < numberOfColumns + 1; i++) {
String columnName = rsMetaData.getColumnName(i);
System.out.print(columnName + " ");
}
while (rs.next()) {
for (int i = 1; i < numberOfColumns + 1; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
DriverManager.setLoginTimeout(60); // fail after 60 seconds
return DriverManager.getConnection(url, "sa", "");
}
}
ResultSetMetaData: getColumnType(int column)
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 = getMySqlConnection();
System.out.println("Got Connection.");
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);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnDisplaySize(i));
System.out.println(rsMetaData.getColumnLabel(i));
System.out.println(rsMetaData.getColumnName(i));
System.out.println(rsMetaData.getColumnType(i));
System.out.println(rsMetaData.getColumnTypeName(i));
System.out.println(rsMetaData.getColumnClassName(i));
System.out.println(rsMetaData.getTableName(i));
System.out.println(rsMetaData.getPrecision(i));
System.out.println(rsMetaData.getScale(i));
System.out.println(rsMetaData.isAutoIncrement(i));
System.out.println(rsMetaData.isCurrency(i));
System.out.println(rsMetaData.isWritable(i));
System.out.println(rsMetaData.isDefinitelyWritable(i));
System.out.println(rsMetaData.isNullable(i));
System.out.println(rsMetaData.isReadOnly(i));
System.out.println(rsMetaData.isCaseSensitive(i));
System.out.println(rsMetaData.isSearchable(i));
System.out.println(rsMetaData.isSigned(i));
System.out.println(rsMetaData.getCatalogName(i));
System.out.println(rsMetaData.getSchemaName(i));
}
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/demo2s";
String username = "oost";
String password = "oost";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:caspian";
String username = "mp";
String password = "mp2";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
ResultSetMetaData: getPrecision(int column)
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 = getMySqlConnection();
System.out.println("Got Connection.");
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);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnDisplaySize(i));
System.out.println(rsMetaData.getColumnLabel(i));
System.out.println(rsMetaData.getColumnName(i));
System.out.println(rsMetaData.getColumnType(i));
System.out.println(rsMetaData.getColumnTypeName(i));
System.out.println(rsMetaData.getColumnClassName(i));
System.out.println(rsMetaData.getTableName(i));
System.out.println(rsMetaData.getPrecision(i));
System.out.println(rsMetaData.getScale(i));
System.out.println(rsMetaData.isAutoIncrement(i));
System.out.println(rsMetaData.isCurrency(i));
System.out.println(rsMetaData.isWritable(i));
System.out.println(rsMetaData.isDefinitelyWritable(i));
System.out.println(rsMetaData.isNullable(i));
System.out.println(rsMetaData.isReadOnly(i));
System.out.println(rsMetaData.isCaseSensitive(i));
System.out.println(rsMetaData.isSearchable(i));
System.out.println(rsMetaData.isSigned(i));
System.out.println(rsMetaData.getCatalogName(i));
System.out.println(rsMetaData.getSchemaName(i));
}
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/demo2s";
String username = "oost";
String password = "oost";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:caspian";
String username = "mp";
String password = "mp2";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
ResultSetMetaData: getScale(int column)
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 = getMySqlConnection();
System.out.println("Got Connection.");
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);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnDisplaySize(i));
System.out.println(rsMetaData.getColumnLabel(i));
System.out.println(rsMetaData.getColumnName(i));
System.out.println(rsMetaData.getColumnType(i));
System.out.println(rsMetaData.getColumnTypeName(i));
System.out.println(rsMetaData.getColumnClassName(i));
System.out.println(rsMetaData.getTableName(i));
System.out.println(rsMetaData.getPrecision(i));
System.out.println(rsMetaData.getScale(i));
System.out.println(rsMetaData.isAutoIncrement(i));
System.out.println(rsMetaData.isCurrency(i));
System.out.println(rsMetaData.isWritable(i));
System.out.println(rsMetaData.isDefinitelyWritable(i));
System.out.println(rsMetaData.isNullable(i));
System.out.println(rsMetaData.isReadOnly(i));
System.out.println(rsMetaData.isCaseSensitive(i));
System.out.println(rsMetaData.isSearchable(i));
System.out.println(rsMetaData.isSigned(i));
System.out.println(rsMetaData.getCatalogName(i));
System.out.println(rsMetaData.getSchemaName(i));
}
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/demo2s";
String username = "oost";
String password = "oost";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:caspian";
String username = "mp";
String password = "mp2";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
ResultSetMetaData: getSchemaName(int column)
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 = getMySqlConnection();
System.out.println("Got Connection.");
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);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnDisplaySize(i));
System.out.println(rsMetaData.getColumnLabel(i));
System.out.println(rsMetaData.getColumnName(i));
System.out.println(rsMetaData.getColumnType(i));
System.out.println(rsMetaData.getColumnTypeName(i));
System.out.println(rsMetaData.getColumnClassName(i));
System.out.println(rsMetaData.getTableName(i));
System.out.println(rsMetaData.getPrecision(i));
System.out.println(rsMetaData.getScale(i));
System.out.println(rsMetaData.isAutoIncrement(i));
System.out.println(rsMetaData.isCurrency(i));
System.out.println(rsMetaData.isWritable(i));
System.out.println(rsMetaData.isDefinitelyWritable(i));
System.out.println(rsMetaData.isNullable(i));
System.out.println(rsMetaData.isReadOnly(i));
System.out.println(rsMetaData.isCaseSensitive(i));
System.out.println(rsMetaData.isSearchable(i));
System.out.println(rsMetaData.isSigned(i));
System.out.println(rsMetaData.getCatalogName(i));
System.out.println(rsMetaData.getSchemaName(i));
}
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/demo2s";
String username = "oost";
String password = "oost";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:caspian";
String username = "mp";
String password = "mp2";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
ResultSetMetaData: getTableName(int column)
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 = getMySqlConnection();
System.out.println("Got Connection.");
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);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnDisplaySize(i));
System.out.println(rsMetaData.getColumnLabel(i));
System.out.println(rsMetaData.getColumnName(i));
System.out.println(rsMetaData.getColumnType(i));
System.out.println(rsMetaData.getColumnTypeName(i));
System.out.println(rsMetaData.getColumnClassName(i));
System.out.println(rsMetaData.getTableName(i));
System.out.println(rsMetaData.getPrecision(i));
System.out.println(rsMetaData.getScale(i));
System.out.println(rsMetaData.isAutoIncrement(i));
System.out.println(rsMetaData.isCurrency(i));
System.out.println(rsMetaData.isWritable(i));
System.out.println(rsMetaData.isDefinitelyWritable(i));
System.out.println(rsMetaData.isNullable(i));
System.out.println(rsMetaData.isReadOnly(i));
System.out.println(rsMetaData.isCaseSensitive(i));
System.out.println(rsMetaData.isSearchable(i));
System.out.println(rsMetaData.isSigned(i));
System.out.println(rsMetaData.getCatalogName(i));
System.out.println(rsMetaData.getSchemaName(i));
}
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/demo2s";
String username = "oost";
String password = "oost";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:caspian";
String username = "mp";
String password = "mp2";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
ResultSetMetaData: isAutoIncrement(int column)
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", "");
}
}
ResultSetMetaData: isCaseSensitive(int column)
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", "");
}
}
ResultSetMetaData: isCurrency(int column)
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", "");
}
}
ResultSetMetaData: isDefinitelyWritable(int column)
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", "");
}
}
ResultSetMetaData: isNullable(int column)
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", "");
}
}
ResultSetMetaData: isReadOnly(int column)
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", "");
}
}
ResultSetMetaData: isSearchable(int column)
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", "");
}
}
ResultSetMetaData: isSigned(int column)
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", "");
}
}
ResultSetMetaData: isWritable(int column)
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", "");
}
}