9.JDBC -Metadata
We have 3 types of metadata in jdbc
-
DataBaseMetaData
-
ResultSetMetaData
-
ParameterMetaData
1. DataBaseMetaData
We can get database meta like database details, driver name, name of total number of tables, no.of views etc. by using DataBaseMetaData class, we can get by using Connection Object
DatabaseMetaData getMetaData()
DatabaseMetaData dm = con.getMeteData()
-
String getDriverName()
-
String getDriverVersion()
-
String getURL()
-
String getUserName()
-
String getDatabaseProductName()
-
String getDatabaseProductVersion()
-
int getDatabaseMajorVersion()
-
int getDatabaseMinorVersion()
public class JDBC {
public static void main(String[] args) throws Exception {
String url = "jdbc:mysql://localhost:3306/mydb";
String u = "root";
String p = "123456";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, u, p);
DatabaseMetaData dm = con.getMetaData();
System.out.println("Driver : "+dm.getDriverName());
System.out.println("DriverVersion: "+dm.getDriverVersion());
System.out.println("URL : "+dm.getURL());
System.out.println("UserName : "+dm.getUserName());
System.out.println("DatabseName : "+dm.getDatabaseProductName());
System.out.println("DVersion : "+dm.getDatabaseProductVersion());
System.out.println("Major : "+dm.getDatabaseMinorVersion());
System.out.println("Minor : "+dm.getDatabaseMajorVersion());
}
}
-----------------------------
Driver : MySQL-AB JDBC Driver
DriverVersion: mysql-connector-java-5.1.18 ( Revision: tonci.grgin@oracle.com-20110930151701-jfj14ddfq48ifkfq )
URL : jdbc:mysql://localhost:3306/mydb
UserName : root@localhost
DatabseName : MySQL
DVersion : 5.6.26
Major : 6
Minor : 5
2. ResultSetMetaData
-
The metadata means data about data
-
We can get ReselutSet meta information like no.of columns, column data,table name etc
-
String getTableName(int column)
-
int getColumnCount()
-
String getColumnName(int column)
-
int getColumnType(int column)
-
public class JDBC {
public static void main(String[] args) throws Exception {
String url = "jdbc:mysql://localhost:3306/mydb";
String u = "root";
String p = "123456";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, u, p);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from customer");
ResultSetMetaData rm = rs.getMetaData();
System.out.println(rm.getTableName(1)); // customer
System.out.println(rm.getColumnCount());// 3
System.out.println(rm.getColumnName(2));// name
System.out.println(rm.getColumnType(2));// 12
}
}
3. ParameterMetaData
Used to get information about the types and properties for each parameter marker in a PreparedStatement object
-
int getParameterCount()
-
int getParameterType(int param)
-
String getParameterTypeName(int param)