old-www/HOWTO/Enterprise-Java-for-Linux-H...

364 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
<TITLE>Enterprise Java for Linux HOWTO: How to Setup JDBC Support </TITLE>
<LINK HREF="Enterprise-Java-for-Linux-HOWTO-7.html" REL=next>
<LINK HREF="Enterprise-Java-for-Linux-HOWTO-5.html" REL=previous>
<LINK HREF="Enterprise-Java-for-Linux-HOWTO.html#toc6" REL=contents>
</HEAD>
<BODY>
<A HREF="Enterprise-Java-for-Linux-HOWTO-7.html">Next</A>
<A HREF="Enterprise-Java-for-Linux-HOWTO-5.html">Previous</A>
<A HREF="Enterprise-Java-for-Linux-HOWTO.html#toc6">Contents</A>
<HR>
<H2><A NAME="sec: jdbc"></A> <A NAME="s6">6. How to Setup JDBC Support </A></H2>
<P>
<P>There are several databases that run on Linux that also support a JDBC
interface. These include:
<UL>
<LI>
<A HREF="#sec: db2">IBM DB2 </A></LI>
<LI>
<A HREF="#sec: minisql">MiniSQL </A></LI>
<LI>
<A HREF="#sec: mysql">MySQL </A></LI>
<LI>
<A HREF="#sec: oracle">Oracle </A></LI>
<LI>
<A HREF="#sec: postgresql">PostgreSQL </A></LI>
<LI>
<A HREF="#sec: sybase">Sybase </A></LI>
</UL>
<P>If you are going to try just one DBMS, I suggest you initially try PostgreSQL,
principally because it comes with most major Linux distributions and may already
be installed on your system.
<H2><A NAME="sec: db2"></A> <A NAME="ss6.1">6.1 IBM DB2 </A>
</H2>
<P>
<P>To be written.
<P>See
<A HREF="http://www-4.ibm.com/software/data/db2/linux/">http://www-4.ibm.com/software/data/db2/linux/</A>for
more information.
<H2><A NAME="sec: minisql"></A> <A NAME="ss6.2">6.2 MiniSQL </A>
</H2>
<P>
<P>To be written.
<P>See
<A HREF="http://www.hughes.com.au/">http://www.hughes.com.au/</A>for
more information.
<H2><A NAME="sec: mysql"></A> <A NAME="ss6.3">6.3 MySQL </A>
</H2>
<P>
<P>To be written.
<P>See
<A HREF="http://www.mysql.org/">http://www.mysql.org/</A>for
more information.
<H2><A NAME="sec: oracle"></A> <A NAME="ss6.4">6.4 Oracle </A>
</H2>
<P>
<P>To be written.
<P>See
<A HREF="http://platforms.oracle.com/linux/">http://platforms.oracle.com/linux/</A><H2><A NAME="sec: postgresql"></A> <A NAME="ss6.5">6.5 PostgreSQL </A>
</H2>
<P>
<H3>Background </H3>
<P>
<P>PostgreSQL is a sophisticated Object-Relational DBMS, supporting almost
all SQL constructs, including subselects, transactions, and user-defined types
and functions. It is the most advanced open-source database available anywhere.
Commercial Support is also available from PostgreSQL, Inc. The current version
is 6.5.3 and is available at any of the many mirror sites or on CD. (From
the PostgreSQL website.)
<P>PostgreSQL may have already been shipped with your Linux distribution because
of its open source license.
<H3>Download and Installation </H3>
<P>
<P>Rather than downloading from PostgreSQL, I suggest you initially try the
PostgreSQL that most likely came with your Linux distribution.
<P>Alternatively, PostgreSQL can be obtained from
<A HREF="http://www.postgresql.org">http://www.postgresql.org</A>.
<P>
<P>To confirm that PostgreSQL is installed on your computer, type:
<PRE>
rpm -qa | grep postgresql
</PRE>
<P>or
<PRE>
which postmaster
which psql
</PRE>
<P>You need the postgresql, postgresql-server, and postgresql-java packages
installed to use Java with PostgreSQL.
<P>Make sure PostgreSQL is running. Type:
<PRE>
ps -f -u postgres
</PRE>
<P>You should see postmaster, the PostgreSQL daemon, running.
<P>If postmaster is not running, there will probably be a Sys V Init script
that you can use to start it. In many distributions it is located in /etc/rc.d/init.d.
To start PostgreSQL, type:
<PRE>
cd /etc/rc.d/init.d
./postgresql start
</PRE>
<P>You can use the above &quot;ps&quot; command to confirm that PostgreSQL
is running.
<P>Note: To use JDBC, PostgreSQL needs to have been started with the '-i'
parameter indicating support for TCP/IP connections rather than solely UNIX
domain sockets. Confirm that postmaster&gt; was started with the '-i' paramter.
<P>
<P>Create a test database by typing:
<PRE>
su - postgres
createdb javatest
</PRE>
<P>You should see no error messages.
<P>Create a test table with one test row. First, log in to the interactive
PostgreSQL tool and connect to the javatest database you just created by typing
(as the postgres user):
<PRE>
psql javatest
</PRE>
<P>You should see confirmation that you are connected to the database: javatest.
<P>
<P>Then, create the test table by typing (within psql):
<PRE>
create table test (col1 varchar(255));
</PRE>
<P>You should see the &quot;CREATE&quot; confirmation message.
<P>Next, insert one row by typing (within psql):
<PRE>
insert into test (col1) values ('Hello, from PostgreSQL!');
</PRE>
<P>You should see the &quot;INSERT&quot; confirmation message.
<P>Finally, confirm that the row is there by typing (within psql):
<PRE>
select col1 from test;
</PRE>
<P>You should see the row you just created.
<P>You can exit psql by typing &quot;\ q&quot;.
<P>For more assistance on working with PostgreSQL, I suggest you look into
the Database-SQL-RDBMS HOW-TO document for Linux (PostgreSQL Object Relational
Database System) at
<A HREF="http://metalab.unc.edu/mdw/HOWTO/PostgreSQL-HOWTO.html">http://metalab.unc.edu/mdw/HOWTO/PostgreSQL-HOWTO.html</A>.
<P>
<P>You will need to add the appropriate JAR to your CLASSPATH. The PostgreSQL
JARs come in the <EM>postgresql-jdbc</EM> package.
<PRE>
export CLASSPATH=$CLASSPATH:/usr/lib/pgsql/jdbc6.5-1.2.jar
</PRE>
<P>You may need to substitute the path depending you where PostgreSQL is installed
on your system.
<H3>Confirming Your Installation </H3>
<P>
<P>You are now ready to compile and run a simple JDBC application that uses
PostgreSQL. Create the following program.
<PRE>
import java.sql.*;
class PostgreSQLTest {
public static void main (String[] args) {
try {
Driver driver = (Driver)
Class.forName(&quot;postgresql.Driver&quot;).newInstance();
DriverManager.registerDriver(driver);
String url = &quot;jdbc:postgresql:javatest&quot;;
Connection con = DriverManager.getConnection(url, &quot;postgres&quot;, &quot;&quot;);
Statement stm = con.createStatement();
stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery(&quot;select col1 from test&quot;);
rs.next();
System.out.println(rs.getString(1));
} catch (SQLException e) {
System.out.println(&quot;Exception!&quot;);
System.out.println(e.toString());
}
}
</PRE>
<P>Compile the program with the Java compiler.
<PRE>
javac PostgreSQLTest.java
</PRE>
<P>If the compiler produces errors, double check the syntax and confirm your
PATH and CLASSPATH.
<P>Run the program with the JVM.
<PRE>
java PostgreSQLTest
</PRE>
<P>If the JVM produces errors, confirm your PATH and CLASSPATH.
<P>You should see the following output:
<PRE>
Hello, from PostgreSQL!
</PRE>
<P>Congratulations, you have installed, set up an environment for, and tested
a JDBC interface to PostgreSQL.
<H3>More Information</H3>
<P>
<P>For more information, I suggest you look into the PostgreSQL website at
<P>
<A HREF="http://www.postgresql.org/">http://www.postgresql.org/</A>.
<P>
<H2><A NAME="sec: sybase"></A> <A NAME="ss6.6">6.6 Sybase </A>
</H2>
<P>
<H3>Background </H3>
<P>
<P>Sybase Adaptive Server Enterprise is a commericial RDBMS that is available
for the Linux operating system. While Sybase has recently released version
12.0, version 11.9.2 is available for Linux.
<P>According to the Sybase website, &quot;By porting ASE to Linux, Sybase
provides the Linux development community with the first highly scalable, high-performance
database engine available for the platform. The package includes the standard
features of Adaptive Server Enterprise and all related connectivity components.
Adaptive Server Enterprise 11.9.2 is offered FREE for development.&quot;
<P>
<H3>Download </H3>
<P>
<P>The Sybase ASE can be obtained from
<A HREF="http://www.sybase.com/products/databaseservers/linux/linux1192_reg.html">http://www.sybase.com/products/databaseservers/linux/linux1192_reg.html</A>.
<P>
<P>In order to download, you will have to register with the Sybase website
and agree to the license online.
<P>The Sybase JDBC driver can be obtained from
<A HREF="http://www.sybase.com/products/internet/jconnect/">http://www.sybase.com/products/internet/jconnect/</A>.
<P>
<P>Select download jConnect 4.2/5.2.
<P>If you have access to a Sybase server on the network, you only need to
download and install the JDBC driver.
<H3>Installation </H3>
<P>
<P>Installation of Sybase is beyond the scope of this HOWTO. This HOWTO will
assume that Sybase has been correctly installed and configured and that you
can get to Sybase using isql.
<P>Log into isql as sa and create a test user and test database by typing:
<P>
<PRE>
create database javatest
go
sp_addlogin javatest, javatest, javatest
go
use javatest
go
sp_dbowner javatest
go
</PRE>
<P>You should see no error messages.
<P>Create a test table with one test row. First, log in to isql as the javatest
user and type:
<PRE>
create table test (col1 varchar(255))
go
</PRE>
<P>You should see no error messages.
<P>Next, insert one row by typing:
<PRE>
insert into test (col1) values ('Hello, from Sybase!')
go
</PRE>
<P>You should see no error messages.
<P>Finally, confirm that the row is there by typing:
<PRE>
select col1 from test
go
</PRE>
<P>You should see the row you just created.
<P>You can exit isql by typing &quot;exit&quot;.
<P>For more assistance on working with Sybase, review the documentation that
can be downloaded with Sybase.
<P>You will need to add the appropriate JAR to your CLASSPATH.
<PRE>
export CLASSPATH=$CLASSPATH:/usr/local/sybase/jConnect-5_2/classes/jconn2.jar
</PRE>
<P>You may need to substitute the path depending you where jConnect is installed
on your system.
<H3>Confirming Your Installation </H3>
<P>
<P>You are now ready to compile and run a simple JDBC application that uses
Sybase. Create the following program.
<PRE>
import java.sql.*;
class SybaseTest {
public static void main (String[] args) {
try {
Driver driver = (Driver)
Class.forName(&quot;com.sybase.jdbc2.jdbc.SybDriver&quot;).newInstance();
DriverManager.registerDriver(driver);
String host = &quot;127.0.0.1&quot;;
String port = &quot;4100&quot;;
String url = &quot;jdbc:sybase:Tds:&quot; + host + &quot;:&quot; + port;
Connection con = DriverManager.getConnection(url, &quot;javatest&quot;, &quot;javatest&quot;);
Statement stm = con.createStatement();
stm.setQueryTimeout(10);
ResultSet rs = stm.executeQuery(&quot;select col1 from test&quot;);
rs.next();
System.out.println(rs.getString(1));
} catch (SQLException e) {
System.out.println(&quot;Exception!&quot;);
System.out.println(e.toString());
}
}
</PRE>
<P>You will need to substitute the host and port number of you Sybase server
as appropriate. See $SYBASE/interfaces and the $DSQUERY entry
for what values to use for the host and port number.
<P>Compile the program with the Java compiler.
<PRE>
javac SybaseTest.java
</PRE>
<P>If the compiler produces errors, double check the syntax and confirm your
PATH and CLASSPATH.
<P>Run the program with the JVM.
<PRE>
java SybaseTest
</PRE>
<P>If the JVM produces errors, confirm your PATH and CLASSPATH.
<P>You should see the following output:
<PRE>
Hello, from Sybase!
</PRE>
<P>Congratulations, you have installed, set up an environment for, and tested
a JDBC interface to Sybase.
<H3>More Information</H3>
<P>
<P>For more information, I suggest you look into the Sybase jConnect website
at
<A HREF="http://www.sybase.com/products/internet/jconnect/">http://www.sybase.com/products/internet/jconnect/</A>.
<P>
<HR>
<A HREF="Enterprise-Java-for-Linux-HOWTO-7.html">Next</A>
<A HREF="Enterprise-Java-for-Linux-HOWTO-5.html">Previous</A>
<A HREF="Enterprise-Java-for-Linux-HOWTO.html#toc6">Contents</A>
</BODY>
</HTML>