Java by API/java.sql/DatabaseMetaData
Содержание
- 1 DatabaseMetaData.columnNullable
- 2 DatabaseMetaData: getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
- 3 DatabaseMetaData: getDatabaseMajorVersion()
- 4 DatabaseMetaData: getDatabaseMinorVersion()
- 5 DatabaseMetaData: getDatabaseProductName()
- 6 DatabaseMetaData: getDatabaseProductVersion()
- 7 DatabaseMetaData: getDefaultTransactionIsolation()
- 8 DatabaseMetaData: getDriverName()
- 9 DatabaseMetaData: getDriverVersion()
- 10 DatabaseMetaData: getExportedKeys(String catalog, String schema, String table)
- 11 DatabaseMetaData: getImportedKeys(String catalog, String schema, String table)
- 12 DatabaseMetaData: getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate)
- 13 DatabaseMetaData: getJDBCMajorVersion()
- 14 DatabaseMetaData: getJDBCMinorVersion()
- 15 DatabaseMetaData: getMaxCharLiteralLength()
- 16 DatabaseMetaData: getMaxRowSize()
- 17 DatabaseMetaData: getMaxStatementLength()
- 18 DatabaseMetaData: getMaxTableNameLength()
- 19 DatabaseMetaData: getMaxTablesInSelect()
- 20 DatabaseMetaData: getNumericFunctions()
- 21 DatabaseMetaData: getPrimaryKeys(String catalog, String schema, String table)
- 22 DatabaseMetaData: getProcedures(String catalog, String schemaPattern, String procedureNamePattern)
- 23 DatabaseMetaData: getProcedureTerm()
- 24 DatabaseMetaData: getSchemas()
- 25 DatabaseMetaData: getSQLKeywords()
- 26 DatabaseMetaData: getStringFunctions()
- 27 DatabaseMetaData: getSystemFunctions()
- 28 DatabaseMetaData: getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern)
- 29 DatabaseMetaData: getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)
- 30 DatabaseMetaData: getTableTypes()
- 31 DatabaseMetaData: getTimeDateFunctions()
- 32 DatabaseMetaData: getTypeInfo()
- 33 DatabaseMetaData: getURL()
- 34 DatabaseMetaData: getUserName()
- 35 DatabaseMetaData: nullsAreSortedHigh()
- 36 DatabaseMetaData: supportsANSI92EntryLevelSQL()
- 37 DatabaseMetaData: supportsANSI92FullSQL()
- 38 DatabaseMetaData: supportsANSI92IntermediateSQL()
- 39 DatabaseMetaData: supportsBatchUpdates()
- 40 DatabaseMetaData: supportsCoreSQLGrammar()
- 41 DatabaseMetaData: supportsExtendedSQLGrammar()
- 42 DatabaseMetaData: supportsFullOuterJoins()
- 43 DatabaseMetaData: supportsMinimumSQLGrammar()
- 44 DatabaseMetaData: supportsMixedCaseQuotedIdentifiers()
- 45 DatabaseMetaData: supportsPositionedDelete()
- 46 DatabaseMetaData: supportsResultSetConcurrency(int type, int concurrency)
- 47 DatabaseMetaData: supportsResultSetHoldability(int holdability)
- 48 DatabaseMetaData: supportsResultSetType(int type)
- 49 DatabaseMetaData: supportsStatementPooling()
- 50 DatabaseMetaData: supportsTableCorrelationNames()
- 51 DatabaseMetaData: supportsTransactionIsolationLevel(int level)
- 52 DatabaseMetaData: supportsTransactions()
DatabaseMetaData.columnNullable
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
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")");
ResultSet rsColumns = null;
DatabaseMetaData meta = conn.getMetaData();
rsColumns = meta.getColumns(null, null, "survey", null);
while (rsColumns.next()) {
String columnName = rsColumns.getString("COLUMN_NAME");
System.out.println("column name=" + columnName);
String columnType = rsColumns.getString("TYPE_NAME");
System.out.println("type:" + columnType);
int size = rsColumns.getInt("COLUMN_SIZE");
System.out.println("size:" + size);
int nullable = rsColumns.getInt("NULLABLE");
if (nullable == DatabaseMetaData.columnNullable) {
System.out.println("nullable true");
} else {
System.out.println("nullable false");
}
int position = rsColumns.getInt("ORDINAL_POSITION");
System.out.println("position:" + position);
}
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;
}
}
DatabaseMetaData: getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
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);
DatabaseMetaData metadata = connection.getMetaData();
ResultSet resultSet = metadata.getColumns(null, null, "users", null);
while (resultSet.next()) {
String name = resultSet.getString("COLUMN_NAME");
String type = resultSet.getString("TYPE_NAME");
int size = resultSet.getInt("COLUMN_SIZE");
System.out.println("Column name: [" + name + "]; type: [" + type + "]; size: [" + size + "]");
}
connection.close();
}
}
DatabaseMetaData: getDatabaseMajorVersion()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getMySqlConnection();
DatabaseMetaData meta = conn.getMetaData();
try {
int majorVersion = meta.getDatabaseMajorVersion();
System.out.println("major Version: " + majorVersion);
int minorVersion = meta.getDatabaseMinorVersion();
System.out.println("minorVersion" + minorVersion);
} catch (Exception e) {
System.out.println("minorVersion unsupported feature");
}
String productName = meta.getDatabaseProductName();
String productVersion = meta.getDatabaseProductVersion();
System.out.println("productName" + productName);
System.out.println("productVersion" + productVersion);
conn.close();
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/databaseName";
String username = "root";
String password = "root";
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:databaseName";
String username = "name";
String password = "password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
DatabaseMetaData: getDatabaseMinorVersion()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getMySqlConnection();
DatabaseMetaData meta = conn.getMetaData();
try {
int majorVersion = meta.getDatabaseMajorVersion();
System.out.println("major Version: " + majorVersion);
int minorVersion = meta.getDatabaseMinorVersion();
System.out.println("minorVersion" + minorVersion);
} catch (Exception e) {
System.out.println("minorVersion unsupported feature");
}
String productName = meta.getDatabaseProductName();
String productVersion = meta.getDatabaseProductVersion();
System.out.println("productName" + productName);
System.out.println("productVersion" + productVersion);
conn.close();
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/databaseName";
String username = "root";
String password = "root";
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:databaseName";
String username = "name";
String password = "password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
DatabaseMetaData: getDatabaseProductName()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
try {
String url = "jdbc:odbc:yourdatabasename";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String user = "guest";
String password = "guest";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
DatabaseMetaData meta = connection.getMetaData();
System.out.println("We are using " + meta.getDatabaseProductName());
System.out.println("Version is " + meta.getDatabaseProductVersion() );
connection.close();
} catch (Exception e) {
System.err.println(e);
}
}
}
DatabaseMetaData: getDatabaseProductVersion()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
try {
String url = "jdbc:odbc:yourdatabasename";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String user = "guest";
String password = "guest";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
DatabaseMetaData meta = connection.getMetaData();
System.out.println("We are using " + meta.getDatabaseProductName());
System.out.println("Version is " + meta.getDatabaseProductVersion() );
connection.close();
} catch (Exception e) {
System.err.println(e);
}
}
}
DatabaseMetaData: getDefaultTransactionIsolation()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: getDriverName()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:databaseName";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String user = "guest";
String password = "guest";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
// Get the MetaData
DatabaseMetaData metaData = conn.getMetaData();
// Get driver information
System.out.println("Driver Informaion");
System.out.println(metaData.getDriverName());
System.out.println(metaData.getDriverVersion());
// Get schema information
System.out.println("Schemas");
ResultSet schemas = metaData.getSchemas();
while (schemas.next()) {
System.out.println(schemas.getString(1));
}
// Get table information
System.out.println("Tables");
ResultSet tables = metaData.getTables("", "", "", null);
while (tables.next()) {
System.out.println(tables.getString(3));
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
DatabaseMetaData: getDriverVersion()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:databaseName";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String user = "guest";
String password = "guest";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
// Get the MetaData
DatabaseMetaData metaData = conn.getMetaData();
// Get driver information
System.out.println("Driver Informaion");
System.out.println(metaData.getDriverName());
System.out.println(metaData.getDriverVersion());
// Get schema information
System.out.println("Schemas");
ResultSet schemas = metaData.getSchemas();
while (schemas.next()) {
System.out.println(schemas.getString(1));
}
// Get table information
System.out.println("Tables");
ResultSet tables = metaData.getTables("", "", "", null);
while (tables.next()) {
System.out.println(tables.getString(3));
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
DatabaseMetaData: getExportedKeys(String catalog, String schema, String table)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getMySqlConnection();
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")");
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getExportedKeys(conn.getCatalog(), null, "survey");
while (rs.next()) {
String fkTableName = rs.getString("FKTABLE_NAME");
String fkColumnName = rs.getString("FKCOLUMN_NAME");
int fkSequence = rs.getInt("KEY_SEQ");
System.out.println("getExportedKeys(): fkTableName="+fkTableName);
System.out.println("getExportedKeys(): fkColumnName="+fkColumnName);
System.out.println("getExportedKeys(): fkSequence="+fkSequence);
}
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;
}
}
DatabaseMetaData: getImportedKeys(String catalog, String schema, String table)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getMySqlConnection();
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")");
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getImportedKeys(conn.getCatalog(), null, "survey");
while (rs.next()) {
String fkTableName = rs.getString("FKTABLE_NAME");
String fkColumnName = rs.getString("FKCOLUMN_NAME");
int fkSequence = rs.getInt("KEY_SEQ");
System.out.println("getExportedKeys(): fkTableName=" + fkTableName);
System.out.println("getExportedKeys(): fkColumnName=" + fkColumnName);
System.out.println("getExportedKeys(): fkSequence=" + fkSequence);
}
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;
}
}
DatabaseMetaData: getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
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")");
ResultSet indexInformation = null;
DatabaseMetaData meta = conn.getMetaData();
// The "_" character represents any single character.
// The "%" character represents any sequence of zero
// or more characters.
indexInformation = meta.getIndexInfo(conn.getCatalog(), null, "survey", true, true);
while (indexInformation.next()) {
short type = indexInformation.getShort("TYPE");
switch (type) {
case DatabaseMetaData.tableIndexClustered:
System.out.println("tableIndexClustered");
case DatabaseMetaData.tableIndexHashed:
System.out.println("tableIndexHashed");
case DatabaseMetaData.tableIndexOther:
System.out.println("tableIndexOther");
case DatabaseMetaData.tableIndexStatistic:
System.out.println("tableIndexStatistic");
default:
System.out.println("tableIndexOther");
}
}
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;
}
}
DatabaseMetaData: getJDBCMajorVersion()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
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")");
DatabaseMetaData meta = conn.getMetaData();
// Oracle (and some other vendors) do not support
// some the following methods; therefore, we need
// to use try-catch block.
try {
int jdbcMajorVersion = meta.getJDBCMajorVersion();
System.out.println("jdbcMajorVersion:" + jdbcMajorVersion);
} catch (Exception e) {
System.out.println("jdbcMajorVersion unsupported feature");
}
try {
int jdbcMinorVersion = meta.getJDBCMinorVersion();
System.out.println("jdbcMinorVersion:" + jdbcMinorVersion);
} catch (Exception e) {
System.out.println("jdbcMinorVersion unsupported feature");
}
String driverName = meta.getDriverName();
String driverVersion = meta.getDriverVersion();
System.out.println("driverName=" + driverName);
System.out.println("driverVersion=" + driverVersion);
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;
}
}
DatabaseMetaData: getJDBCMinorVersion()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
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")");
DatabaseMetaData meta = conn.getMetaData();
// Oracle (and some other vendors) do not support
// some the following methods; therefore, we need
// to use try-catch block.
try {
int jdbcMajorVersion = meta.getJDBCMajorVersion();
System.out.println("jdbcMajorVersion:" + jdbcMajorVersion);
} catch (Exception e) {
System.out.println("jdbcMajorVersion unsupported feature");
}
try {
int jdbcMinorVersion = meta.getJDBCMinorVersion();
System.out.println("jdbcMinorVersion:" + jdbcMinorVersion);
} catch (Exception e) {
System.out.println("jdbcMinorVersion unsupported feature");
}
String driverName = meta.getDriverName();
String driverVersion = meta.getDriverVersion();
System.out.println("driverName=" + driverName);
System.out.println("driverVersion=" + driverVersion);
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;
}
}
DatabaseMetaData: getMaxCharLiteralLength()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: getMaxRowSize()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: getMaxStatementLength()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: getMaxTableNameLength()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
// Get max table name length
int length = dbmd.getMaxTableNameLength();
System.out.println(length);
}
}
DatabaseMetaData: getMaxTablesInSelect()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: getNumericFunctions()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost/yourDatabase";
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);
DatabaseMetaData metadata = connection.getMetaData();
String[] functions = metadata.getNumericFunctions().split(",\\s*");
for (int i = 0; i < functions.length; i++) {
String function = functions[i];
System.out.println("Function = " + function);
}
connection.close();
}
}
DatabaseMetaData: getPrimaryKeys(String catalog, String schema, String table)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getHSQLConnection();
System.out.println("Got Connection.");
Statement st = conn.createStatement();
st.executeUpdate("create table survey (id int,name varchar, PRIMARY KEY (id) );");
st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getPrimaryKeys(null, null, "survey");
java.util.List list = new java.util.ArrayList();
while (rs.next()) {
String columnName = rs.getString("COLUMN_NAME");
System.out.println("getPrimaryKeys(): columnName=" + columnName);
}
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/octopus";
String username = "root";
String password = "root";
Class.forName(driver); // load MySQL 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;
}
}
DatabaseMetaData: getProcedures(String catalog, String schemaPattern, String procedureNamePattern)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println(mtdt.getProcedureTerm());
ResultSet rs = mtdt.getProcedures(conn.getCatalog(),"%", "%");
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
for (int i = 1; i <= numCols; i++) {
if (i > 1)
System.out.print(", ");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
while (rs.next()) {
for (int i = 1; i <= numCols; i++) {
if (i > 1)
System.out.print(", ");
System.out.print(rs.getString(i));
}
System.out.println("");
}
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", "");
}
}
DatabaseMetaData: getProcedureTerm()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println(mtdt.getProcedureTerm());
ResultSet rs = mtdt.getProcedures(conn.getCatalog(),"%", "%");
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
for (int i = 1; i <= numCols; i++) {
if (i > 1)
System.out.print(", ");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
while (rs.next()) {
for (int i = 1; i <= numCols; i++) {
if (i > 1)
System.out.print(", ");
System.out.print(rs.getString(i));
}
System.out.println("");
}
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", "");
}
}
DatabaseMetaData: getSchemas()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:databaseName";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String user = "guest";
String password = "guest";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
// Get the MetaData
DatabaseMetaData metaData = conn.getMetaData();
// Get driver information
System.out.println("Driver Informaion");
System.out.println(metaData.getDriverName());
System.out.println(metaData.getDriverVersion());
// Get schema information
System.out.println("Schemas");
ResultSet schemas = metaData.getSchemas();
while (schemas.next()) {
System.out.println(schemas.getString(1));
}
// Get table information
System.out.println("Tables");
ResultSet tables = metaData.getTables("", "", "", null);
while (tables.next()) {
System.out.println(tables.getString(3));
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
DatabaseMetaData: getSQLKeywords()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.util.Arrays;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "com.jnetdirect.jsql.JSQLDriver";
Class.forName(driverName);
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbmd = connection.getMetaData();
String[] keywords = dbmd.getSQLKeywords().split(",\\s*");
Arrays.toString(keywords);
}
}
DatabaseMetaData: getStringFunctions()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost/yourDatabase";
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);
DatabaseMetaData metadata = connection.getMetaData();
String[] functions = metadata.getStringFunctions().split(",\\s*");
for (int i = 0; i < functions.length; i++) {
String function = functions[i];
System.out.println("Function = " + function);
}
connection.close();
}
}
DatabaseMetaData: getSystemFunctions()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost/yourDatabase";
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);
DatabaseMetaData metadata = connection.getMetaData();
String[] functions = metadata.getSystemFunctions().split(",\\s*");
for (int i = 0; i < functions.length; i++) {
String function = functions[i];
System.out.println("Function = " + function);
}
}
}
DatabaseMetaData: getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
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")");
ResultSet privileges = null;
DatabaseMetaData meta = conn.getMetaData();
// The "_" character represents any single character.
// The "%" character represents any sequence of zero
// or more characters.
privileges = meta.getTablePrivileges(conn.getCatalog(), "%", "survey");
while (privileges.next()) {
String catalog = privileges.getString("TABLE_CAT");
String schema = privileges.getString("TABLE_SCHEM");
String tableName = privileges.getString("TABLE_NAME");
String privilege = privileges.getString("PRIVILEGE");
String grantor = privileges.getString("GRANTOR");
String grantee = privileges.getString("GRANTEE");
String isGrantable = privileges.getString("IS_GRANTABLE");
System.out.println("table name:" + tableName);
System.out.println("catalog:"+catalog);
System.out.println("schema:"+ schema);
System.out.println("privilege:"+privilege);
System.out.println("grantor:"+grantor);
System.out.println("isGrantable:"+isGrantable);
System.out.println("grantee:"+grantee);
}
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;
}
}
DatabaseMetaData: getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)
/*
Driver Loaded.
Got Connection.
SYSTEM_ALIASES
SYSTEM_ALLTYPEINFO
SYSTEM_AUTHORIZATIONS
SYSTEM_BESTROWIDENTIFIER
SYSTEM_CACHEINFO
SYSTEM_CATALOGS
SYSTEM_CHECK_COLUMN_USAGE
SYSTEM_CHECK_CONSTRAINTS
SYSTEM_CHECK_ROUTINE_USAGE
SYSTEM_CHECK_TABLE_USAGE
SYSTEM_CLASSPRIVILEGES
SYSTEM_COLLATIONS
SYSTEM_COLUMNPRIVILEGES
SYSTEM_COLUMNS
SYSTEM_CROSSREFERENCE
SYSTEM_INDEXINFO
SYSTEM_PRIMARYKEYS
SYSTEM_PROCEDURECOLUMNS
SYSTEM_PROCEDURES
SYSTEM_PROPERTIES
SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS
SYSTEM_SCHEMAS
SYSTEM_SCHEMATA
SYSTEM_SEQUENCES
SYSTEM_SESSIONINFO
SYSTEM_SESSIONS
SYSTEM_SUPERTABLES
SYSTEM_SUPERTYPES
SYSTEM_TABLEPRIVILEGES
SYSTEM_TABLES
SYSTEM_TABLETYPES
SYSTEM_TABLE_CONSTRAINTS
SYSTEM_TEXTTABLES
SYSTEM_TRIGGERCOLUMNS
SYSTEM_TRIGGERS
SYSTEM_TYPEINFO
SYSTEM_UDTATTRIBUTES
SYSTEM_UDTS
SYSTEM_USAGE_PRIVILEGES
SYSTEM_USERS
SYSTEM_VERSIONCOLUMNS
SYSTEM_VIEWS
SYSTEM_VIEW_COLUMN_USAGE
SYSTEM_VIEW_ROUTINE_USAGE
SYSTEM_VIEW_TABLE_USAGE
* */
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
System.out.println(rs.getString(3));
} }
static Connection conn;
static Statement st;
static {
try {
// Step 1: Load the JDBC driver.
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
// Step 2: Establish the connection to the database.
String url = "jdbc:hsqldb:data/tutorial";
conn = DriverManager.getConnection(url, "sa", "");
System.out.println("Got Connection.");
st = conn.createStatement();
} catch (Exception e) {
System.err.println("Got an exception! ");
e.printStackTrace();
System.exit(0);
}
}
}
DatabaseMetaData: getTableTypes()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: getTimeDateFunctions()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost/yourDatabase";
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);
DatabaseMetaData metadata = connection.getMetaData();
String[] functions = metadata.getTimeDateFunctions().split(",\\s*");
for (int i = 0; i < functions.length; i++) {
String function = functions[i];
System.out.println("Function = " + function);
}
connection.close();
}
}
DatabaseMetaData: getTypeInfo()
/*
Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
Use of this software is authorized pursuant to the terms of the license found at
http://developer.java.sun.ru/berkeley_license.html.
Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
- Redistribution of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistribution in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
Neither the name of Sun Microsystems, Inc. or the names of contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
This software is provided "AS IS," without a warranty of any kind.
ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICORSYSTEMS, INC. ("SUN")
AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN
IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
You acknowledge that this software is not designed, licensed or intended for
use in the design, construction, operation or maintenance of any nuclear
facility.
*/
/*
* Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
* Use of this software is authorized pursuant to the terms of the license found at
* http://developer.java.sun.ru/berkeley_license.html.
*/
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main {
public static void main(String args[]) {
String url = "jdbc:mySubprotocol:myDataSource";
Connection con;
DatabaseMetaData dbmd;
try {
Class.forName("myDriver.ClassName");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"myLogin", "myPassword");
dbmd = con.getMetaData();
ResultSet rs = dbmd.getTypeInfo();
while (rs.next()) {
String typeName = rs.getString("TYPE_NAME");
short dataType = rs.getShort("DATA_TYPE");
String createParams = rs.getString("CREATE_PARAMS");
int nullable = rs.getInt("NULLABLE");
boolean caseSensitive = rs.getBoolean("CASE_SENSITIVE");
System.out.println("DBMS type " + typeName + ":");
System.out.println(" java.sql.Types: " + dataType);
System.out.print(" parameters used to create: ");
System.out.println(createParams);
System.out.println(" nullable?: " + nullable);
System.out.print(" case sensitive?: ");
System.out.println(caseSensitive);
System.out.println("");
}
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
DatabaseMetaData: getURL()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println("URL in use: " + mtdt.getURL());
System.out.println("User name: " + mtdt.getUserName());
System.out.println("DBMS name: " + mtdt.getDatabaseProductName());
System.out.println("DBMS version: " + mtdt.getDatabaseProductVersion());
System.out.println("Driver name: " + mtdt.getDriverName());
System.out.println("Driver version: " + mtdt.getDriverVersion());
System.out.println("supp. SQL Keywords: " + mtdt.getSQLKeywords());
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", "");
}
}
DatabaseMetaData: getUserName()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println("URL in use: " + mtdt.getURL());
System.out.println("User name: " + mtdt.getUserName());
System.out.println("DBMS name: " + mtdt.getDatabaseProductName());
System.out.println("DBMS version: " + mtdt.getDatabaseProductVersion());
System.out.println("Driver name: " + mtdt.getDriverName());
System.out.println("Driver version: " + mtdt.getDriverVersion());
System.out.println("supp. SQL Keywords: " + mtdt.getSQLKeywords());
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", "");
}
}
DatabaseMetaData: nullsAreSortedHigh()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: supportsANSI92EntryLevelSQL()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: supportsANSI92FullSQL()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println("ANSI92 Entry Level: " + mtdt.supportsANSI92EntryLevelSQL());
System.out.println("ANSI92 Intermediate: " + mtdt.supportsANSI92IntermediateSQL());
System.out.println("ANSI92 Full SQL: " + mtdt.supportsANSI92FullSQL());
System.out.println("Minimum SQL Grammar: " + mtdt.supportsMinimumSQLGrammar());
System.out.println("Core SQL Grammar: " + mtdt.supportsCoreSQLGrammar());
System.out.println("Extended SQL Grammar: " + mtdt.supportsExtendedSQLGrammar());
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", "");
}
}
DatabaseMetaData: supportsANSI92IntermediateSQL()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println("ANSI92 Entry Level: " + mtdt.supportsANSI92EntryLevelSQL());
System.out.println("ANSI92 Intermediate: " + mtdt.supportsANSI92IntermediateSQL());
System.out.println("ANSI92 Full SQL: " + mtdt.supportsANSI92FullSQL());
System.out.println("Minimum SQL Grammar: " + mtdt.supportsMinimumSQLGrammar());
System.out.println("Core SQL Grammar: " + mtdt.supportsCoreSQLGrammar());
System.out.println("Extended SQL Grammar: " + mtdt.supportsExtendedSQLGrammar());
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", "");
}
}
DatabaseMetaData: supportsBatchUpdates()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: supportsCoreSQLGrammar()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: supportsExtendedSQLGrammar()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println("ANSI92 Entry Level: " + mtdt.supportsANSI92EntryLevelSQL());
System.out.println("ANSI92 Intermediate: " + mtdt.supportsANSI92IntermediateSQL());
System.out.println("ANSI92 Full SQL: " + mtdt.supportsANSI92FullSQL());
System.out.println("Minimum SQL Grammar: " + mtdt.supportsMinimumSQLGrammar());
System.out.println("Core SQL Grammar: " + mtdt.supportsCoreSQLGrammar());
System.out.println("Extended SQL Grammar: " + mtdt.supportsExtendedSQLGrammar());
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", "");
}
}
DatabaseMetaData: supportsFullOuterJoins()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: supportsMinimumSQLGrammar()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println("ANSI92 Entry Level: " + mtdt.supportsANSI92EntryLevelSQL());
System.out.println("ANSI92 Intermediate: " + mtdt.supportsANSI92IntermediateSQL());
System.out.println("ANSI92 Full SQL: " + mtdt.supportsANSI92FullSQL());
System.out.println("Minimum SQL Grammar: " + mtdt.supportsMinimumSQLGrammar());
System.out.println("Core SQL Grammar: " + mtdt.supportsCoreSQLGrammar());
System.out.println("Extended SQL Grammar: " + mtdt.supportsExtendedSQLGrammar());
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", "");
}
}
DatabaseMetaData: supportsMixedCaseQuotedIdentifiers()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: supportsPositionedDelete()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: supportsResultSetConcurrency(int type, int concurrency)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost/yourDatabase";
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);
DatabaseMetaData metadata = connection.getMetaData();
boolean updatable = metadata.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
System.out.println("Updatable ResultSet supported = " + updatable);
connection.close();
}
}
DatabaseMetaData: supportsResultSetHoldability(int holdability)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData dbMeta = conn.getMetaData();
if (dbMeta.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
System.out.println("this database hold cursors over commit");
} else if (dbMeta.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT)) {
System.out.println("this database close cursors at commit");
}
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", "");
}
}
DatabaseMetaData: supportsResultSetType(int type)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
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")");
DatabaseMetaData meta = conn.getMetaData();
if (meta.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)) {
System.out.println("type name=TYPE_FORWARD_ONLY");
}
if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
System.out.println("type name=TYPE_SCROLL_INSENSITIVE");
}
if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
System.out.println("type name=TYPE_SCROLL_SENSITIVE");
}
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;
}
}
DatabaseMetaData: supportsStatementPooling()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
String username = "name";
String password = "password";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
try {
DatabaseMetaData dbmd = conn.getMetaData();
if (dbmd == null) {
System.out.println("No");
}
if (dbmd.supportsStatementPooling()) {
System.out.println("statement pooling is supported");
} else {
System.out.println("No");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
}
}
DatabaseMetaData: supportsTableCorrelationNames()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] argv) {
Connection connection = null;
Statement statement;
ResultSet rs;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
connection = ds.getConnection();
DatabaseMetaData md = connection.getMetaData();
statement = connection.createStatement();
System.out.println("getURL() - " + md.getURL());
System.out.println("getUserName() - " + md.getUserName());
System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());
System.out.println("<H1>Feature Support</H1>");
System.out.println("supportsAlterTableWithDropColumn - "
+ md.supportsAlterTableWithDropColumn() + "<BR>");
System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
System.out.println("supportsMixedCaseQuotedIdentifiers - "
+ md.supportsMixedCaseQuotedIdentifiers());
System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getMaxRowSize - " + md.getMaxRowSize());
System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxConnections - " + md.getMaxConnections());
System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());
System.out.println("getTableTypes()");
rs = md.getTableTypes();
while (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("getTables()");
rs = md.getTables("accounts", "", "%", new String[0]);
while (rs.next()) {
System.out.println(rs.getString("TABLE_NAME"));
}
System.out.println("Transaction Support");
System.out.println("getDefaultTransactionIsolation() - "
+ md.getDefaultTransactionIsolation());
System.out.println("dataDefinitionIgnoredInTransactions() - "
+ md.dataDefinitionIgnoredInTransactions());
System.out.println("General Source Information");
System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
System.out.println("getTypeInfo()");
rs = md.getTypeInfo();
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DatabaseMetaData: supportsTransactionIsolationLevel(int level)
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData dbMd = conn.getMetaData();
if (dbMd.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED)) {
System.out.println("Transaction Isolation level "
+ "TRANSACTION_READ_COMMITTED is supported.");
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
}
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", "");
}
}
DatabaseMetaData: supportsTransactions()
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestSupportsTransactions {
public static boolean supportsTransactions(Connection conn) throws SQLException {
if (conn == null) {
return false;
}
DatabaseMetaData dbMetaData = conn.getMetaData();
if (dbMetaData == null) {
// metadata is not supported
return false;
}
return dbMetaData.supportsTransactions();
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:scorpian";
String username = "userName";
String password = "pass";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void main(String[] args)throws Exception {
Connection conn = getOracleConnection();
try {
System.out.println("conn=" + conn);
System.out.println("Transaction Support:" + supportsTransactions(conn));
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}