SCD Lec 17
SCD Lec 17
1
Meta Data
2
Meta Data
What if you want to know:
◦ How many columns are in the result set?
◦ What is the name of a given column?
◦ What is the data type of a specific column?
◦ What is the maximum character size of a column?
◦ Can you search on a given column?
JDBC 3
Using ResultSetMetaData
Idea
JDBC 4
Useful ResultSetMetaData Methods
getColumnCount ( )
◦ Returns the number of columns in the result set
getColumnDisplaySize (int)
◦ Returns the maximum width of the specified column in characters
getColumnType (int)
◦ Returns the SQL type for the column to compare against types in
java.sql.Types
JDBC 5
Example Code: MetaDataEx
using ResultSetMetaData
import java.sql.*;
try {
Class.forName(“Driver name”);
ResultSet rs = pStmt.executeQuery( );
JDBC 6
Example Code: MetaDataEx (cont.)
using ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();
String cName;
for (int i=1; i <= numColumns; i++)
{
cName = rsmd.getColumnName(i);
System.out.print(cName);
System.out.print("\t");
}
// changing line
System.out.println("");
JDBC 7
Example Code: MetaDataEx (cont.)
using ResultSetMetaData
String id, name, add, ph;
while (rs.next())
{
id = rs.getString(1);
name = rs.getString(2);
add = rs.getString(3);
ph = rs.getString(4);
System.out.print(id);
System.out.print("\t");
System.out.print(name);
System.out.print("\t");
System.out.print(add);
System.out.print("\t");
System.out.print(ph);
System.out.println("");
}
JDBC 8
Example Code: MetaDataEx (cont.)
using ResultSetMetaData
con.close();
JDBC 9
Example Code: MetaDataEx
Compile & Execute
JDBC 10
DatabaseMetaData
What if we want to know
◦ What SQL types are supported by DBMS to create table?
◦ What is the name of a database product?
◦ What is the version number of this database product?
◦ What is the name of the JDBC driver that is used?
◦ Is the database in a read-only mode?
JDBC 11
Using DatabaseMetaData
Idea
JDBC 12
Using DatabaseMetaData
Idea
JDBC 13
Useful DataBaseMetaData Methods
getDatabaseProductName ( )
◦ Returns the name of the database product name
getDatabaseProductVersion ( )
◦ Returns the version number of this database product
getDriverName( )
◦ Returns the name of the JDBC driver used to established the
connection
isReadOnly ( )
◦ Retrieves whether this database is in read-only mode.
◦ Returns true if so, false otherwise.
JDBC 14
Example Code: Modify MetaDataEx
using DataBaseMetaData
import java.sql.*;
try {
Class.forName(“Driver name”);
……
JDBC 15
Example Code: Modify MetaDataEx
using DataBaseMetaData
String pName = dbMetaData.getDatabaseProductName();
System.out.println(“Database: ” + pName);
……
JDBC 16
Example Code: Modify MetaDataEx
using DataBaseMetaData
// process results
con.close();
JDBC 17
Example Code: Modify MetaDataEx
Compile & Execute
JDBC 18
Task
Add more operations to the following screen. Show
MetaData Summary of Current DB Table in
JOptionPane Dialog.
JDBC 19