27.10.2013 Views

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

connection that the application wants to obtain, in our case it is a connection to a<br />

<strong>Firebird</strong> database. Example of obtaining the connection is shown on Illustration<br />

2.2.<br />

package hello;<br />

import java.sql.*;<br />

public class HelloServer {<br />

}<br />

public static void main(String[] args) throws Exception {<br />

}<br />

Class.forName("org.firebirdsql.jdbc.FBDriver");<br />

Connection connection = DriverManager.getConnection(<br />

"jdbc:firebirdsql:localhost/3050:c:/db/employee.fdb",<br />

"SYSDBA", "masterkey");<br />

// do something here<br />

Illustration 2.2: Simple example shows how to obtain <strong>JDBC</strong> connection.<br />

The first line of this code is important – it tells JVM to load the <strong>Jaybird</strong> <strong>2.1</strong> <strong>JDBC</strong><br />

<strong>driver</strong>. According to <strong>JDBC</strong> specification, at this point <strong>driver</strong> registers itself in<br />

java.sql.DriverManager and tells it for which protocol it is responsible for.<br />

There are two ways to register <strong>JDBC</strong> <strong>driver</strong>:<br />

• Possibility 1. The application loads the <strong>driver</strong>'s class. The <strong>JDBC</strong> specification<br />

requires that during class initialization the <strong>driver</strong> performs the registration<br />

itself.<br />

Class.forName("org.firebirdsql.jdbc.FBDriver");<br />

• Possibility 2. The <strong>JDBC</strong> <strong>driver</strong> is listed in a jdbc.<strong>driver</strong>s system property.<br />

For example in your ~/.hotjava/properties file you can specify following<br />

line:<br />

jdbc.<strong>driver</strong>s=foo.Driver:org.firebirdsql.jdbc.FBDriver<br />

Alternatively you can specify the value of this property during JVM startup:<br />

java<br />

-Djdbc.<strong>driver</strong>s=org.firebirdsql.jdbc.FBDriver<br />

-classpath jaybird-full-<strong>2.1</strong>.6.jar;C:/myproject/classes<br />

my.company.Some<strong>Java</strong>Example<br />

The second statement of the example tells the java.sql.DriverManager to open<br />

database connection to the <strong>Firebird</strong> server running on the host where <strong>Java</strong> code is<br />

executed, and the path to the database is c:/database/employee.fdb.<br />

Database specification consists of the name of the server where the database<br />

server resides, optionally you can specify a port to which the <strong>driver</strong> will connect<br />

(by default port 3050 is used). The server name can be specified either using its IP<br />

address (for example 19<strong>2.1</strong>68.0.5) or using its DNS name (for example fbserver.mycompany.com<br />

or just fb-server).<br />

After the server name and port, path to the database is specified. The format in<br />

which the path is specified depends on the platform where the <strong>Firebird</strong> server<br />

Chapter 2. Obtaining a connection 10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!