1
This is a simple readme describing how to compile and use the jdbc driver.
3
This file was amended on January 17 2001 to reflect the changes made in the 7.1
6
---------------------------------------------------------------------------
8
This isn't a guide on how to use JDBC - for that refer to Javasoft's web site:
10
http://www.javasoft.com/
12
For problems with this driver, then refer to the postgres-jdbc email
15
http://www.postgresql.org/
17
The Driver's home page is:
19
http://jdbc.postgresql.org/
20
or http://www.retep.org.uk/postgresql/
22
NB: They are both the same physical directory so both will always be in sync
23
(unless the laws of physics break down ;-) )
25
---------------------------------------------------------------------------
29
To compile you will need to have ANT installed. To obtain ant go to
30
http://jakarta.apache.org/ant/index.html and download the binary. Being pure
31
java it will run on virtually all java platforms. If you have any problems
32
please email the jdbc list.
34
Once you have ANT, run the configure script in the top-level directory with
35
the --with-java option. Then proceed with 'make' and 'make install' as
36
usual. This will compile the correct driver for your JVM, and build a .jar
37
file (Java ARchive) called postgresql.jar. The file will be installed in
38
the directory PREFIX/share/java.
40
That jar file will contain the driver for _your_ version of the JDK.
42
Note: As of 7.1, you can build from the top-level directory or from
45
REMEMBER: Once you have compiled the driver, it will work on ALL platforms
46
that support that version of the API. You don't need to build it for each
49
That means you don't have to compile it on every platform. Believe me, I
50
still hear from people who ask me "I've compiled it ok under Solaris, but it
51
won't compile under Linux" - there's no difference.
53
Don't try to run javac directly. Don't try to run ant directly. Neither
58
You may see a message similar to:
60
postgresql/Driver.java:87: interface java.sql.Connection is an interface. It can't be instantiated.
61
return new Connection (host(), port(), props, database(), url, this);
63
This is caused by not having the current directory in your CLASSPATH. Under
64
Linux/Solaris, unset the CLASSPATH environment variable, and rerun ant.
66
If you are still having problems, I keep a copy of the driver (for different
67
versions of the backend) on my web site http://www.retep.org.uk/postgres/
68
or http://jdbc.postgresql.org/
70
More details are in the Implementation file src/interfaces/jdbc/Implementation
72
---------------------------------------------------------------------------
76
To install the driver, the .class files have to be in the classpath.
78
ie: under LINUX/SOLARIS (the example here is my linux box):
80
export CLASSPATH=.:/usr/local/pgsql/share/java/postgresql.jar
82
Please don't be tempted to extract the files from the .jar file. There are a
83
lot of files in there, and you may break the Exception handling.
85
---------------------------------------------------------------------------
89
To use the driver, you must introduce it to JDBC. Again, there's two ways
94
This method hardcodes your driver into your application/applet. You
95
introduce the driver using the following snippet of code:
98
Class.forName("org.postgresql.Driver");
99
} catch(Exception e) {
100
// your error handling code goes here
103
Remember, this method restricts your code to just the postgresql database.
104
However, this is how most people load the driver.
108
This method specifies the driver from the command line. When running the
109
application, you specify the driver using the option:
111
-Djdbc.drivers=org.postgresql.Driver
113
eg: This is an example of running one of my other projects with the driver:
115
java -Djdbc.drivers=org.postgresql.Driver uk.org.retep.finder.Main
117
note: This method only works with Applications (not for Applets).
118
However, the application is not tied to one driver, so if you needed
119
to switch databases (why I don't know ;-) ), you don't need to
120
recompile the application (as long as you havent hardcoded the url's).
122
---------------------------------------------------------------------------
126
The driver recognises JDBC URL's of the form:
128
jdbc:postgresql:database
130
jdbc:postgresql://host/database
132
jdbc:postgresql://host:port/database
134
Also, you can supply both username and passwords as arguments, by appending
137
jdbc:postgresql:database?user=me
138
jdbc:postgresql:database?user=me&password=mypass
142
1) If you are connecting to localhost or 127.0.0.1 you can leave it out of the
143
URL. ie: jdbc:postgresql://localhost/mydb can be replaced with
146
2) The port defaults to 5432 if it's left out.
148
---------------------------------------------------------------------------
150
That's the basics related to this driver. You'll need to read the JDBC Docs
151
on how to use it. However, there are some examples included in the example
152
directory. To build, type: make examples
154
To run them, they follow the same syntax. For example, the basic example shows
155
how to insert data, and perform queries:
157
java example.basic jdbc:postgresql:test user password
159
---------------------------------------------------------------------------
166
A "feature" of PostgreSQL is that access to LargeObjects is only permitted
167
within a Transaction. Because of this, any use of LargeObjects (also known
168
as Blobs) requires that the Connection.setAutoCommit() method be called
169
disabling the autocommit feature.
173
Connection db; // open the connection here
174
db.setAutoCommit(false); // Turn off AutoCommit
180
Most of the time, you can use the getBytes()/setBytes() methods to read and
181
write small Large Objects. However, PostgreSQL's own internal api's are
182
available. These allow you to access the object as if it was a file.
184
The first thing you need to do is to open the LargeObjectManager. This class
185
handles the opening of existing objects, and creating new ones. To do this,
186
you use the following line of code:
188
LargeObjectManager lobj;
189
lobj = ((org.postgresql.Connection)db).getLargeObjectAPI();
191
where db is a reference to an open Connection object.
193
Once that is done, you can use the API for the lifetime of that Connection.
195
To create an object, you call the create() method. This takes an argument
196
with the file modes you intend to use. The following line is normally
199
int oid = lobj.create(LargeObjectManager.READ|LargeObjectManager.WRITE);
201
Here, lobj is the LargeObjectManager we have opened earlier, and oid is the
202
Large Object's oid in the database.
204
To open an existing object, you use the open() method. This takes an oid, and
205
the file permissions. It then returns a LargeObject object.
207
LargeObject obj = lobj.open(oid,LargeObjectManager.WRITE);
209
Once the LargeObject is open, you can call methods to read, write, seek etc.
210
Here's the supported methods:
212
int oid = obj.getOID(); Return the objects oid
213
obj.close(); Close the object
214
byte data[] = obj.read(int len); Read len bytes
215
onj.read(byte data[],int off,int len); Read into data[off] len bytes
216
obj.write(byte data[]); Write the array data
217
obj.write(byte data[],int off,int len); Write len bytes from data[off]
218
obj.seek(int pos,int ref); As fseek in C.
219
obj.seek(int pos); Move to pos (from the begining)
220
int pos = obj.tell(); Returns the current position
221
int size = obj.size(); Returns the objects size
223
Caveat: If you commit(), rollback() a transaction, or turn on autocommit whilst
224
an object is open PostgreSQL will close it. You will need to reopen the object
225
before using it again. Using the existing LargeObject will cause an
226
SQLException to be thrown.
232
The driver now issues the "show datestyle;" query when it first connects, so
233
any call to ResultSet.getDate() how returns the correct date.
235
One caveat though: if you change the datestyle from within JDBC, you must also
236
issue the "show datestyle" query. Without this, the driver will not know of
240
Statement s = db.createStatement();
242
s.executeUpdate("set datestyle='european'");
243
s.executeUpdate("show datestyle");
247
Please note: This may change later, so that the driver uses the same format
248
internally (similar to how the ODBC driver works).
252
JDBC supports database specific data types using the getObject() call. The
253
following types have their own Java equivalents supplied by the driver:
255
box, circle, line, lseg, path, point, polygon
257
When using the getObject() method on a resultset, it returns a PG_Object,
258
which holds the postgres type, and its value. This object also supports
259
methods to retrive these types.
261
Eg: column 3 contains a point, and rs is the ResultSet:
263
PG_Object o = (PG_Object)rs.getObject(3);
264
PGpoint p = o.getPoint();
265
System.out.println("point returned x="+p.x+", y="+p.y);
267
Also, when using these classes, their toString() methods return the correct
268
syntax for writing these to the database.
270
---------------------------------------------------------------------------
272
Peter T Mount, December 29 1998
273
home email: peter@retep.org.uk http://www.retep.org.uk
274
work email: petermount@it.maidstone.gov.uk or peter@taer.maidstone.gov.uk
276
PS: Please use the home email whenever possible. If you must contact me at work
277
then please cc my home one at the same time.