jdbc.sgml 73.9 KB
Newer Older
1
<!--
T
Tatsuo Ishii 已提交
2
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/jdbc.sgml,v 1.33 2001/11/27 13:02:33 ishii Exp $
3 4
-->

5
 <chapter id="jdbc">
6
  <title><acronym>JDBC</acronym> Interface</title>
7

8 9 10
  <note>
   <title>Author</title>
   <para>
B
Barry Lind 已提交
11 12
    Originally written by Peter T. Mount (<email>peter@retep.org.uk</email>), 
    the original author of the <acronym>JDBC</acronym> driver.
13 14
   </para>
  </note>
15 16 17 18 19 20 21 22

  <para>
   <acronym>JDBC</acronym> is a core <acronym>API</acronym> of Java 1.1 and later.
   It provides a standard set of
   interfaces to <acronym>SQL</acronym>-compliant databases.
  </para>

  <para>
23
   <productname>PostgreSQL</> provides a <firstterm>type
24 25 26 27 28
   4</firstterm> <acronym>JDBC</acronym> Driver.  Type 4 indicates
   that the driver is written in Pure Java, and communicates in the
   database system's own network protocol. Because of this, the driver
   is platform independent; once compiled, the driver can be used on
   any system.
29 30
  </para>

31 32 33 34 35 36 37 38
  <para>
   This chapter is not intended as a complete guide to
   <acronym>JDBC</acronym> programming, but should help to get you
   started. For more information refer to the standard
   <acronym>JDBC</acronym> <acronym>API</acronym> documentation.
   Also, take a look at the examples included with the source. The
   basic example is used here.
  </para>
39

40 41
 <sect1 id="jdbc-setup">
  <title>Setting up the <acronym>JDBC</acronym> Driver</title>
42

43
  <sect2 id="jdbc-build">
B
Barry Lind 已提交
44
   <title>Getting the Driver</title>
45

46
   <para>
B
Barry Lind 已提交
47
    Precompiled versions of the driver can be downloaded from
48 49
    the <ulink
    url="http://jdbc.postgresql.org"><productname>PostgreSQL</productname>
B
Barry Lind 已提交
50 51 52 53 54 55 56
    <acronym>JDBC</acronym> web site</ulink>.
   </para>

   <para>
    Alternatively you can build the driver from source.  Although you 
    should only need to do this if you are making changes to the source
    code.
57
   </para>
58

59 60
   <para>
    Starting with <productname>PostgreSQL</productname> version 7.1,
61 62
    the <acronym>JDBC</acronym> driver is built using
    <application>Ant</application>, a special tool for building
63
    Java-based packages.  You should download
64 65 66 67 68 69 70 71 72 73 74
    <application>Ant</application> from the <ulink
    url="http://jakarta.apache.org/ant/index.html"><application>Ant</application>
    web site</ulink> and install it before proceeding.  Precompiled
    <application>Ant</application> distributions are typically set up
    to read a file <filename>.antrc</filename> in the current user's
    home directory for configuration.  For example, to use a different
    <acronym>JDK</acronym> than the default, this may work:
<programlisting>
JAVA_HOME=/usr/local/sun-jdk1.3
JAVACMD=$JAVA_HOME/bin/java
</programlisting>
75
   </para>
76

77
   <para>
B
Barry Lind 已提交
78
    To build the driver, add the <option>--with-java</option> option to your
79
    <filename>configure</filename> command line, e.g.,
80
<screen>
81
<prompt>$</prompt> <userinput>./configure --prefix=<replaceable>xxx</replaceable> --with-java ...</userinput>
82
</screen>
83 84
    This will build and install the driver along with the rest of the
    <productname>PostgreSQL</productname> package when you issue the
B
Barry Lind 已提交
85
    <literal>make/gmake</literal> and <literal>make/gmake install</literal>
86 87 88 89
    commands.  If you only want to build the driver and not the rest
    of <productname>PostgreSQL</productname>, change into the
    directory <filename
    class="directory">src/interfaces/jdbc</filename> and issue the
B
Barry Lind 已提交
90
    respective <literal>make/gmake</literal> command there.  Refer to the
91 92
    <productname>PostgreSQL</productname> installation instructions
    for more information about the configuration and build process.
93 94
   </para>

B
Barry Lind 已提交
95 96 97 98 99 100 101 102 103 104
   <para>When building the driver from source the jar file that is created
    will be named <filename>postgresql.jar</filename>.  The build will 
    create this file in the <filename>src/interfaces/jdbc/jars</filename>
    directory.  The resulting driver will be built for the version of 
    Java you are running.  If you build with a 1.1 JDK you will build
    a version that supports the jdbc1 specification, if you build with a 
    Java2 JDK (i.e. JDK1.2 or JDK1.3) you will build a version that 
    supports the jdbc2 specification. 
   </para>
 
105
   <note>
106
    <para>
B
Barry Lind 已提交
107 108
     Do not try to build the driver by calling <command>javac</command> 
     directly, as the driver uses some dynamic loading techniques for
109 110 111 112 113
     performance reasons, and <command>javac</command> cannot cope.
     Do not try to run <command>ant</command> directly either, because
     some configuration information is communicated through the
     makefiles.  Running <command>ant</command> directly without
     providing these parameters will result in a broken driver.
114
    </para>
115 116
   </note>
  </sect2>
117

118 119
  <sect2 id="jdbc-classpath">
   <title>Setting up the Class Path</title>
120

121
   <para>
B
Barry Lind 已提交
122 123 124 125 126 127
    To use the driver, the jar archive (named
    <filename>postgresql.jar</filename> if you built from source, otherwise
    it will likely be named <filename>jdbc7.2-1.1.jar</filename> or 
    <filename>jdbc7.2-1.2.jar</filename> for the jdbc1 and jdbc2 versions
    respectively)
    needs to be included in the
128 129
    class path, either by putting it in the <envar>CLASSPATH</envar>
    environment variable, or by using flags on the
130 131 132 133 134
    <command>java</command> command line.  By default, the jar archive
    is installed in the directory <filename
    class="directory">/usr/local/pgsql/share/java</filename>.  You may
    have it in a different directory if you used the
    <option>--prefix</option> option when you ran
B
Barry Lind 已提交
135 136
    <filename>configure</filename>, or if you are using a binary distribution
    that places it in some different location.
137
   </para>
138

139 140 141 142 143 144 145 146
   <informalexample>
    <para>
     For instance, I have an application that uses the
     <acronym>JDBC</acronym> driver to access a large database
     containing astronomical objects. I have the application and the
     <acronym>JDBC</acronym> driver installed in the <filename
     class="directory">/usr/local/lib</filename> directory, and the
     Java <acronym>JDK</acronym> installed in
B
Barry Lind 已提交
147
     <filename>/usr/local/jdk1.3.1</filename>.  To run the
148 149
     application, I would use:
<programlisting>
150
export CLASSPATH=/usr/local/lib/finder.jar<co id="co.jdbc-finder">:/usr/local/pgsql/share/java/postgresql.jar:.
B
Barry Lind 已提交
151
java Finder
152 153 154 155
</programlisting>
     <calloutlist>
      <callout arearefs="co.jdbc-finder">
       <para>
B
Barry Lind 已提交
156
        <filename>finder.jar</filename> contains the Finder application.
157 158 159 160
       </para>
      </callout>
     </calloutlist>
    </para>
161

162 163 164 165 166 167
    <para>
     Loading the driver from within the application is covered in
     <xref linkend="jdbc-use">.
    </para>
   </informalexample>
  </sect2>
168

169
  <sect2 id="jdbc-prepare">
170 171 172
   <title>Preparing the Database for <acronym>JDBC</acronym></title>

   <para>
B
Barry Lind 已提交
173 174 175 176 177 178
    Because Java only uses TCP/IP connections, the
    <application>PostgreSQL</application> server must be configured to
    accept TCP/IP connections.  This can be done by setting 
    <literal>tcpip_socket = true</literal> in the 
    <filename>postgresql.conf</filename> file or by supplying the
    <option>-i</option> option flag when starting
179
    <command>postmaster</command>.
180 181 182
   </para>

   <para>
183 184 185 186
    Also, the client authentication setup in the
    <filename>pg_hba.conf</filename> file may need to be configured.
    Refer to the <citetitle>Administrator's Guide</citetitle> for
    details.  The <acronym>JDBC</acronym> Driver supports trust,
B
Barry Lind 已提交
187
    ident, password, md5, and crypt authentication methods.
188
   </para>
189 190
  </sect2>
 </sect1>
191

192 193
 <sect1 id="jdbc-use">
  <title>Using the Driver</title>
194

195
  <sect2 id="jdbc-import">
196 197 198
   <title>Importing <acronym>JDBC</acronym></title>

   <para>
199 200
    Any source that uses <acronym>JDBC</acronym> needs to import the
    <literal>java.sql</literal> package, using:
201

202
<programlisting>
203
import java.sql.*;
204
</programlisting>
205 206 207

    <important>
     <para>
208 209 210
      Do not import the <literal>org.postgresql</literal> package. If
      you do, your source will not compile, as
      <command>javac</command> will get confused.
211 212 213
     </para>
    </important>
   </para>
214
  </sect2>
215

216
  <sect2 id="jdbc-load">
217 218 219
   <title>Loading the Driver</title>

   <para>
220 221 222
    Before you can connect to a database, you need to load the
    driver. There are two methods available, and it depends on your
    code which is the best one to use.
223 224 225 226 227
   </para>

   <para>
    In the first method, your code implicitly loads the driver using the
    <function>Class.forName()</function> method.
228
    For <productname>PostgreSQL</>, you would use:
229

230
<programlisting>
231
Class.forName("org.postgresql.Driver");
232
</programlisting>
233 234 235 236

    This will load the driver, and while loading, the driver will automatically
    register itself with <acronym>JDBC</acronym>.

237 238 239 240 241 242 243
    <note>
     <para>
      The <function>forName()</function> method can throw a
      <classname>ClassNotFoundException</classname> if the driver is
      not available.
     </para>
    </note>
244 245 246
   </para>

   <para>
247
    This is the most common method to use, but restricts your code to
248
    use just <productname>PostgreSQL</productname>.  If your code may
249
    access another database system in the future, and you do not use
250
    any <productname>PostgreSQL</productname>-specific extensions, then
251
    the second method is advisable.
252 253 254
   </para>

   <para>
255 256 257 258 259 260 261 262 263
    The second method passes the driver as a parameter to the
    <acronym>JVM</acronym> as it starts, using the <option>-D</option>
    argument. Example:
<programlisting>
java -Djdbc.drivers=org.postgresql.Driver example.ImageViewer
</programlisting>
    In this example, the <acronym>JVM</acronym> will attempt to load
    the driver as part of its initialization. Once done, the
    <classname>ImageViewer</classname> is started.
264 265 266
   </para>

   <para>
267 268 269 270
    Now, this method is the better one to use because it allows your
    code to be used with other database packages without recompiling
    the code. The only thing that would also change is the connection
    <acronym>URL</acronym>, which is covered next.
271 272 273
   </para>

   <para>
274 275 276 277 278
    One last thing: When your code then tries to open a
    <classname>Connection</classname>, and you get a <errorname>No
    driver available</errorname> <classname>SQLException</classname>
    being thrown, this is probably caused by the driver not being in
    the class path, or the value in the parameter not being correct.
279
   </para>
280
  </sect2>
281

282
  <sect2 id="jdbc-connect">
283 284 285
   <title>Connecting to the Database</title>

   <para>
286 287
    With <acronym>JDBC</acronym>, a database is represented by a
    <acronym>URL</acronym> (Uniform Resource Locator).  With
288
    <application>PostgreSQL</application>, this takes one of the
289
    following forms:
290 291 292

    <itemizedlist>
     <listitem>
293 294 295
<synopsis>
jdbc:postgresql:<replaceable class="parameter">database</replaceable>
</synopsis>
296 297 298
     </listitem>

     <listitem>
299 300 301
<synopsis>
jdbc:postgresql://<replaceable class="parameter">host</replaceable>/<replaceable class="parameter">database</replaceable>
</synopsis>
302 303 304
     </listitem>

     <listitem>
305 306 307
<synopsis>
jdbc:postgresql://<replaceable class="parameter">host</replaceable>:<replaceable class="parameter">port</replaceable>/<replaceable class="parameter">database</replaceable>
</synopsis>
308 309 310 311 312 313 314 315 316 317 318 319
     </listitem>
    </itemizedlist>

    where:

    <variablelist>
     <varlistentry>
      <term>
       <replaceable class="parameter">host</replaceable>
      </term>
      <listitem>
       <para>
320
        The host name of the server. Defaults to <literal>localhost</literal>.
321 322 323 324 325 326 327 328 329 330
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>
       <replaceable class="parameter">port</replaceable>
      </term>
      <listitem>
       <para>
331
        The port number the server is listening on. Defaults to the
332
        <productname>PostgreSQL</productname> standard port number (5432).
333 334 335 336 337 338 339 340 341 342
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>
       <replaceable class="parameter">database</replaceable>
      </term>
      <listitem>
       <para>
343
        The database name.
344 345 346 347 348 349 350
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>

   <para>
351
    To connect, you need to get a <classname>Connection</classname> instance from 
352
    <acronym>JDBC</acronym>. To do this,
353
    you would use the <function>DriverManager.getConnection()</function> method:
354

355 356 357
<programlisting>
Connection db = DriverManager.getConnection(url, username, password);
</programlisting>
358
   </para>
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  </sect2>

  <sect2 id="jdbc-disconnect">
   <title>Closing the Connection</title>

   <para>
    To close the database connection, simply call the
    <function>close()</function> method to the <classname>Connection</classname>:
<programlisting>
db.close();
</programlisting>
   </para>
  </sect2>
 </sect1>

374

375
  <sect1 id="jdbc-query">
376 377 378
   <title>Issuing a Query and Processing the Result</title>

   <para>
379
    Any time you want to issue <acronym>SQL</acronym> statements to
B
Barry Lind 已提交
380 381
    the database, you require a <classname>Statement</classname> or 
    <classname>PreparedStatement</classname> instance. Once you have 
382 383
    a <classname>Statement</classname> or 
    <classname>PreparedStatement</classname>, you can use issue a
384 385 386
    query. This will return a <classname>ResultSet</classname>
    instance, which contains the entire result.  <xref
    linkend="jdbc-query-example"> illustrates this process.
387 388
   </para>

389 390 391 392
   <example id="jdbc-query-example">
    <title>Processing a Simple Query in <acronym>JDCB</acronym></title>

    <para>
B
Barry Lind 已提交
393 394
     This example will issue a simple query and print out the first
     column of each row using a <classname>Statement</classname>.
395 396
<programlisting>
Statement st = db.createStatement();
B
Barry Lind 已提交
397 398 399 400 401 402 403 404 405 406 407
ResultSet rs = st.executeQuery("SELECT * FROM mytable where columnfoo = 500");
while(rs.next()) {
    System.out.print("Column 1 returned ");
    System.out.println(rs.getString(1));
}
rs.close();
st.close();
</programlisting>
    </para>

    <para>
408
     This example will issue the same query as before using 
B
Barry Lind 已提交
409 410 411 412 413 414 415
     a <classname>PreparedStatement</classname>
     and a bind value in the query.
<programlisting>
int foovalue = 500;
PreparedStatement st = db.prepareStatement("SELECT * FROM mytable where columnfoo = ?");
st.setInt(1, foovalue);
ResultSet rs = st.executeQuery();
416 417 418 419 420 421 422 423 424 425
while(rs.next()) {
    System.out.print("Column 1 returned ");
    System.out.println(rs.getString(1));
}
rs.close();
st.close();
</programlisting>
    </para>
   </example>

426
   <sect2>
B
Barry Lind 已提交
427 428
    <title>Using the <classname>Statement</classname> or <classname>
    PreparedStatement</classname> Interface</title>
429 430

    <para>
431
     The following must be considered when using the
432 433
     <classname>Statement</classname> or 
     <classname>PreparedStatement</classname> interface:
434 435 436 437

     <itemizedlist>
      <listitem>
       <para>
438 439 440 441 442
        You can use a single <classname>Statement</classname> instance
        as many times as you want. You could create one as soon as you
        open the connection and use it for the connection's
        lifetime. But you have to remember that only one
        <classname>ResultSet</classname> can exist per
443 444
        <classname>Statement</classname> or 
        <classname>PreparedStatement</classname> at a given time.
445 446 447 448 449
       </para>
      </listitem>

      <listitem>
       <para>
450 451 452
        If you need to perform a query while processing a
        <classname>ResultSet</classname>, you can simply create and
        use another <classname>Statement</classname>.
453 454
       </para>
      </listitem>
455

456 457
      <listitem>
       <para>
458 459 460 461
        If you are using threads, and several are using the database,
        you must use a separate <classname>Statement</classname> for
        each thread. Refer to <xref linkend="jdbc-thread"> if you are
        thinking of using threads, as it covers some important points.
462 463
       </para>
      </listitem>
B
Barry Lind 已提交
464 465 466 467

      <listitem>
       <para>
        When you are done using the <classname>Statement</classname>
468 469
        or <classname>PreparedStatement</classname>
        you should close it.
B
Barry Lind 已提交
470 471
       </para>
      </listitem>
472 473 474 475 476
     </itemizedlist>
    </para>
   </sect2>

   <sect2>
477
    <title>Using the <classname>ResultSet</classname> Interface</title>
478 479

    <para>
480 481
     The following must be considered when using the
     <classname>ResultSet</classname> interface:
482 483 484 485

     <itemizedlist>
      <listitem>
       <para>
486 487 488 489
        Before reading any values, you must call
        <function>next()</function>. This returns true if there is a
        result, but more importantly, it prepares the row for
        processing.
490 491 492 493 494
       </para>
      </listitem>

      <listitem>
       <para>
495 496 497
        Under the <acronym>JDBC</acronym> specification, you should
        access a field only once. It is safest to stick to this rule,
        although at the current time, the
498
        <productname>PostgreSQL</productname> driver will allow you to
499
        access a field as many times as you want.
500 501 502 503 504
       </para>
      </listitem>

      <listitem>
       <para>
505 506
        You must close a <classname>ResultSet</classname> by calling
        <function>close()</function> once you have finished using it.
507 508 509 510 511
       </para>
      </listitem>

      <listitem>
       <para>
512 513 514 515 516
        Once you make another query with the
        <classname>Statement</classname> used to create a
        <classname>ResultSet</classname>, the currently open
        <classname>ResultSet</classname> instance is closed
        automatically.
517 518 519 520 521 522
       </para>
      </listitem>
     </itemizedlist>
    </para>
   </sect2>
  </sect1>
523

524
  <sect1 id="jdbc-update">
525
   <title>Performing Updates</title>
526

527
   <para>
B
Barry Lind 已提交
528 529 530 531 532 533 534 535
    To change data (perform an insert, update, or delete) 
    you use the <function>executeUpdate()</function> method.
    <function>executeUpdate()</function> is similar to the
    <function>executeQuery()</function> used to issue a select,
    however it doesn't return a <classname>ResultSet</classname>,
    instead it returns the number of records affected by the insert,
    update, or delete statement.
   </para>
536

537 538
  <example id="jdbc-delete-example">
   <title>Simple Delete Example</title>
B
Barry Lind 已提交
539 540 541
   <para>
     This example will issue a simple delete and print out the number
     of rows deleted.
542
<programlisting>
B
Barry Lind 已提交
543 544 545 546 547 548
int foovalue = 500;
PreparedStatement st = db.prepareStatement("DELETE FROM mytable where columnfoo = ?");
st.setInt(1, foovalue);
int rowsDeleted = st.executeUpdate();
System.out.println(rowsDeleted + " rows deleted");
st.close();
549
</programlisting>
550
   </para>
551
  </example>
552 553
  </sect1>

B
Barry Lind 已提交
554 555 556 557 558 559 560 561 562 563 564
  <sect1 id="jdbc-ddl">
   <title>Creating and Modifying Database Objects</title>

   <para>
    To create, modify or drop a database object like a table or view
    you use the <function>execute()</function> method.
    <function>execute</function> is similar to the
    <function>executeQuery()</function> used to issue a select,
    however it doesn't return a result.
   </para>

565 566
  <example id="jdbc-drop-table-example">
   <title>Drop Table Example</title>
B
Barry Lind 已提交
567 568 569 570 571 572 573 574
   <para>
     This example will drop a table.
<programlisting>
Statement st = db.createStatement();
ResultSet rs = st.executeQuery("DROP TABLE mytable");
st.close();
</programlisting>
   </para>
575
  </example>
B
Barry Lind 已提交
576 577 578 579 580 581
  </sect1>

 <sect1 id="jdbc-binary-data">
  <title>Storing Binary Data</title>

  <para>
582
    <application>PostgreSQL</application> provides two distinct ways to 
B
Barry Lind 已提交
583
    store binary data.  Binary data can be stored in a table using 
B
Bruce Momjian 已提交
584
    <application>PostgreSQL's</application> binary datatype 
B
Barry Lind 已提交
585 586
    <type>bytea</type>, or by using the <firstterm>Large Object</firstterm>
    feature which stores the binary data in a separate table in a special 
587 588
    format, and refers to that table by storing a value of type 
    <type>OID</type> in your table.
B
Barry Lind 已提交
589 590 591 592 593 594 595
  </para>

  <para>
    In order to determine which method is appropriate you 
    need to understand the limitations of each method.  The 
    <type>bytea</type> datatype is not well suited for storing very 
    large amounts of binary data.  While a column of type 
B
Bruce Momjian 已提交
596
    <type>bytea</type> can hold upto 1Gig of binary data, it would 
B
Barry Lind 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609
    require a huge amount of memory (<acronym>RAM</acronym>) to 
    process such a large value.  The Large Object method for 
    storing binary data is better suited to storing very large values, 
    but it has its own limitations.  Specifically deleting a row 
    that contains a Large Object does not delete the Large Object.
    Deleting the Large Object is a separate operation that needs to
    be performed.  Large Objects also have some security
    issues since anyone connected to the database case view 
    and/or modify any Large Object, even if they don't have 
    permissions to view/update the row containing the Large Object.
  </para>

  <para>
610 611
    7.2 is the first release of the <acronym>JDBC</acronym> Driver 
    that supports the <type>bytea</type> datatype.  The introduction of 
B
Barry Lind 已提交
612 613 614 615 616 617 618 619 620 621 622 623
    this functionality in 7.2 has introduced a change in behavior 
    as compared to previous releases.  In 7.2 the methods 
    <function>getBytes()</function>, <function>setBytes()</function>, 
    <function>getBinaryStream()</function>, and 
    <function>setBinaryStream()</function> operate on 
    the <type>bytea</type> datatype.  In 7.1 these methods operated 
    on the <type>OID</type> datatype associated with Large Objects.  
    It is possible to revert the driver back to the old 7.1 behavior 
    by setting the <parameter>compatible</parameter> property on 
    the <classname>Connection</classname> to a value of 
    <literal>7.1</literal>
  </para>
624

625
  <para>
B
Barry Lind 已提交
626 627 628 629 630 631 632 633 634
    To use the <type>bytea</type> datatype you should simply use 
    the <function>getBytes()</function>, <function>setBytes()</function>,
    <function>getBinaryStream()</function>, or 
    <function>setBinaryStream()</function> methods.
  </para>

  <para>
    To use the Large Object functionality you can use either the 
    <classname>LargeObject</classname> <acronym>API</acronym>
B
Bruce Momjian 已提交
635
    provided by the <application>PostgreSQL</application> 
B
Barry Lind 已提交
636 637 638
    <acronym>JDBC</acronym> Driver, or by using the 
    <function>getBLOB()</function> and <function>setBLOB()</function>
    methods.
639
  </para>
640

641
  <important>
642
   <para>
B
Barry Lind 已提交
643
    For <application>PostgreSQL</application>, you must access Large
644 645 646
    Objects within an <acronym>SQL</acronym> transaction.  You would
    open a transaction by using the
    <function>setAutoCommit()</function> method with an input
B
Barry Lind 已提交
647
    parameter of <literal>false</literal>.
648
   </para>
649
  </important>
650

B
Barry Lind 已提交
651 652 653 654 655
  <note><para>In a future release of the
      <acronym>JDBC</acronym> Driver, the <function>getBLOB()</function>
      and <function>setBLOB()</function> methods may no longer 
      interact with Large Objects and will instead work on 
      <type>bytea</type> datatypes.  So it is recommended that you 
B
Bruce Momjian 已提交
656
      use the <classname>LargeObject</classname> <acronym>API</acronym> 
B
Barry Lind 已提交
657 658
      if you intend to use Large Objects.
  </para></note>
659 660


B
Barry Lind 已提交
661 662
  <example id="jdbc-binary-data-example">
   <title>Binary Data Examples</title>
663

664 665
   <para>
    For example, suppose you have a table containing the file name of
B
Barry Lind 已提交
666 667
    an image and you also want to store the image in a <type>bytea</type>
    column:
668
<programlisting>
B
Barry Lind 已提交
669
CREATE TABLE images (imgname text, img bytea);
670
</programlisting>
671
   </para>
672

673 674
   <para>
    To insert an image, you would use:
675
<programlisting>
676
File file = new File("myimage.gif");
677
FileInputStream fis = new FileInputStream(file);
B
Barry Lind 已提交
678
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
679 680
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, file.length());
681 682 683
ps.executeUpdate();
ps.close();
fis.close();
684
</programlisting>
B
Barry Lind 已提交
685 686 687 688 689 690

    Here, <function>setBinaryStream()</function> transfers a set number
    of bytes from a stream into the column of type <type>bytea</type>.
    This also could have been done using the <function>setBytes()</function>
    method if the contents of the image was already in a 
    <classname>byte[]</classname>. 
691
   </para>
692

693
   <para>
694 695 696
    Retrieving an image is even easier.  (We use
    <classname>PreparedStatement</classname> here, but the
    <classname>Statement</classname> class can equally be used.)
697

698
<programlisting>
B
Barry Lind 已提交
699
PreparedStatement ps = con.prepareStatement("SELECT img FROM images WHERE imgname=?");
700
ps.setString(1, "myimage.gif");
701
ResultSet rs = ps.executeQuery();
702
if (rs != null) {
703
    while(rs.next()) {
B
Barry Lind 已提交
704
        byte[] imgBytes = rs.getBytes(1);
705
        // use the stream in some way here
706 707 708 709
    }
    rs.close();
}
ps.close();
710
</programlisting>
711 712 713
   </para>

   <para>
714
    Here the binary data was retrieved as an
B
Barry Lind 已提交
715 716
    <classname>byte[]</classname>.  You could have used a 
    <classname>InputStream</classname> object instead.  
717
   </para>
B
Barry Lind 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

   <para>
    Alternativly you could be storing a very large file and want to use
    the <classname>LargeObject</classname> <acronym>API</acronym> to 
    store the file:
<programlisting>
CREATE TABLE imagesLO (imgname text, imgOID OID);
</programlisting>
   </para>

   <para>
    To insert an image, you would use:
<programlisting>
// All LargeObject API calls must be within a transaction
conn.setAutoCommit(false);
733 734

// Get the Large Object Manager to perform operations with
B
Barry Lind 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
LargeObjectManager lobj = ((org.postgresql.Connection)conn).getLargeObjectAPI();

//create a new large object
int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);

//open the large object for write
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);

// Now open the file
File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);

// copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0)
{
	obj.write(buf, 0, s);
	tl += s;
}

// Close the large object
obj.close();

//Now insert the row into imagesLO
PreparedStatement ps = conn.prepareStatement("INSERT INTO imagesLO VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setInt(2, oid);
ps.executeUpdate();
ps.close();
fis.close();
</programlisting>
T
Tatsuo Ishii 已提交
767
</para>
B
Barry Lind 已提交
768 769 770 771 772 773
   <para>
    Retrieving the image from the Large Object:

<programlisting>
// All LargeObject API calls must be within a transaction
conn.setAutoCommit(false);
774 775

// Get the Large Object Manager to perform operations with
B
Barry Lind 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
LargeObjectManager lobj = ((org.postgresql.Connection)conn).getLargeObjectAPI();

PreparedStatement ps = con.prepareStatement("SELECT imgOID FROM imagesLO WHERE imgname=?");
ps.setString(1, "myimage.gif");
ResultSet rs = ps.executeQuery();
if (rs != null) {
    while(rs.next()) {
	//open the large object for reading
	int oid = rs.getInt(1);
	LargeObject obj = lobj.open(oid, LargeObjectManager.READ);

	//read the data
	byte buf[] = new byte[obj.size()];
	obj.read(buf, 0, obj.size());
	//do something with the data read here
791

B
Barry Lind 已提交
792 793 794 795 796 797 798 799 800
	// Close the object
	obj.close();
    }
    rs.close();
}
ps.close();
</programlisting>
   </para>

801 802
  </example>
 </sect1>
803 804


805 806 807
 <sect1 id="jdbc-ext">
  <title><application>PostgreSQL</application> Extensions to the
    <acronym>JDBC</acronym> <acronym>API</acronym></title>
808

809
  <para>
810
   <productname>PostgreSQL</productname> is an extensible database
811 812
   system.  You can add your own functions to the backend, which can
   then be called from queries, or even add your own data types.  As
813
   these are facilities unique to <productname>PostgreSQL</productname>,
814 815 816 817 818
   we support them from Java, with a set of extension
   <acronym>API</acronym>'s. Some features within the core of the
   standard driver actually use these extensions to implement Large
   Objects, etc.
  </para>
819

820 821
  <sect2>
   <title>Accessing the Extensions</title>
822

823 824 825 826 827
   <para>
    To access some of the extensions, you need to use some extra
    methods in the <classname>org.postgresql.Connection</classname>
    class. In this case, you would need to case the return value of
    <function>Driver.getConnection()</function>.  For example:
828
<programlisting>
829 830 831 832 833 834
Connection db = Driver.getConnection(url, username, password);
// ...
// later on
Fastpath fp = ((org.postgresql.Connection)db).getFastpathAPI();
</programlisting>
   </para>
835

836 837
   <sect3>
    <title>Class <classname>org.postgresql.Connection</classname></title>
838

839 840
<synopsis>
public class Connection extends Object implements Connection
841 842 843

java.lang.Object
   |
844 845
   +----org.postgresql.Connection
</synopsis>
846

847 848 849 850 851 852
    <para>
     These are the extra methods used to gain access to
     <productname>PostgreSQL</productname>'s extensions. Methods
     defined by <classname>java.sql.Connection</classname> are not
     listed.
    </para>
853

854 855
    <sect4>
     <title>Methods</title>
856

857 858 859 860 861 862 863 864 865 866
     <itemizedlist>
      <listitem>
<synopsis>
public Fastpath getFastpathAPI() throws SQLException
</synopsis>
       <para>
        This returns the Fastpath <acronym>API</acronym> for the
        current connection.  It is primarily used by the Large Object
        <acronym>API</acronym>.
       </para>
867

868 869 870 871 872 873 874
       <para>
        The best way to use this is as follows:
<programlisting>
import org.postgresql.fastpath.*;
...
Fastpath fp = ((org.postgresql.Connection)myconn).getFastpathAPI();
</programlisting>
875
        where <varname>myconn</> is an open <classname>Connection</> to <productname>PostgreSQL</productname>.
876
       </para>
877

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
       <formalpara>
        <title>Returns:</title>
        <para>
         Fastpath object allowing access to functions on the
         <productname>PostgreSQL</productname> backend.
        </para>
       </formalpara>

       <formalpara>
        <title>Throws:</title>
        <para>
         SQLException by Fastpath when initializing for first time
        </para>
       </formalpara>
      </listitem>
893

894 895 896 897 898 899 900 901
      <listitem>
       <para>
<synopsis>
public LargeObjectManager getLargeObjectAPI() throws SQLException
</synopsis>
        This returns the Large Object <acronym>API</acronym> for the
        current connection.
       </para>
902

903 904 905 906 907 908 909
       <para>
        The best way to use this is as follows:
<programlisting>
import org.postgresql.largeobject.*;
...
LargeObjectManager lo = ((org.postgresql.Connection)myconn).getLargeObjectAPI();
</programlisting>
910
        where <varname>myconn</> is an open <classname>Connection</> to
911 912
        <productname>PostgreSQL</productname>.
       </para>
913

914 915 916
       <formalpara>
        <title>Returns:</title>
        <para>
917
         <classname>LargeObject</classname> object that implements the <acronym>API</acronym>
918 919 920 921 922 923
        </para>
       </formalpara>

       <formalpara>
        <title>Throws:</title>
        <para>
924
         <classname>SQLException</classname> by <classname>LargeObject</classname> when initializing for first time
925 926 927
        </para>
       </formalpara>
      </listitem>
928

929 930 931 932 933 934
      <listitem>
       <para>
<synopsis>
public void addDataType(String type, String name)
</synopsis>
        This allows client code to add a handler for one of
935
        <productname>PostgreSQL</productname>'s more unique data types. Normally, a data type not
936 937 938
        known by the driver is returned by <literal>ResultSet.getObject()</literal> as a
        <classname>PGobject</> instance.  This method allows you to write a class
        that extends <classname>PGobject</>, and tell the driver the type name, and
939 940 941
        class name to use. The down side to this, is that you must
        call this method each time a connection is made.
       </para>
942

943 944 945
       <para>
        The best way to use this is as follows:
<programlisting>
946
 ...
947
((org.postgresql.Connection)myconn).addDataType("mytype","my.class.name");
948
 ...
949
</programlisting>
950
        where <varname>myconn</varname> is an open <classname>Connection</> to
951 952 953 954 955 956 957
        <productname>PostgreSQL</productname>.  The handling class must
        extend <classname>org.postgresql.util.PGobject</classname>.
       </para>
      </listitem>
     </itemizedlist>
    </sect4>
   </sect3>
958 959


960 961
   <sect3>
    <title>Class <classname>org.postgresql.Fastpath</classname></title>
962

963 964
<synopsis>
public class Fastpath extends Object
965 966 967

java.lang.Object
   |
968 969
   +----org.postgresql.fastpath.Fastpath
</synopsis>
970

971 972
    <para>
     <classname>Fastpath</classname> is an <acronym>API</acronym> that
973
     exists within the <application>libpq</application> C interface, and allows a client machine
974 975 976 977
     to execute a function on the database backend.  Most client code
     will not need to use this method, but it is provided because the
     Large Object <acronym>API</acronym> uses it.
    </para>
978

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    <para>
     To use, you need to import the
     <classname>org.postgresql.fastpath</classname> package, using the
     line:
<programlisting>
import org.postgresql.fastpath.*;
</programlisting>
     Then, in your code, you need to get a
     <classname>FastPath</classname> object:
<programlisting>
Fastpath fp = ((org.postgresql.Connection)conn).getFastpathAPI();
</programlisting>
     This will return an instance associated with the database
     connection that you can use to issue commands. The casing of
     <classname>Connection</classname> to
     <classname>org.postgresql.Connection</classname> is required, as
     the <function>getFastpathAPI()</function> is an extension method,
     not part of <acronym>JDBC</acronym>.  Once you have a
     <classname>Fastpath</classname> instance, you can use the
     <function>fastpath()</function> methods to execute a backend
     function.
    </para>
1001

1002 1003 1004 1005 1006 1007
    <formalpara>
     <title>See Also:</title>
     <para>
      <classname>FastpathFastpathArg</classname>, <classname>LargeObject</classname>
     </para>
    </formalpara>
1008

1009 1010
    <sect4>
     <title>Methods</title>
1011

1012 1013 1014 1015 1016 1017 1018 1019
     <itemizedlist>
      <listitem>
<synopsis>
public Object fastpath(int fnid,
                       boolean resulttype,
                       FastpathArg args[]) throws SQLException
</synopsis>
       <para>
1020
        Send a function call to the <productname>PostgreSQL</productname> backend.
1021
       </para>
1022

1023 1024 1025
       <formalpara>
        <title>Parameters:</title>
        <para>
1026 1027
                <parameter>fnid</> - Function id
                <parameter>resulttype</> - True if the result is an integer, false 
1028 1029
for
                other results
1030
                <parameter>args</> - <classname>FastpathArguments</classname> to pass to fastpath
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
        </para>
       </formalpara>

       <formalpara>
        <title>Returns:</title>
        <para>
         null if no data, Integer if an integer result, or byte[]
         otherwise
        </para>
       </formalpara>
      </listitem>
1042

1043 1044 1045 1046 1047 1048 1049
      <listitem>
<synopsis>
public Object fastpath(String name,
                       boolean resulttype,
                       FastpathArg args[]) throws SQLException
</synopsis>
       <para>
1050
        Send a function call to the <productname>PostgreSQL</productname> backend by name.
1051
       </para>
1052

1053 1054 1055
       <note>
        <para>
         The mapping for the procedure name to function id needs to
1056
         exist, usually to an earlier call to <function>addfunction()</function>. This is
1057 1058 1059 1060 1061 1062 1063 1064 1065
         the preferred method to call, as function id's can/may change
         between versions of the backend. For an example of how this
         works, refer to org.postgresql.LargeObject
        </para>
       </note>

       <formalpara>
        <title>Parameters:</title>
        <para>
1066 1067
                <parameter>name</> - Function name
                <parameter>resulttype</> - True if the result is an integer, false 
1068 1069
for
                other results
1070
                <parameter>args</> - <classname>FastpathArguments</classname> to pass to fastpath
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
        </para>
       </formalpara>

       <formalpara>
        <title>Returns:</title>
        <para>
         null if no data, Integer if an integer result, or byte[]
         otherwise
        </para>
       </formalpara>

       <formalpara>
        <title>See Also:</title>
        <para><classname>LargeObject</classname></para>
       </formalpara>
      </listitem>
1087

1088 1089 1090 1091 1092 1093 1094 1095
      <listitem>
<synopsis>          
public int getInteger(String name,
                      FastpathArg args[]) throws SQLException
</synopsis>
       <para>
        This convenience method assumes that the return value is an Integer
       </para>
1096

1097 1098 1099
       <formalpara>
        <title>Parameters:</title>
        <para>
1100 1101
                <parameter>name</parameter> - Function name
                <parameter>args</parameter> - Function arguments
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
        </para>
       </formalpara>

       <formalpara>
        <title>Returns:</title>
        <para>integer result</para>
       </formalpara>

       <formalpara>
        <title>Throws:</title>
        <para>
1113
         <classname>SQLException</classname> if a database-access error occurs or no result
1114 1115 1116
        </para>
       </formalpara>
      </listitem>
1117

1118 1119 1120 1121 1122 1123 1124 1125 1126
      <listitem>
<synopsis>
public byte[] getData(String name,
                      FastpathArg args[]) throws SQLException
</synopsis>
       <para>
        This convenience method assumes that the return value is binary
        data.
       </para>
1127

1128 1129 1130
       <formalpara>
        <title>Parameters:</title>
        <para>
1131 1132
                <parameter>name</parameter> - Function name
                <parameter>args</parameter> - Function arguments
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
        </para>
       </formalpara>

       <formalpara>
        <title>Returns:</title>
        <para>byte[] array containing result</para>
       </formalpara>

       <formalpara>
        <title>Throws:</title>
        <para>
1144
         <classname>SQLException</classname> if a database-access error occurs or no result
1145 1146 1147
        </para>
       </formalpara>
      </listitem>
1148

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
      <listitem>
<synopsis>
public void addFunction(String name,
                        int fnid)
</synopsis>
       <para>
        This adds a function to our look-up table.  User code should
        use the <function>addFunctions</function> method, which is based upon a query,
        rather than hard coding the oid. The oid for a function is not
        guaranteed to remain static, even on different servers of the
        same version.
       </para>
      </listitem>
1162

1163 1164 1165 1166 1167 1168 1169 1170 1171
      <listitem>
<synopsis>
public void addFunctions(ResultSet rs) throws SQLException
</synopsis>
       <para>
        This takes a <classname>ResultSet</classname> containing two columns. Column 1
        contains the function name, Column 2 the oid.  It reads the
        entire <classname>ResultSet</classname>, loading the values into the function table.
       </para>
1172

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
       <important>
        <para>
         Remember to <function>close()</function> the
         <classname>ResultSet</classname> after calling this!
        </para>
       </important>

       <note>
        <title>Implementation note about function name look-ups</title>

        <para>
1184
         <productname>PostgreSQL</productname> stores the function id's and their corresponding
1185
         names in the <classname>pg_proc</> table. To speed things up locally,
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
         instead of querying each function from that table when
         required, a <classname>Hashtable</classname> is used. Also, only the function's
         required are entered into this table, keeping connection
         times as fast as possible.
        </para>

        <para>
         The <classname>org.postgresql.LargeObject</classname> class
         performs a query upon its start-up, and passes the returned
         <classname>ResultSet</classname> to the
         <function>addFunctions()</function> method here.  Once this
         has been done, the Large Object <acronym>API</acronym> refers
         to the functions by name.
        </para>

        <para>
         Do not think that manually converting them to the oid's will
1203
         work. OK, they will for now, but they can change during
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
         development (there was some discussion about this for V7.0),
         so this is implemented to prevent any unwarranted headaches
         in the future.
        </para>
       </note>

       <formalpara>
        <title>See Also:</title>
        <para>
         <classname>LargeObjectManager</classname>
        </para>
       </formalpara>
      </listitem>
1217

1218 1219 1220 1221 1222 1223
      <listitem>
<synopsis>
public int getID(String name) throws SQLException
</synopsis>
       <para>
        This returns the function id associated by its name If
1224 1225
        <function>addFunction()</function> or <function>addFunctions()</function> have not been called for this
        name, then an <classname>SQLException</classname> is thrown.
1226 1227 1228 1229 1230
       </para>
      </listitem>
     </itemizedlist>
    </sect4>
   </sect3>
1231 1232


1233 1234
   <sect3>
    <title>Class <classname>org.postgresql.fastpath.FastpathArg</classname></title>
1235

1236 1237
<synopsis>
public class FastpathArg extends Object
1238 1239 1240

java.lang.Object
   |
1241 1242
   +----org.postgresql.fastpath.FastpathArg
</synopsis>
1243

1244 1245 1246 1247 1248
    <para>
     Each fastpath call requires an array of arguments, the number and
     type dependent on the function being called.  This class
     implements methods needed to provide this capability.
    </para>
1249

1250 1251 1252 1253
    <para>
     For an example on how to use this, refer to the
     <classname>org.postgresql.LargeObject</classname> package.
    </para>
1254

1255 1256 1257 1258 1259 1260
    <formalpara>
     <title>See Also:</title>
     <para>
      <classname>Fastpath</classname>, <classname>LargeObjectManager</classname>, <classname>LargeObject</classname>
     </para>
    </formalpara>
1261

1262 1263
    <sect4>
     <title>Constructors</title>
1264

1265 1266 1267 1268 1269 1270 1271 1272
     <itemizedlist>
      <listitem>
<synopsis>
public FastpathArg(int value)
</synopsis>
       <para>
        Constructs an argument that consists of an integer value
       </para>
1273

1274 1275 1276 1277 1278 1279 1280
       <formalpara>
        <title>Parameters:</title>
        <para>
         value - int value to set
        </para>
       </formalpara>
      </listitem>
1281

1282 1283 1284 1285 1286 1287 1288
      <listitem>
<synopsis>
public FastpathArg(byte bytes[])
</synopsis>
       <para>
        Constructs an argument that consists of an array of bytes
       </para>
1289

1290 1291 1292 1293 1294 1295 1296
       <formalpara>
        <title>Parameters:</title>
        <para>
         bytes - array to store
        </para>
       </formalpara>
      </listitem>
1297

1298 1299 1300 1301 1302 1303 1304 1305 1306
      <listitem>
<synopsis>
public FastpathArg(byte buf[],
                   int off,
                   int len)
</synopsis>
       <para>
        Constructs an argument that consists of part of a byte array
       </para>
1307

1308 1309 1310 1311 1312
       <formalpara>
        <title>Parameters:</title>
        <para>
         <variablelist>
          <varlistentry>
1313
           <term><parameter>buf</></term>
1314 1315 1316 1317 1318 1319
           <listitem>
            <simpara>source array</simpara>
           </listitem>
          </varlistentry>

          <varlistentry>
1320
           <term><parameter>off</parameter></term>
1321 1322 1323 1324 1325 1326
           <listitem>
            <simpara>offset within array</simpara>
           </listitem>
          </varlistentry>

          <varlistentry>
1327
           <term><parameter>len</parameter></term>
1328 1329 1330 1331 1332 1333 1334 1335
           <listitem>
            <simpara>length of data to include</simpara>
           </listitem>
          </varlistentry>
         </variablelist>
        </para>
       </formalpara>
      </listitem>
1336

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
      <listitem>
<synopsis>
public FastpathArg(String s)
</synopsis>
       <para>
        Constructs an argument that consists of a String.
       </para>
      </listitem>
     </itemizedlist>
    </sect4>
   </sect3>
  </sect2>
1349 1350


1351 1352
  <sect2>
   <title>Geometric Data Types</title>
1353

1354 1355 1356 1357 1358 1359 1360 1361
   <para>
    <productname>PostgreSQL</productname> has a set of data types that
    can store geometric features into a table. These include single
    points, lines, and polygons.  We support these types in Java with
    the org.postgresql.geometric package.  It contains classes that
    extend the org.postgresql.util.PGobject class.  Refer to that
    class for details on how to implement your own data type handlers.
   </para>
1362

1363 1364
<programlisting>
Class org.postgresql.geometric.PGbox
1365 1366 1367

java.lang.Object
   |
1368
   +----org.postgresql.util.PGobject
1369
           |
1370
           +----org.postgresql.geometric.PGbox
1371 1372 1373 1374

   public class PGbox extends PGobject implements Serializable, 
Cloneable

1375
   This represents the box data type within <productname>PostgreSQL</productname>.
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

Variables

 public PGpoint point[]

          These are the two corner points of the box.

Constructors

 public PGbox(double x1,
              double y1,
              double x2,
              double y2)

        Parameters:
                x1 - first x coordinate
                y1 - first y coordinate
                x2 - second x coordinate
                y2 - second y coordinate

 public PGbox(PGpoint p1,
              PGpoint p2)

        Parameters:
                p1 - first point
                p2 - second point

 public PGbox(String s) throws SQLException
                            
        Parameters:
1406
                s - Box definition in <productname>PostgreSQL</productname> syntax
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419

        Throws: SQLException
                if definition is invalid
                
 public PGbox()

          Required constructor
              
Methods

 public void setValue(String value) throws SQLException
                
          This method sets the value of this object. It should be 
1420
overridden, but still called by subclasses.
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
                            
        Parameters:
                value - a string representation of the value of the 
object
        Throws: SQLException
                thrown if value is invalid for this type

        Overrides:
                setValue in class PGobject

 public boolean equals(Object obj)

        Parameters:
                obj - Object to compare with
                
        Returns:
                true if the two boxes are identical
          
        Overrides:
                equals in class PGobject

 public Object clone()
        
1444
          This must be overridden to allow the object to be cloned
1445 1446 1447 1448 1449 1450 1451

        Overrides:
                clone in class PGobject
   
 public String getValue()
        
        Returns:
1452
                the PGbox in the syntax expected by <productname>PostgreSQL</productname>
1453 1454 1455 1456

        Overrides:
                getValue in class PGobject

1457 1458
<!-- **************************************************************** -->
Class org.postgresql.geometric.PGcircle
1459 1460 1461

java.lang.Object
   |
1462
   +----org.postgresql.util.PGobject
1463
           |
1464
           +----org.postgresql.geometric.PGcircle
1465 1466 1467 1468
        
   public class PGcircle extends PGobject implements Serializable, 
Cloneable
               
1469
   This represents <productname>PostgreSQL</productname>'s circle data type, consisting of a point 
1470 1471 1472 1473 1474 1475
and a radius

Variables

 public PGpoint center
           
1476
          This is the center point
1477
 
1478
 double radius
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
           
          This is the radius
   
Constructors   

 public PGcircle(double x,
                 double y,
                 double r)
          
        Parameters:
1489 1490
               x - coordinate of center
                y - coordinate of center
1491 1492 1493 1494 1495 1496
                r - radius of circle

 public PGcircle(PGpoint c,
                 double r)
          
        Parameters:
1497
                c - PGpoint describing the circle's center
1498 1499 1500 1501 1502
                r - radius of circle

 public PGcircle(String s) throws SQLException

        Parameters:
1503
                s - definition of the circle in <productname>PostgreSQL</productname>'s syntax.
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516

        Throws: SQLException
                on conversion failure

 public PGcircle()

          This constructor is used by the driver.
            
Methods   

 public void setValue(String s) throws SQLException

        Parameters:
1517
                s - definition of the circle in <productname>PostgreSQL</productname>'s syntax.
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530

        Throws: SQLException
                on conversion failure

        Overrides:
                setValue in class PGobject

 public boolean equals(Object obj)

        Parameters:
                obj - Object to compare with
            
        Returns:
1531
                true if the two circles are identical
1532 1533 1534 1535 1536 1537

        Overrides:
                equals in class PGobject

 public Object clone()

1538
          This must be overridden to allow the object to be cloned
1539 1540 1541 1542 1543 1544 1545

        Overrides:
                clone in class PGobject

 public String getValue()

        Returns:
1546
                the PGcircle in the syntax expected by <productname>PostgreSQL</productname>
1547 1548 1549 1550
        
        Overrides:
                getValue in class PGobject

1551 1552
<!-- **************************************************************** -->
Class org.postgresql.geometric.PGline
1553 1554 1555

java.lang.Object
   |
1556
   +----org.postgresql.util.PGobject
1557
           |
1558
           +----org.postgresql.geometric.PGline
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595

   public class PGline extends PGobject implements Serializable, 
Cloneable

   This implements a line consisting of two points. Currently line is 
not yet implemented in the backend, but this class ensures that when 
it's done were ready for it.

Variables
   
 public PGpoint point[]
     
          These are the two points.

Constructors

 public PGline(double x1,
               double y1,
               double x2,
               double y2)

        Parameters:
                x1 - coordinate for first point
                y1 - coordinate for first point
                x2 - coordinate for second point
                y2 - coordinate for second point

 public PGline(PGpoint p1,
               PGpoint p2)
     
        Parameters:
                p1 - first point
                p2 - second point

 public PGline(String s) throws SQLException
               
        Parameters:
1596
                s - definition of the line in <productname>PostgreSQL</productname>'s syntax.
1597 1598 1599 1600 1601 1602

        Throws: SQLException
                on conversion failure

 public PGline()

1603
          required by the driver
1604 1605 1606 1607 1608 1609
               
Methods

 public void setValue(String s) throws SQLException

        Parameters:
1610
                s - Definition of the line segment in <productname>PostgreSQL</productname>'s 
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
syntax

        Throws: SQLException
                on conversion failure

        Overrides:
                setValue in class PGobject
                
 public boolean equals(Object obj)

        Parameters:
                obj - Object to compare with
               
        Returns:
1625
                true if the two lines are identical
1626 1627 1628 1629 1630 1631
   
        Overrides:
                equals in class PGobject

 public Object clone()
        
1632
          This must be overridden to allow the object to be cloned
1633 1634 1635 1636 1637 1638 1639

        Overrides:
                clone in class PGobject

 public String getValue()
   
        Returns:
1640
                the PGline in the syntax expected by <productname>PostgreSQL</productname>
1641 1642 1643 1644
        
        Overrides:
                getValue in class PGobject

1645 1646
<!-- **************************************************************** -->
Class org.postgresql.geometric.PGlseg
1647 1648 1649
             
java.lang.Object
   |
1650
   +----org.postgresql.util.PGobject
1651
           |
1652
           +----org.postgresql.geometric.PGlseg
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
          
   public class PGlseg extends PGobject implements Serializable, 
Cloneable
 
   This implements a lseg (line segment) consisting of two points

Variables

 public PGpoint point[]
           
          These are the two points.

Constructors
   
 public PGlseg(double x1,
               double y1,
               double x2,
               double y2)
     
        Parameters:

                x1 - coordinate for first point
                y1 - coordinate for first point
                x2 - coordinate for second point
                y2 - coordinate for second point

 public PGlseg(PGpoint p1,
               PGpoint p2)
           
        Parameters:
                p1 - first point
                p2 - second point
   
 public PGlseg(String s) throws SQLException

        Parameters:
1689
                s - Definition of the line segment in <productname>PostgreSQL</productname>'s syntax.
1690 1691 1692 1693 1694 1695

        Throws: SQLException
                on conversion failure

 public PGlseg()

1696
          required by the driver
1697 1698 1699 1700 1701 1702
               
Methods    
   
 public void setValue(String s) throws SQLException
   
        Parameters:
1703
                s - Definition of the line segment in <productname>PostgreSQL</productname>'s 
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
syntax

        Throws: SQLException
                on conversion failure
     
        Overrides:
                setValue in class PGobject
                
 public boolean equals(Object obj)

        Parameters:
                obj - Object to compare with
               
        Returns:
1718
                true if the two line segments are identical
1719 1720 1721 1722 1723 1724
   
        Overrides:
                equals in class PGobject
   
 public Object clone()

1725
          This must be overridden to allow the object to be cloned
1726 1727 1728 1729 1730 1731 1732

        Overrides:
               clone in class PGobject

 public String getValue()

        Returns:
1733
                the PGlseg in the syntax expected by <productname>PostgreSQL</productname>
1734 1735 1736 1737
        
        Overrides:
                getValue in class PGobject

1738 1739
<!-- **************************************************************** -->
Class org.postgresql.geometric.PGpath
1740 1741 1742
                                
java.lang.Object
   |
1743
   +----org.postgresql.util.PGobject
1744
           |
1745
           +----org.postgresql.geometric.PGpath
1746 1747 1748 1749
          
   public class PGpath extends PGobject implements Serializable, 
Cloneable
               
1750
   This implements a path (a multiply segmented line, which may be 
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
closed)
           
Variables

 public boolean open
               
          True if the path is open, false if closed

 public PGpoint points[]

          The points defining this path

Constructors   

 public PGpath(PGpoint points[],
               boolean open)
          
        Parameters:
                points - the PGpoints that define the path
                open - True if the path is open, false if closed

 public PGpath()

          Required by the driver

 public PGpath(String s) throws SQLException

        Parameters:
1779
                s - definition of the path in <productname>PostgreSQL</productname>'s syntax.
1780 1781 1782 1783 1784 1785 1786 1787 1788

        Throws: SQLException
                on conversion failure

Methods

 public void setValue(String s) throws SQLException
   
        Parameters:
1789
                s - Definition of the path in <productname>PostgreSQL</productname>'s syntax
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
           
        Throws: SQLException
                on conversion failure

        Overrides:
                setValue in class PGobject

 public boolean equals(Object obj)

        Parameters:
                obj - Object to compare with

        Returns:
1803
                true if the two pathes are identical
1804 1805 1806 1807 1808 1809

        Overrides:
                equals in class PGobject

 public Object clone()

1810
          This must be overridden to allow the object to be cloned
1811 1812 1813 1814 1815 1816

        Overrides:
                clone in class PGobject

 public String getValue()

1817
          This returns the path in the syntax expected by 
1818
<productname>PostgreSQL</productname>
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

        Overrides:
                getValue in class PGobject

 public boolean isOpen()

     This returns true if the path is open

 public boolean isClosed()

     This returns true if the path is closed

 public void closePath()

     Marks the path as closed

 public void openPath()

     Marks the path as open

1839 1840
<!-- **************************************************************** -->
Class org.postgresql.geometric.PGpoint
1841 1842 1843
                                
java.lang.Object
   |
1844
   +----org.postgresql.util.PGobject
1845
           |
1846
           +----org.postgresql.geometric.PGpoint
1847 1848 1849 1850 1851 1852 1853
          
   public class PGpoint extends PGobject implements Serializable, 
Cloneable

   This implements a version of java.awt.Point, except it uses double 
to represent the coordinates.

1854
   It maps to the point data type in <productname>PostgreSQL</productname>.
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877

Variables

 public double x

          The X coordinate of the point

 public double y

          The Y coordinate of the point

Constructors

 public PGpoint(double x,
                double y)

        Parameters:
                x - coordinate
                y - coordinate

 public PGpoint(String value) throws SQLException
     
          This is called mainly from the other geometric types, when a 
1878
point is embedded within their definition.
1879 1880
             
        Parameters:
1881
                value - Definition of this point in <productname>PostgreSQL</productname>'s 
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
syntax
   
 public PGpoint()
          
          Required by the driver

Methods

 public void setValue(String s) throws SQLException

        Parameters:
1893
                s - Definition of this point in <productname>PostgreSQL</productname>'s syntax
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906

        Throws: SQLException
                on conversion failure

        Overrides:
                setValue in class PGobject
          
 public boolean equals(Object obj)

        Parameters:
                obj - Object to compare with

        Returns:
1907
                true if the two points are identical
1908 1909 1910 1911 1912 1913

        Overrides:
                equals in class PGobject

 public Object clone()
                
1914
          This must be overridden to allow the object to be cloned
1915 1916 1917 1918 1919 1920 1921

        Overrides:
                clone in class PGobject
          
 public String getValue()       
    
        Returns:
1922
                the PGpoint in the syntax expected by <productname>PostgreSQL</productname>
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986

        Overrides:
                getValue in class PGobject
          
 public void translate(int x,
                       int y)

          Translate the point with the supplied amount.

        Parameters:
                x - integer amount to add on the x axis
                y - integer amount to add on the y axis

 public void translate(double x,
                       double y)
          
          Translate the point with the supplied amount.
 
        Parameters:
                x - double amount to add on the x axis
                y - double amount to add on the y axis

 public void move(int x,
                  int y)
                
          Moves the point to the supplied coordinates.

        Parameters:
                x - integer coordinate
                y - integer coordinate

public void move(double x,
                  double y)
          
          Moves the point to the supplied coordinates.

        Parameters:
                x - double coordinate
                y - double coordinate

 public void setLocation(int x,
                         int y)

          Moves the point to the supplied coordinates. refer to
          java.awt.Point for description of this

        Parameters:
                x - integer coordinate
                y - integer coordinate

        See Also:
                Point

 public void setLocation(Point p)

          Moves the point to the supplied java.awt.Point refer to
          java.awt.Point for description of this

        Parameters:
                p - Point to move to

        See Also:
                Point

1987 1988
<!-- **************************************************************** -->
Class org.postgresql.geometric.PGpolygon
1989 1990 1991
                                
java.lang.Object
   |
1992
   +----org.postgresql.util.PGobject
1993
           |
1994
           +----org.postgresql.geometric.PGpolygon
1995 1996 1997 1998

   public class PGpolygon extends PGobject implements Serializable, 
Cloneable
               
1999
   This implements the polygon data type within <productname>PostgreSQL</productname>.
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018

Variables

 public PGpoint points[]

          The points defining the polygon
                                
Constructors

 public PGpolygon(PGpoint points[])

          Creates a polygon using an array of PGpoints

        Parameters:
                points - the points defining the polygon

 public PGpolygon(String s) throws SQLException
                 
        Parameters:
2019
                s - definition of the polygon in <productname>PostgreSQL</productname>'s syntax.
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032

        Throws: SQLException
                on conversion failure

 public PGpolygon()

          Required by the driver

Methods

 public void setValue(String s) throws SQLException

        Parameters:
2033
                s - Definition of the polygon in <productname>PostgreSQL</productname>'s syntax
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046

        Throws: SQLException
                on conversion failure

        Overrides:
                setValue in class PGobject

 public boolean equals(Object obj)
     
        Parameters:
                obj - Object to compare with
                                
        Returns:
2047
                true if the two polygons are identical
2048 2049 2050 2051 2052 2053

        Overrides:
                equals in class PGobject

 public Object clone()
        
2054
          This must be overridden to allow the object to be cloned
2055 2056 2057 2058 2059 2060 2061

        Overrides:
                clone in class PGobject
                 
 public String getValue()

        Returns:
2062
                the PGpolygon in the syntax expected by <productname>PostgreSQL</productname>
2063 2064 2065

        Overrides:
                getValue in class PGobject
2066 2067
</programlisting>
  </sect2>
2068 2069


2070 2071
  <sect2>
   <title>Large Objects</title>
2072

2073 2074 2075
   <para>
    Large objects are supported in the standard
    <acronym>JDBC</acronym> specification.  However, that interface is
2076
    limited, and the <acronym>API</acronym> provided by <productname>PostgreSQL</productname> allows for random
2077 2078
    access to the objects contents, as if it was a local file.
   </para>
2079

2080
   <para>
2081
    The org.postgresql.largeobject package provides to Java the <application>libpq</application>
2082 2083 2084 2085 2086
    C interface's large object <acronym>API</acronym>. It consists of
    two classes, <classname>LargeObjectManager</classname>, which deals with creating,
    opening and deleting large objects, and <classname>LargeObject</classname> which deals
    with an individual object.
   </para>
2087

2088 2089
   <sect3>
    <title>Class <classname>org.postgresql.largeobject.LargeObject</classname></title>
2090

2091
<synopsis>
2092 2093
public class LargeObject extends Object

2094 2095 2096 2097
java.lang.Object
   |
   +----org.postgresql.largeobject.LargeObject
</synopsis>
2098

2099 2100 2101 2102
    <para>
     This class implements the large object interface to
     <productname>PostgreSQL</productname>.
    </para>
2103

2104 2105 2106 2107 2108
    <para>
     It provides the basic methods required to run the interface, plus
     a pair of methods that provide <classname>InputStream</classname> and <classname>OutputStream</classname>
     classes for this object.
    </para>
2109

2110
    <para>
B
Barry Lind 已提交
2111 2112
     Normally, client code would use the methods in 
     <classname>BLOB</classname> to access large objects.
2113
    </para>
2114

2115
    <para>
B
Barry Lind 已提交
2116 2117
     However, sometimes lower level access to Large Objects is
     required, that is not supported by the <acronym>JDBC</acronym>
2118 2119
     specification.
    </para>
2120

2121 2122 2123 2124
    <para>
     Refer to org.postgresql.largeobject.LargeObjectManager on how to
     gain access to a Large Object, or how to create one.
    </para>
2125

2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159
    <formalpara>
     <title>See Also:</title>
     <para><classname>LargeObjectManager</classname></para>
    </formalpara>

    <sect4>
     <title>Variables</title>

     <variablelist>
      <varlistentry>
       <term>public static final int SEEK_SET</term>
       <listitem>
        <para>Indicates a seek from the beginning of a file</para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>public static final int SEEK_CUR</term>
       <listitem>
        <para>Indicates a seek from the current position</para>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>public static final int SEEK_END</term>
       <listitem>
        <para>Indicates a seek from the end of a file</para>
       </listitem>
      </varlistentry>
     </variablelist>
    </sect4>

    <sect4>
     <title>Methods</title>
2160

2161 2162 2163 2164 2165 2166 2167 2168 2169
     <itemizedlist>
      <listitem>
<synopsis>
public int getOID()
</synopsis>
       <para>
        Returns the OID of this <classname>LargeObject</classname>
       </para>
      </listitem>
2170

2171 2172 2173 2174 2175 2176 2177 2178 2179
      <listitem>
<synopsis>
public void close() throws SQLException
</synopsis>
       <para>
        This method closes the object. You must not call methods in
        this object after this is called.
       </para>
      </listitem>
2180

2181 2182 2183 2184 2185 2186 2187 2188
      <listitem>
<synopsis>
public byte[] read(int len) throws SQLException
</synopsis>
       <para>
        Reads some data from the object, and return as a byte[] array
       </para>
      </listitem>
2189

2190 2191
      <listitem>
<synopsis>
2192
public int read(byte buf[],
2193 2194 2195 2196 2197 2198
                 int off,
                 int len) throws SQLException
</synopsis>
       <para>
        Reads some data from the object into an existing array
       </para>
2199

2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
       <formalpara>
        <title>Parameters:</title>
        <para>
         <variablelist>
          <varlistentry>
           <term>buf</term>
           <listitem>
            <simpara>destination array</simpara>
           </listitem>
          </varlistentry>

          <varlistentry>
           <term>off</term>
           <listitem>
            <simpara>offset within array</simpara>
           </listitem>
          </varlistentry>

          <varlistentry>
           <term>len</term>
           <listitem>
            <simpara>number of bytes to read</simpara>
           </listitem>
          </varlistentry>
         </variablelist>
        </para>
       </formalpara>
      </listitem>
2228

2229 2230 2231 2232 2233 2234 2235 2236
      <listitem>
<synopsis>
public void write(byte buf[]) throws SQLException
</synopsis>
       <para>
        Writes an array to the object
       </para>
      </listitem>
2237

2238 2239 2240
      <listitem>
<synopsis>
public void write(byte buf[],
2241 2242
                  int off,
                  int len) throws SQLException
2243 2244 2245 2246
</synopsis>
       <para>
        Writes some data from an array to the object
       </para>
2247

2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
       <formalpara>
        <title>Parameters:</title>
        <para>
         <variablelist>
          <varlistentry>
           <term>buf</term>
           <listitem>
            <simpara>destination array</simpara>
           </listitem>
          </varlistentry>

          <varlistentry>
           <term>off</term>
           <listitem>
            <simpara>offset within array</simpara>
           </listitem>
          </varlistentry>

          <varlistentry>
           <term>len</term>
           <listitem>
            <simpara>number of bytes to write</simpara>
           </listitem>
          </varlistentry>
         </variablelist>
        </para>
       </formalpara>
      </listitem>
2276

2277
<!--
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
 public void seek(int pos,
                  int ref) throws SQLException

          Sets the current position within the object.

          This is similar to the fseek() call in the standard C 
library.It allows you to have random access to the large object.

        Parameters:
                pos - position within object
                ref - Either SEEK_SET, SEEK_CUR or SEEK_END
        Throws: SQLException
                if a database-access error occurs.

 public void seek(int pos) throws SQLException

          Sets the current position within the object.

          This is similar to the fseek() call in the standard C 
library.It allows you to have random access to the large object.

        Parameters:
                pos - position within object from begining

        Throws: SQLException
                if a database-access error occurs.

 public int tell() throws SQLException

        Returns:
                the current position within the object

        Throws: SQLException
                if a database-access error occurs.

 public int size() throws SQLException

          This method is inefficient, as the only way to find out the 
size of the object is to seek to the end, record the current position, 
then return to the original position.

          A better method will be found in the future.

        Returns:
                the size of the large object

        Throws: SQLException
                if a database-access error occurs.

 public InputStream getInputStream() throws SQLException

          Returns an InputStream from this object.

          This InputStream can then be used in any method that 
requires an InputStream.

        Throws: SQLException
                if a database-access error occurs.

 public OutputStream getOutputStream() throws SQLException

          Returns an OutputStream to this object

          This OutputStream can then be used in any method that 
requires an OutputStream.

        Throws: SQLException
                if a database-access error occurs.
2346 2347 2348 2349
-->
     </itemizedlist>
    </sect4>
   </sect3>
2350 2351


2352 2353
   <sect3>
    <title>Class <classname>org.postgresql.largeobject.LargeObjectManager</classname></title>
2354

2355 2356
<synopsis>                                
public class LargeObjectManager extends Object
2357

2358 2359 2360 2361
java.lang.Object
   |
   +----org.postgresql.largeobject.LargeObjectManager
</synopsis>
2362

2363 2364 2365 2366 2367 2368 2369 2370
    <para>
     This class implements the large object interface to
     <productname>PostgreSQL</productname>.  It provides methods that
     allow client code to create, open and delete large objects from
     the database. When opening an object, an instance of
     <classname>org.postgresql.largeobject.LargeObject</classname> is
     returned, and its methods then allow access to the object.
    </para>
2371

2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
    <para>
     This class can only be created by org.postgresql.Connection.  To
     get access to this class, use the following segment of code:
<programlisting>
import org.postgresql.largeobject.*;
Connection  conn;
LargeObjectManager lobj;
// ... code that opens a connection ...
lobj = ((org.postgresql.Connection)myconn).getLargeObjectAPI();
</programlisting>
    </para>
2383

2384
    <para>
B
Barry Lind 已提交
2385 2386 2387
     Normally, client code would use the <classname>BLOB</classname>
     methods to access large objects.  However, sometimes
     lower level access to Large Objects is required, that is not
2388 2389
     supported by the <acronym>JDBC</acronym> specification.
    </para>
2390

2391 2392 2393 2394
    <para>
     Refer to org.postgresql.largeobject.LargeObject on how to
     manipulate the contents of a Large Object.
    </para>
2395

2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
    <sect4>
     <title>Variables</title>

     <variablelist>
      <varlistentry>
       <term>public static final int WRITE</term>
       <listitem>
        <simpara>This mode indicates we want to write to an object.</simpara>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>public static final int READ</term>
       <listitem>
        <simpara>This mode indicates we want to read an object.</simpara>
       </listitem>
      </varlistentry>

      <varlistentry>
       <term>public static final int READWRITE</term>
       <listitem>
        <simpara>This mode is the default. It indicates we want read and write access to a large object.</simpara>
       </listitem>
      </varlistentry>
     </variablelist>
    </sect4>

    <sect4>
     <title>Methods</title>
2425

2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
     <itemizedlist>
      <listitem>
<synopsis>
public LargeObject open(int oid) throws SQLException
</synopsis>
       <para>
        This opens an existing large object, based on its OID. This
        method assumes that READ and WRITE access is required (the
        default).
       </para>
      </listitem>
2437

2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
      <listitem>
<synopsis>
public LargeObject open(int oid,
                        int mode) throws SQLException
</synopsis>
       <para>
        This opens an existing large object, based on its OID, and
        allows setting the access mode.
       </para>
      </listitem>
2448

2449 2450 2451 2452 2453 2454 2455 2456 2457
      <listitem>
<synopsis>
public int create() throws SQLException
</synopsis>
       <para>
        This creates a large object, returning its OID.
        It defaults to READWRITE for the new object's attributes.
       </para>
      </listitem>
2458

2459 2460 2461 2462 2463 2464 2465 2466 2467
      <listitem>
<synopsis>
public int create(int mode) throws SQLException
</synopsis>
       <para>
        This creates a large object, returning its OID, and sets the
        access mode.
       </para>
      </listitem>
2468

2469 2470 2471 2472 2473 2474 2475 2476
      <listitem>
<synopsis>
public void delete(int oid) throws SQLException
</synopsis>
       <para>          
        This deletes a large object.
       </para>
      </listitem>
2477

2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
      <listitem>
<synopsis>
public void unlink(int oid) throws SQLException
</synopsis>
       <para>
        This deletes a large object.  It is identical to the delete
        method, and is supplied as the C <acronym>API</acronym> uses
        <quote>unlink</quote>.
       </para>
      </listitem>
     </itemizedlist>
    </sect4>
   </sect3>
  </sect2>
2492

2493
 </sect1>
B
Barry Lind 已提交
2494

2495

2496 2497
 <sect1 id="jdbc-thread">
  <title>Using the driver in a multi-threaded or a servlet environment</title>
2498

2499 2500 2501 2502 2503 2504 2505
  <para>
   A problem with many <acronym>JDBC</acronym> drivers is that only
   one thread can use a <classname>Connection</classname> at any one
   time -- otherwise a thread could send a query while another one is
   receiving results, and this would be a bad thing for the database
   engine.
  </para>
2506

2507
  <para>
B
Bruce Momjian 已提交
2508
   The <productname>PostgreSQL</productname> <acronym>JDBC</acronym> Driver
B
Barry Lind 已提交
2509
   is thread safe.
2510 2511 2512 2513
   Consequently, if your application uses multiple threads then you do
   not have to worry about complex algorithms to ensure that only one
   uses the database at any time.
  </para>
2514

2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
  <para>
   If a thread attempts to use the connection while another one is
   using it, it will wait until the other thread has finished its
   current operation.  If it is a regular <acronym>SQL</acronym>
   statement, then the operation consists of sending the statement and
   retrieving any <classname>ResultSet</classname> (in full).  If it
   is a <classname>Fastpath</classname> call (e.g., reading a block
   from a <classname>LargeObject</classname>) then it is the time to
   send and retrieve that block.
  </para>
2525

2526 2527 2528 2529 2530 2531 2532
  <para>
   This is fine for applications and applets but can cause a
   performance problem with servlets.  With servlets you can have a
   heavy load on the connection. If you have several threads
   performing queries then each but one will pause, which may not be what
   you are after.
  </para>
2533

2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
  <para>
   To solve this, you would be advised to create a pool of
   connections.  When ever a thread needs to use the database, it asks
   a manager class for a <classname>Connection</classname>. The
   manager hands a free connection to the thread and marks it as
   busy. If a free connection is not available, it opens one.  Once
   the thread has finished with it, it returns it to the manager who
   can then either close it or add it to the pool. The manager would
   also check that the connection is still alive and remove it from
   the pool if it is dead.
  </para>
2545

2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
  <para>
   So, with servlets, it is up to you to use either a single
   connection, or a pool. The plus side for a pool is that threads
   will not be hit by the bottle neck caused by a single network
   connection. The down side is that it increases the load on the
   server, as a backend process is created for each
   <classname>Connection</classname>.  It is up to you and your
   applications requirements.
  </para>
 </sect1>
2556 2557


2558 2559
 <sect1 id="jdbc-reading">
  <title>Further Reading</title>
2560

2561 2562 2563 2564 2565 2566 2567 2568
  <para>
   If you have not yet read it, I'd advise you read the
   <acronym>JDBC</acronym> <acronym>API</acronym> Documentation
   (supplied with Sun's <acronym>JDK</acronym>), and the
   <acronym>JDBC</acronym> Specification.  Both are available from
   <ulink
   url="http://java.sun.com/products/jdbc/index.html">http://java.sun.com/products/jdbc/index.html</ulink>.
  </para>
2569

2570 2571 2572 2573 2574 2575 2576
  <para>
   <ulink
   url="http://jdbc.postgresql.org">http://jdbc.postgresql.org</ulink>
   contains updated information not included in this document, and
   also includes precompiled drivers.
  </para>
 </sect1>
2577
</chapter>
2578 2579 2580

<!-- Keep this comment at the end of the file
Local variables:
2581
mode:sgml
2582 2583 2584 2585 2586 2587 2588 2589 2590
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
2591
sgml-local-catalogs:("/usr/lib/sgml/catalog")
2592 2593
sgml-local-ecat-files:nil
End:
2594
-->