Metadata is data about data.
Database metadata is information about a database.
Database metadata provides information about the structure of a database and its tables, views, and stored procedures.
JDBC provides four interfaces that deal with database metadata
Example :
Database metadata is information about a database.
Database metadata provides information about the structure of a database and its tables, views, and stored procedures.
JDBC provides four interfaces that deal with database metadata
- java.sql.DatabaseMetaData: about the database as a whole: table names, table indexes, database product name and version, and actions the database supports.
- java.sql.ResultSetMetaData: about the types and properties of the columns in a ResultSet object.
- java.sql.ParameterMetaData: about the types and properties of the parameters in a PreparedStatement object.
- javax.sql.RowSetMetaData: about the columns in a RowSet object.
Example :
public class JDBCDatabaseMetaDataExample { public static void main(String args[]) { try { // Loading database driver Class.forName("com.mysql.jdbc.Driver"); // Connecting to the database Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); DatabaseMetaData metaData = conn.getMetaData(); ResultSet rs = metaData.getTypeInfo(); System.out.println("Printing AllPremitive DataTypes supported by this Database Applications\n"); while (rs.next()) { System.out.println(rs.getString(1)); } rs.close(); Statement stmt = conn.createStatement(); ResultSet resultSet = stmt.executeQuery("SELECT * FROM student"); ResultSetMetaData md = resultSet.getMetaData(); System.out.println("\n Fetching Query............."); for (int i = 1; i <= md.getColumnCount(); i++) System.out.print(md.getColumnLabel(i) + " "); System.out.println(); while (resultSet.next()) { for (int i = 1; i <= md.getColumnCount(); i++) System.out.print(resultSet.getString(i) + " "); System.out.println(); } resultSet.close(); stmt.close(); conn.close(); } catch (SQLException e) { System.out.println(e.toString()); } catch (Exception e) { System.out.println(e); } } }
No comments:
Post a Comment