Comprehensive information about the database as a whole.
How to get DatabaseMetaData object
Connection provide the following method which return DatabaseMetaData object -
DatabaseMetaData getMetaData() - Retrieves a DatabaseMetaData object that contains metadata about the database to which this connection object represents a connection.
Methods of DatabaseMetaData
1. ResultSet getTables(String catalog,String schema pattern, String table-name pattern, String[] types) - Retrieves a description of the table available in the given catalog.
2. String getURL() - Retrieves the URL for this DBMS.
3. String getUserName() - Retrieves the user-name as known to the database.
4. String getDriverName() - Retrieves the name of this JDBC driver.
5. String getDriverVersion() - Retrieves the version number of this JDBC driver as a string.
6. String getDatabaseProductName() - Retrieves the name of this database product.
7. String getDatabaseProductVersion() - Retrieves the version number of this database product.
PROGRAM TO FIND DATABASE PRODUCT NAME, VERSION, DRIVER NAME AND DRIVER VERSION
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Test31 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
DatabaseMetaData dmd = con.getMetaData();
System.out.println(dmd.getUserName());
System.out.println(dmd.getDatabaseProductName());
System.out.println(dmd.getURL());
System.out.println(dmd.getDatabaseProductVersion());
System.out.println(dmd.getDriverName());
System.out.println(dmd.getDriverVersion());
}
}
Output -
Oracle
jdbc:oracle:thin:@localhost:1521:orcl
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Oracle JDBC driver
11.2.0.1.0
PROGRAM TO DISPLAY TABLES
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class Test32 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
DatabaseMetaData dmd = con.getMetaData();
String t[] ={"TABLE","VIEW"};
ResultSet rs = dmd.getTables(null, null, "E%", t);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
for(int i=1;i<=count;i++)
{
System.out.println(rsmd.getColumnType(i));
while(rs.next())
System.out.println(rs.getString(3)+" : "+rs.getString(4));
}
}
}
Output -
EMP : TABLE
EXPCOMPRESSEDPART : VIEW
EXPCOMPRESSEDSUB : VIEW
EXPCOMPRESSEDTAB : VIEW
EXPEXEMPT : VIEW
EXPGETENCCOLNAM : VIEW
EXPGETMAPOBJ : VIEW
EXPMAPIOT : VIEW
EXPPIOTMAP : VIEW
EXPTABSUBPART : VIEW
EXPTABSUBPARTDATA_VIEW : VIEW
EXPTABSUBPARTLOBFRAG : VIEW
EXPTABSUBPARTLOB_VIEW : VIEW
EXPXSLDELIM : VIEW
EXU102XTYPU : VIEW
EXU10ADEFPSWITCHES : VIEW
EXU10AOBJSWITCH : VIEW
EXU10ASCU : VIEW
EXU10CCLO : VIEW
EXU10CCLU : VIEW
EXU10COEU : VIEW
EXU10DEFPSWITCHES : VIEW
EXU10DOSO : VIEW
EXU10IND_BASE : VIEW
EXU10LNKU : VIEW
EXU10MVL : VIEW
EXU10MVLU : VIEW
EXU10OBJSWITCH : VIEW
EXU10R2DEFPSWITCHES : VIEW
EXU10R2OBJSWITCH : VIEW
EXU10SNAPLU : VIEW
EXU10SNAPU : VIEW
EXU10TABSU : VIEW
EXU10TABU : VIEW
EXU10USCIU : VIEW
EXU11IND : VIEW
EXU11IND_BASE : VIEW
EXU11SNAPU : VIEW
EXU11XML : VIEW
EXU816MAXSQV : VIEW
EXU816TGRU : VIEW
EXU81ACTIONOBJ : VIEW
EXU81ACTIONPKG : VIEW
EXU81ASSOC : VIEW
EXU81CSC : VIEW
EXU81DOIU : VIEW
EXU81IND : VIEW
EXU81IND_BASE : VIEW
EXU81ITYU : VIEW
EXU81IXCPU : VIEW
EXU81IXSPU : VIEW
EXU81JAV : VIEW
EXU81JAVT : VIEW
EXU81LBCPU : VIEW
EXU81LBPU : VIEW
EXU81LBSPU : VIEW
EXU81NOS : VIEW
EXU81OBJECTPKG : VIEW
EXU81OPRU : VIEW
EXU81PLBU : VIEW
EXU81PROCOBJ : VIEW
EXU81PROCOBJINSTANCE : VIEW
EXU81RGCU : VIEW
EXU81RGSU : VIEW
EXU81RLS : VIEW
EXU81SCMU : VIEW
EXU81SLFCU : VIEW
EXU81SNAPLU : VIEW
EXU81SNAPU : VIEW
EXU81SPOKIU : VIEW
EXU81SPOKU : VIEW
EXU81SRTU : VIEW
Methods of DatabaseMetaData
1. ResultSet getTables(String catalog,String schema pattern, String table-name pattern, String[] types) - Retrieves a description of the table available in the given catalog.
2. String getURL() - Retrieves the URL for this DBMS.
3. String getUserName() - Retrieves the user-name as known to the database.
4. String getDriverName() - Retrieves the name of this JDBC driver.
5. String getDriverVersion() - Retrieves the version number of this JDBC driver as a string.
6. String getDatabaseProductName() - Retrieves the name of this database product.
7. String getDatabaseProductVersion() - Retrieves the version number of this database product.
PROGRAM TO FIND DATABASE PRODUCT NAME, VERSION, DRIVER NAME AND DRIVER VERSION
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Test31 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
DatabaseMetaData dmd = con.getMetaData();
System.out.println(dmd.getUserName());
System.out.println(dmd.getDatabaseProductName());
System.out.println(dmd.getURL());
System.out.println(dmd.getDatabaseProductVersion());
System.out.println(dmd.getDriverName());
System.out.println(dmd.getDriverVersion());
}
}
Output -
Oracle
jdbc:oracle:thin:@localhost:1521:orcl
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Oracle JDBC driver
11.2.0.1.0
PROGRAM TO DISPLAY TABLES
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class Test32 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
DatabaseMetaData dmd = con.getMetaData();
String t[] ={"TABLE","VIEW"};
ResultSet rs = dmd.getTables(null, null, "E%", t);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
for(int i=1;i<=count;i++)
{
System.out.println(rsmd.getColumnType(i));
while(rs.next())
System.out.println(rs.getString(3)+" : "+rs.getString(4));
}
}
}
Output -
EMP : TABLE
EXPCOMPRESSEDPART : VIEW
EXPCOMPRESSEDSUB : VIEW
EXPCOMPRESSEDTAB : VIEW
EXPEXEMPT : VIEW
EXPGETENCCOLNAM : VIEW
EXPGETMAPOBJ : VIEW
EXPMAPIOT : VIEW
EXPPIOTMAP : VIEW
EXPTABSUBPART : VIEW
EXPTABSUBPARTDATA_VIEW : VIEW
EXPTABSUBPARTLOBFRAG : VIEW
EXPTABSUBPARTLOB_VIEW : VIEW
EXPXSLDELIM : VIEW
EXU102XTYPU : VIEW
EXU10ADEFPSWITCHES : VIEW
EXU10AOBJSWITCH : VIEW
EXU10ASCU : VIEW
EXU10CCLO : VIEW
EXU10CCLU : VIEW
EXU10COEU : VIEW
EXU10DEFPSWITCHES : VIEW
EXU10DOSO : VIEW
EXU10IND_BASE : VIEW
EXU10LNKU : VIEW
EXU10MVL : VIEW
EXU10MVLU : VIEW
EXU10OBJSWITCH : VIEW
EXU10R2DEFPSWITCHES : VIEW
EXU10R2OBJSWITCH : VIEW
EXU10SNAPLU : VIEW
EXU10SNAPU : VIEW
EXU10TABSU : VIEW
EXU10TABU : VIEW
EXU10USCIU : VIEW
EXU11IND : VIEW
EXU11IND_BASE : VIEW
EXU11SNAPU : VIEW
EXU11XML : VIEW
EXU816MAXSQV : VIEW
EXU816TGRU : VIEW
EXU81ACTIONOBJ : VIEW
EXU81ACTIONPKG : VIEW
EXU81ASSOC : VIEW
EXU81CSC : VIEW
EXU81DOIU : VIEW
EXU81IND : VIEW
EXU81IND_BASE : VIEW
EXU81ITYU : VIEW
EXU81IXCPU : VIEW
EXU81IXSPU : VIEW
EXU81JAV : VIEW
EXU81JAVT : VIEW
EXU81LBCPU : VIEW
EXU81LBPU : VIEW
EXU81LBSPU : VIEW
EXU81NOS : VIEW
EXU81OBJECTPKG : VIEW
EXU81OPRU : VIEW
EXU81PLBU : VIEW
EXU81PROCOBJ : VIEW
EXU81PROCOBJINSTANCE : VIEW
EXU81RGCU : VIEW
EXU81RGSU : VIEW
EXU81RLS : VIEW
EXU81SCMU : VIEW
EXU81SLFCU : VIEW
EXU81SNAPLU : VIEW
EXU81SNAPU : VIEW
EXU81SPOKIU : VIEW
EXU81SPOKU : VIEW
EXU81SRTU : VIEW
No comments:
Post a Comment