0% found this document useful (0 votes)
22 views

SCD Lec 17

The document discusses how to retrieve metadata about the structure and contents of database tables and databases using JDBC in Java. It explains that the ResultSetMetaData object can be used to obtain information about the columns in a ResultSet, such as the number of columns, their names, data types, and sizes. It also introduces the DatabaseMetaData object, which provides metadata about the database as a whole, including the database name and version, driver name, and whether it is read-only. An example code demonstrates how to retrieve and display this metadata information from a ResultSet and the DatabaseMetaData object.

Uploaded by

Hanan Aslam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

SCD Lec 17

The document discusses how to retrieve metadata about the structure and contents of database tables and databases using JDBC in Java. It explains that the ResultSetMetaData object can be used to obtain information about the columns in a ResultSet, such as the number of columns, their names, data types, and sizes. It also introduces the DatabaseMetaData object, which provides metadata about the database as a whole, including the database name and version, driver name, and whether it is read-only. An example code demonstrates how to retrieve and display this metadata information from a ResultSet and the DatabaseMetaData object.

Uploaded by

Hanan Aslam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

More on JDBC

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

◦ From a ResultSet (the return type of


executeQuery), derive a ResultSetMetaData
object

◦ Use that object to look up the number, names, and types


of columns

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

getColumnName(int) / getColumnLabel (int)

◦ The getColumnName method returns the database name of the column


◦ The getColumnLabel method returns the suggested column label for
printouts

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.*;

public class MetaDataEx {

public static void main ( String args[ ]) {

try {

Class.forName(“Driver name”);

Connection con = DriverManager.getConnection(url, usr, pwd);

String sql = “SELECT * FROM Person”;


PreparedStatement pStmt = con.prepareStatement(sql);

ResultSet rs = pStmt.executeQuery( );

JDBC 6
Example Code: MetaDataEx (cont.)
using ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();

int numColumns = rsmd.getColumnCount();


System.out.println(“Number of Columns:” + numColumns);

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();

} catch (Exception ex) {


System.out.println(ex);
}

}// end main


}//end class

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

◦ From a Connection, derive a DatabaseMetaData


object

◦ Contains the comprehensive information about the


database as a whole

JDBC 12
Using DatabaseMetaData
Idea

◦ From a Connection, derive a DatabaseMetaData


object

◦ Contains the comprehensive information about the


database as a whole

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.*;

public class MetaDataEx {

public static void main ( String args[ ]) {

try {

Class.forName(“Driver name”);

Connection con = DriverManager.getConnection(url, usr, pwd);

DatabaseMetaData dbMetadata = con.getMetaData();

……

JDBC 15
Example Code: Modify MetaDataEx
using DataBaseMetaData
String pName = dbMetaData.getDatabaseProductName();
System.out.println(“Database: ” + pName);

String pVer = dbMetaData.getDatabaseProductVersion();


System.out.println(“Version: ” + pVer);

String dName = dbMetaData.getDriverName();


System.out.println(“Driver: ” + dName);

boolean rOnly = dbMetaData.isReadOnly();


System.out.println(“Read-Only: ” + rOnly);

……

JDBC 16
Example Code: Modify MetaDataEx
using DataBaseMetaData

// create Statement & execute query

// process results

con.close();

}catch ( Exception ex) {


System.out.printl(ex);
}
} // end main
} // end class

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

You might also like