Java examples on Java JDBC
- How do I create a connection to MS Access database?
- How do I use DatabaseMetaData to get table column names?
- How do I create a connection to database?
- How do I retrieve values from ResultSet?
- How do I read BLOBs data from database?
- How do I execute stored procedure?
- How do I store BLOBs data into database?
- How do I delete record from table?
- How do I query records from table?
- How do I insert a record into database table?
- How do I read CLOBs data from database?
- How do I know number of rows affected when updating data in database table?
- How do I get date time functions supported by database?
- How do I convert java.util.Date to java.sql.Date?
- How do I make updates in Updatable ResultSet?
- How do I register out parameter in CallableStatement?
- How do I commit or rollback transaction in JDBC?
- How do I get all table names from database?
- How do I create a batch update in JDBC?
- How do I drop table from database?
- How do I create a table in database?
- How do I get string functions supported by database?
- How do I create a scrollable result sets?
- How do I retrieve auto-generated keys?
- How do I move cursor to the last record?
- How do I move cursor in scrollable result sets?
- How do I set the query timeout limit?
- How do I get list of stored procedure names?
- How do I check if cursor is in the last row?
- How do I get column names of a table using ResultSetMetaData?
- How do I disable auto commit mode in JDBC?
- How do I get JDBC driver information?
- How do I get column's precision and scale value?
- How do I know the current position of cursor?
- How do I know if a table column can have a null value or not?
- How do I move to absolute or relative row?
- How do I know if a table column value is auto-increment?
- How do I store CLOBs data into database?
- How do I check if cursor is in the first row?
- How do I get the max concurrent connection to a database?
- How do I know the designated column's table name?
- How do I get numeric functions supported by database?
- How do I get database maximum table name length?
- How do I get data types supported by database?
- How to automatically close resources in JDBC?
- How do I get system functions supported by database?
- How do I get database product information?
- How do I know if database support batch update?
- How do I know if database support updatable result sets?
- How do I know if database support transaction?
- How do I limit MySQL query result?
- How do I know if database support scrollable result sets?
- How do I set the maximum rows to read in a query?
- How do I set the fetch size of a statement?
- How do I call a stored procedure that return a result set?
- How do I get the maximum number of concurrent connections?
- How do I check if the OUT parameter value is null?
- How do I retrieve available schemas in database?
- How do I get JDBC driver property information?
- How do I check whether a driver is JDBC compliant?
How do I retrieve available schemas in database?
The examples below show you how to get the available schema names in Oracle database. The available schema names can be obtained from the DatabaseMetaData object by calling the getSchemas() method.
This method returns a ResultSet object. After obtaining the ResultSet object we need to iterate it and get the schema name by calling the result set getString() method and pass TABLE_SCHEM as the column name.
package org.kodejava.example.sql;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MetadataGetSchema {
public static void main(String[] args) {
Connection connection = null;
try {
connection = getConnection();
//
// Gets DatabaseMetaData
//
DatabaseMetaData metadata = connection.getMetaData();
//
// Retrieves the schema names available in this database
//
ResultSet rs = metadata.getSchemas();
while (rs.next()) {
String schema = rs.getString("TABLE_SCHEM");
System.out.println("Schema: " + schema);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
closeConnection(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* Get a connection to database.
* @return a connection to database.
* @throws Exception when an exception occurs.
*/
private static Connection getConnection() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
return DriverManager.getConnection(url, "kodejava", "welcome");
}
/**
* Close a connection to database.
* @param connection a connection to be closed.
* @throws SQLException when an exception occurs.
*/
private static void closeConnection(Connection connection)
throws SQLException {
if (connection != null && !connection.isClosed()) {
connection.close();
}
}
}
Here an a list of schema names available in the Oracle XE database:
Schema: ANONYMOUS Schema: CTXSYS Schema: DBSNMP Schema: DIP Schema: FLOWS_020100 Schema: FLOWS_FILES Schema: HR Schema: KODEJAVA Schema: MDSYS Schema: OUTLN Schema: SYS Schema: SYSTEM Schema: TSMSYS Schema: XDB