Docsity

Meta Data - Web Design and Development - Lecture Handouts, Lecture notes of Web Design and Development

Toggle sidebar
bg1
Web Design & Development CS-506
- 204 -
Lecture 17
Meta Data
In simple terms, Meta Data is data (information) about data. The actual data has no
meaning without existence of Meta data. To clarify this, let’s look at an example. Given
below are listed some numeric values
1000
2000
4000
What this information about? We cannot state accurately. These values might be
representing some one’s salaries, price, tax payable & utility bill etc. But if we specify
Meta data about this data like shown below:
Salary
1000
2000
4000
Now, just casting a glance on these values, you can conclude that it’s all about some ones
salaries.
ResultSet Meta data
ResultSet Meta Data will help you in answering such questions
- How many columns are in the ResultSet?
- What is the name of given column?
- Are the column name case sensitive?
- 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?
Creating ResultSetMetaData object
From a ResultSet (the return type of executeQuery() ), derive a
ResultSetMetaData object by calling getMetaData() method as shown in the
given code snippet (here rs is a valid ResultSet object):
ResultSetMetaData rsmd = rs.getMetaData();
Now, rsmd can be used to look up number, names & types of columns
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Meta Data - Web Design and Development - Lecture Handouts and more Lecture notes Web Design and Development in PDF only on Docsity!

Lecture 17

Meta Data

In simple terms, Meta Data is data (information) about data. The actual data has no meaning without existence of Meta data. To clarify this, let’s look at an example. Given below are listed some numeric values

What this information about? We cannot state accurately. These values might be representing some one’s salaries, price, tax payable & utility bill etc. But if we specify Meta data about this data like shown below:

Salary 1000 2000 4000

Now, just casting a glance on these values, you can conclude that it’s all about some ones salaries.

ResultSet Meta data

ResultSet Meta Data will help you in answering such questions

  • How many columns are in the ResultSet?
  • What is the name of given column?
  • Are the column name case sensitive?
  • 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?

Creating ResultSetMetaData object

From a ResultSet (the return type of executeQuery() ), derive a ResultSetMetaData object by calling getMetaData() method as shown in the given code snippet (here rs is a valid ResultSet object):

ResultSetMetaData rsmd = rs.getMetaData();

Now, rsmd can be used to look up number, names & types of columns

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
  1. // changing line or printing an empty string
  2. System.out.println(“ ”);
  3. // printing all values of ResultSet by iterating over it
  4. String id, name, add, ph;
  5. while( rs.next() )
  6. {
  7. id = rs.getString(1);
  8. name = rs.getString(2);
  9. add = rs.getString(3);
  10. ph = rs.getString(4);
  11. System.out.println(id);
  12. System.out.println(“\t”);
  13. System.out.println(name);
  14. System.out.println(“\t”);
  15. System.out.println(add);
  16. System.out.println(“\t”);
  17. System.out.println(ph);
  18. System.out.println(“ ”);
  19. }
  20. //Step 8: close the connection
  21. con.close();
  22. }catch(Exception sqlEx){
  23. System.out.println(sqlEx);
  24. }
  25. } // end main
  26. } // end class

Compile & Execute:

The database contains the following values at the time of execution of this program. The database and the output are shown below:

Example Code 17.2: using DataBaseMetaData

This code is modification of the example code 17.1. Changes made are shown in bold face.

  1. // File MetaDataEx.java
  2. import java.sql.*;
  3. public class MetaDataEx {
  4. public static void main (String args[ ]) {
  5. try {
  6. //Step 2: load driver
  7. Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
  8. //Step 3: define the connection URL
  9. String url = “jdbc:odbc:personDSN”;
  10. //Step 4: establish the connection
  11. Connection con = null;
  12. con = DriverManager.getConnection(url, “”, “”);
  13. // getting DataBaseMetaDat object
  14. DataBaseMetaData dbMetaData = con.getMetaData();
  15. // printing database product name
  16. Sring pName = dbMetaData.getDatabaseProductName();
  17. System.out.println(“DataBase: ” + pName);
  18. // printing database product version
  19. Sring pVer = dbMetaData.getDatabaseProductVersion();
  20. System.out.println(“Version: ” + pVer);
  21. // printing driver name used to establish connection &
  22. // to retrieve data
  23. Sring dName = dbMetaData.getDriverName();
  24. System.out.println(“Driver: ” + dName);
  25. // printing whether database is read-only or not
  26. boolean rOnly = dbMetaData.isReadOnly();
  27. System.out.println(“Read-Only: ” + rOnly);
  28. // you can create & execute statements and can
  29. // process results over here if needed
  30. //Step 8: close the connection
  31. con.close();
  32. }catch(Exception sqlEx){
  33. System.out.println(sqlEx);
  34. }
  35. } // end main
  36. } // end class

Compile & Execute

On executing the above program, the following output will produce:

4. Type – 2: Native – API/partly Java driver

  • Converts JDBC calls into database-specific calls such as SQL Server, Informix, Oracle or Sybase.
  • Partly-Java drivers communicate with database-specific API (which may be in C/C++) using the Java Native Interface.
  • Significantly better Performance than the JDBC-ODBC bridge 4. Type – 3: Net – protocol/all–Java driver 4.
  • Follows a three-tiered approach whereby the JDBC database requests ()are passed through the network to the middle-tier server
  • Pure Java client to server drivers which send requests that are not database- specific to a server that translates them into a database-specific protocol. .
  • If the middle-tier server is written in java, it can use a type 1or type 2JDBC driver to do this

4. Type – 4: Native – protocol / all – java driver 4.

  • Converts JDBC calls into the vendor-specific DBMS protocol so that client application can communicate directly with the database server
  • Completely implemented in Java to achieve platform independence and eliminate deployment issues.
  • Performance is typically very good