Adv Java - [JDBC: Introduction]

♠ Posted by Unknown in at 01:10

JDBC [Java DataBase Connectivity]


Introduction:
It has been estimated that half of all software development involves client/server operations. A great promise of Java has been the ability to build platform-independent client/server database applications. This has come to fruition with Java DataBase Conectivity (JDBC).


JDBC, like many of the APIs in Java, is designed for simplicity. The method calls you make correspond to the logical operations you’d think of doing when gathering data from a database: connect to the database, create a statement and execute the query, and look at the result set.

To allow this platform independence, JDBC provides a driver manager that dynamically maintains all the driver objects that your database queries will need. So if you have three different kinds of vendor databases to connect to, you’ll need three different driver objects. The driver objects register themselves with the driver manager at the time of loading, and you can see force the loading using Class.forName().

To open a database, you must create a “database URL” that specifies:

  1. That you’re using JDBC with “jdbc”.
  2. The “subprotocol”: the name of the driver or the name of a database connectivity mechanism. Since the design of JDBC was inspired by ODBC, the first subprotocol available is the “jdbc-odbc bridge,“ specified by “odbc”.
  3. The database idenfitier. This varies with the database driver used, but it generally provides a logical name that is mapped by the database administration software to a physical directory where the database tables are located. For your database identifier to have any meaning, you must register the name using your database administration software.

All this information is combined into one string, the “database URL”. For example, to connect through the ODBC subprotocol to a database identified as “student,” the database URL could be:

          String dbUrl = “jdbc:odbc:people”;

If you’re connecting across a network, the database URL will contain the connection information identifying the remote machine and can become a bit intimidating. Here is an example of a CloudScape database being called from a remote client utilizing RMI:

          jdbc:rmi://792.168.170.27.1099/jdbc:cloudscape:db

This database URL is really two jdbc calls in one. The first part “jdbc:rmi://792.168.170.27.1099/”uses RMI to make the connection to the remote database engine listening on port 1099 at IP Address 192.168.170.27. the second part of the URL, “jdbc:cloudscape:db” Conveys the more typical settings using the subprotocol and database name but this will only happen after the first section has made the connection via RMI to the remote machine.


When you are ready to connect to the database, call the static method DriverManager.getConnection() and pass it the database URL, the user name, and a password to get into the database. You get back a Connection object that you can then use to query and manipulate the database.


0 comments:

Post a Comment