Changes

Jump to: navigation, search

User:Minooz/NexJ/database

6 bytes added, 15:44, 15 November 2010
JDBC
==JDBC==
: To make sure that the Driver class passes through the class loader, you can do a lookup by class name, as shown in the Java code snippet in this example.
<code> try {
Class.forName("org.postgresql.Driver");
The first part, jdbc, is a constant. It represents that you are connecting to a JDBC data source. The second part, [drivertype], represents the kind of database you want to connect to. Use postgresql to connect to a PostgreSQL database. The third part is passed off to the driver, which finds the actual database. It takes on one of the following formats:
<code> :databasename:--- //hostname/databasename: ------ //hostname:portnumber/databasename</code>
</code> :In the first case, the PostgreSQL database is running on the local machine, on the default port number. The databasename is the literal name of the database you wish to connect to. The second case is used for when you want to specify a hostname and a database. This also uses the default port number. The third case allows you to specify a port number as well. Even if you use the first type of URL, the JDBC connection will always be made via TCP/IP.
For the purposes of the examples from now on, this chapter will use the URL: jdbc:postgresql://localhost/booktown, meaning you are connecting to host localhost and database booktown. With that in mind, try to make a connection, using all you have learned so far. Example 12-2 shows a simple Java program that opens a JDBC connection to the booktown database. If you run the example yourself, be sure to replace the username and password with values that will work on your system.
1
edit

Navigation menu