JDBC's information on DatabaseMetadata objects

xiaoxiao2021-03-06  76

Original link:

http://dev.9cbs.net/Develop/Article/35/Article/35/Article/34/Article/34/34864.shtm

After connecting the database management system through JDBC, you get a Connection object, which can get various information about the database management system from this object, including each table in the database, each column, data type, trigger, storage Process and other information. Based on this information, JDBC can access a database that implements prior understanding. Getting this information is implemented on an object of the DatabaseMetadata class, while the DatabaseMetadata object is available on the Connection object.

Let's take a look at this example:

Package com.rongji.deemo;

Import java.sql.connection;

Import java.sql.driverManager;

Import java.sql.statement;

Import java.sql.databaseMetadata;

Public class dataconn {

Public dataconn () {

}

Public static void main (String [] args) {

Try

{

// Load the driver

// The following code is to load the JDBD-ODBC driver

Class.Forname ("Oracle.jdbc.driver.OracleDriver");

//establish connection

// Connect to DBMS with the appropriate driver, look at the following code [You can modify the database related information you are connected]:

String Url = "JDBC: Oracle: Thin: @ 192.168.4.45: 1521: Oemrep";

String User = "ums";

String password = "rongji";

// Create a connection with URL

Connection con = DRIVERMANAGER.GETCONNECTION (URL, User, Password);

/ / Get information about the database

Databasemetadata dbmetadata = con.getMetadata ();

/ / Return a String class object, representing the URL of the database

System.out.println ("URL:" DBMetadata.getURL () ";");

/ / Returns the username of the current database management system.

System.out.println ("Username:" DBMetadata.getusername () ";");

// Returns a boolean value indicating whether the database only allows a read operation.

System.out.println ("IsreadOnly:" DBMetadata.isreadOnly () ";");

/ / Return to the product name of the database.

System.out.println ("DatabaseProductName:" DBMetAdata.getDatabaseProductName () ";");

/ / Return to the version number of the database.

System.out.println ("DatabaseProductVersion:" DBMetadata.GetDatabaseProductVersion () ";");

// Returns the name of the drive driver.

System.out.println ("drivername:" dbmetadata.getdrivername () ";"); // Returns the version number of the driver.

System.out.println ("driverversion:" dbmetadata.getdriverversion ());

// Close connection

C. close ();

}

Catch (Exception E)

{

// Output exception information

System.err.println ("SQLException:" E.GetMessage ());

E.PrintStackTrace ();

}

}

}

Through the example above, we can see that the implementation of the object of the DatabaseMetadata class, as the following statement

<%

DatabaseMetadata DataMeta = con.getMetadata ();

%>

A number of methods for obtaining a data source are available in a very detailed understanding of the database in a DatabaseMetadata class. Just as the information displayed in our above example [Other methods, please refer to the DatabaseMetadata class in the JDK API]:

GetURL ()

Returns a String class object that represents the URL of the database.

Getusername ()

Returns the username of the current database management system.

IsreadOnly ()

Returns a Boolean value indicating whether the database only allows read operations.

GetDatabaseProductName ()

Returns the product name of the database.

GetDatabaseProductVersion ()

Returns the version number of the database.

GetDrivername ()

Returns the name of the drive driver.

GetDriverversion ()

Returns the version number of the driver.

转载请注明原文地址:https://www.9cbs.com/read-94028.html

New Post(0)