<!--
-$Header: /cvsroot/pgsql/doc/src/sgml/Attic/jdbc.sgml,v 1.31 2001/11/26 19:07:11 momjian Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/Attic/jdbc.sgml,v 1.32 2001/11/27 01:20:17 barry Exp $
-->
<chapter id="jdbc">
Any time you want to issue <acronym>SQL</acronym> statements to
the database, you require a <classname>Statement</classname> or
<classname>PreparedStatement</classname> instance. Once you have
- a <classname>Statement</classname> or <classname>PreparedStatement
- </classname>, you
- can use issue a
+ a <classname>Statement</classname> or
+ <classname>PreparedStatement</classname>, you can use issue a
query. This will return a <classname>ResultSet</classname>
instance, which contains the entire result. <xref
linkend="jdbc-query-example"> illustrates this process.
</para>
<para>
- This example will issues the same query as before using
+ This example will issue the same query as before using
a <classname>PreparedStatement</classname>
and a bind value in the query.
<programlisting>
<para>
The following must be considered when using the
- <classname>Statement</classname> interface:
+ <classname>Statement</classname> or
+ <classname>PreparedStatement</classname> interface:
<itemizedlist>
<listitem>
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
- <classname>Statement</classname> at a given time.
+ <classname>Statement</classname> or
+ <classname>PreparedStatement</classname> at a given time.
</para>
</listitem>
<listitem>
<para>
When you are done using the <classname>Statement</classname>
- you should close the <classname>Statement</classname>.
+ or <classname>PreparedStatement</classname>
+ you should close it.
</para>
</listitem>
</itemizedlist>
update, or delete statement.
</para>
+ <example id="jdbc-delete-example">
+ <title>Simple Delete Example</title>
<para>
This example will issue a simple delete and print out the number
of rows deleted.
st.close();
</programlisting>
</para>
+ </example>
</sect1>
<sect1 id="jdbc-ddl">
however it doesn't return a result.
</para>
+ <example id="jdbc-drop-table-example">
+ <title>Drop Table Example</title>
<para>
This example will drop a table.
<programlisting>
st.close();
</programlisting>
</para>
+ </example>
</sect1>
<sect1 id="jdbc-binary-data">
<title>Storing Binary Data</title>
<para>
- <application>PostgreSQL</application> provides two distinct way to
+ <application>PostgreSQL</application> provides two distinct ways to
store binary data. Binary data can be stored in a table using
<application>PostgreSQL's</application> binary datatype
<type>bytea</type>, or by using the <firstterm>Large Object</firstterm>
feature which stores the binary data in a separate table in a special
- format, and refers to from your own tables by an <type>OID</type> value.
+ format, and refers to that table by storing a value of type
+ <type>OID</type> in your table.
</para>
<para>
</para>
<para>
- 7.2 is the first release that the <acronym>JDBC</acronym> Driver
- supports the <type>bytea</type> datatype. The introduction of
+ 7.2 is the first release of the <acronym>JDBC</acronym> Driver
+ that supports the <type>bytea</type> datatype. The introduction of
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>,
</para>
<para>
- Here you can see how the Large Object is retrieved as an
+ Here the binary data was retrieved as an
<classname>byte[]</classname>. You could have used a
<classname>InputStream</classname> object instead.
</para>
<programlisting>
// All LargeObject API calls must be within a transaction
conn.setAutoCommit(false);
+
+// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.Connection)conn).getLargeObjectAPI();
//create a new large object
<programlisting>
// All LargeObject API calls must be within a transaction
conn.setAutoCommit(false);
+
+// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.Connection)conn).getLargeObjectAPI();
PreparedStatement ps = con.prepareStatement("SELECT imgOID FROM imagesLO WHERE imgname=?");
byte buf[] = new byte[obj.size()];
obj.read(buf, 0, obj.size());
//do something with the data read here
- }
+
// Close the object
obj.close();
}