~ubuntu-branches/ubuntu/wily/libpgjava/wily

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/README

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
This is a simple readme describing how to compile and use the jdbc driver.
 
2
 
 
3
This file was amended on January 17 2001 to reflect the changes made in the 7.1
 
4
release.
 
5
 
 
6
---------------------------------------------------------------------------
 
7
 
 
8
This isn't a guide on how to use JDBC - for that refer to Javasoft's web site:
 
9
 
 
10
        http://www.javasoft.com/
 
11
 
 
12
For problems with this driver, then refer to the postgres-jdbc email
 
13
list:
 
14
 
 
15
        http://www.postgresql.org/
 
16
 
 
17
The Driver's home page is:
 
18
 
 
19
        http://jdbc.postgresql.org/
 
20
or      http://www.retep.org.uk/postgresql/
 
21
 
 
22
NB: They are both the same physical directory so both will always be in sync
 
23
(unless the laws of physics break down ;-) )
 
24
 
 
25
---------------------------------------------------------------------------
 
26
 
 
27
COMPILING
 
28
 
 
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.
 
33
 
 
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.
 
39
 
 
40
That jar file will contain the driver for _your_ version of the JDK.
 
41
 
 
42
Note: As of 7.1, you can build from the top-level directory or from
 
43
src/interfaces/jdbc.
 
44
 
 
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
 
47
platform.
 
48
 
 
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.
 
52
 
 
53
Don't try to run javac directly.  Don't try to run ant directly.  Neither
 
54
will work.
 
55
 
 
56
Possible problems
 
57
 
 
58
You may see a message similar to:
 
59
 
 
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);
 
62
 
 
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.
 
65
 
 
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/
 
69
 
 
70
More details are in the Implementation file src/interfaces/jdbc/Implementation
 
71
 
 
72
---------------------------------------------------------------------------
 
73
 
 
74
INSTALLING THE DRIVER
 
75
 
 
76
To install the driver, the .class files have to be in the classpath.
 
77
 
 
78
ie: under LINUX/SOLARIS (the example here is my linux box):
 
79
 
 
80
        export CLASSPATH=.:/usr/local/pgsql/share/java/postgresql.jar
 
81
 
 
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.
 
84
 
 
85
---------------------------------------------------------------------------
 
86
 
 
87
USING THE DRIVER
 
88
 
 
89
To use the driver, you must introduce it to JDBC. Again, there's two ways
 
90
of doing this:
 
91
 
 
92
1: Hardcoded.
 
93
 
 
94
   This method hardcodes your driver into your application/applet. You
 
95
   introduce the driver using the following snippet of code:
 
96
 
 
97
        try {
 
98
          Class.forName("org.postgresql.Driver");
 
99
        } catch(Exception e) {
 
100
          // your error handling code goes here
 
101
        }
 
102
 
 
103
   Remember, this method restricts your code to just the postgresql database.
 
104
   However, this is how most people load the driver.
 
105
 
 
106
2: Parameters
 
107
 
 
108
   This method specifies the driver from the command line. When running the
 
109
   application, you specify the driver using the option:
 
110
 
 
111
        -Djdbc.drivers=org.postgresql.Driver
 
112
 
 
113
   eg: This is an example of running one of my other projects with the driver:
 
114
 
 
115
        java -Djdbc.drivers=org.postgresql.Driver uk.org.retep.finder.Main
 
116
 
 
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).
 
121
 
 
122
---------------------------------------------------------------------------
 
123
 
 
124
JDBC URL syntax
 
125
 
 
126
The driver recognises JDBC URL's of the form:
 
127
 
 
128
        jdbc:postgresql:database
 
129
 
 
130
        jdbc:postgresql://host/database
 
131
 
 
132
        jdbc:postgresql://host:port/database
 
133
 
 
134
Also, you can supply both username and passwords as arguments, by appending
 
135
them to the URL. eg:
 
136
 
 
137
        jdbc:postgresql:database?user=me
 
138
        jdbc:postgresql:database?user=me&password=mypass
 
139
 
 
140
Notes:
 
141
 
 
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
 
144
   jdbc:postgresql:mydb
 
145
 
 
146
2) The port defaults to 5432 if it's left out.
 
147
 
 
148
---------------------------------------------------------------------------
 
149
 
 
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
 
153
 
 
154
To run them, they follow the same syntax. For example, the basic example shows
 
155
how to insert data, and perform queries:
 
156
 
 
157
        java example.basic jdbc:postgresql:test user password
 
158
 
 
159
---------------------------------------------------------------------------
 
160
 
 
161
POSTGRESQL SPECIFICS
 
162
--------------------
 
163
 
 
164
Large Objects:
 
165
 
 
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.
 
170
 
 
171
For example:
 
172
 
 
173
        Connection db;                  // open the connection here
 
174
        db.setAutoCommit(false);        // Turn off AutoCommit
 
175
 
 
176
                        ------------------
 
177
 
 
178
Large Object API
 
179
 
 
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.
 
183
 
 
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:
 
187
 
 
188
        LargeObjectManager lobj;
 
189
        lobj = ((org.postgresql.Connection)db).getLargeObjectAPI();
 
190
 
 
191
where db is a reference to an open Connection object.
 
192
 
 
193
Once that is done, you can use the API for the lifetime of that Connection.
 
194
 
 
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
 
197
sufficient:
 
198
 
 
199
       int oid = lobj.create(LargeObjectManager.READ|LargeObjectManager.WRITE);
 
200
 
 
201
Here, lobj is the LargeObjectManager we have opened earlier, and oid is the
 
202
Large Object's oid in the database.
 
203
 
 
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.
 
206
 
 
207
        LargeObject obj = lobj.open(oid,LargeObjectManager.WRITE);
 
208
 
 
209
Once the LargeObject is open, you can call methods to read, write, seek etc.
 
210
Here's the supported methods:
 
211
 
 
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
 
222
 
 
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.
 
227
 
 
228
                        ------------------
 
229
 
 
230
Date datatype:
 
231
 
 
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.
 
234
 
 
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
 
237
the change.
 
238
 
 
239
ie:
 
240
        Statement s = db.createStatement();
 
241
        ...
 
242
        s.executeUpdate("set datestyle='european'");
 
243
        s.executeUpdate("show datestyle");
 
244
        ..
 
245
        s.close();
 
246
 
 
247
Please note: This may change later, so that the driver uses the same format
 
248
internally (similar to how the ODBC driver works).
 
249
 
 
250
                        ------------------
 
251
 
 
252
JDBC supports database specific data types using the getObject() call. The
 
253
following types have their own Java equivalents supplied by the driver:
 
254
 
 
255
        box, circle, line, lseg, path, point, polygon
 
256
 
 
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.
 
260
 
 
261
        Eg: column 3 contains a point, and rs is the ResultSet:
 
262
 
 
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);
 
266
 
 
267
Also, when using these classes, their toString() methods return the correct
 
268
syntax for writing these to the database.
 
269
 
 
270
---------------------------------------------------------------------------
 
271
 
 
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
 
275
 
 
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.