Thread: JDBC

JDBC

From
Marcelo Pereira
Date:
Hi All,

I have been working to make PostgreSQL acessible from java. I have
downloaded and instaled the following:

- j2sdk-1_4_1_01-linux-i586.bin
- jakarta-ant-1.5.1-src.tar.gz

and compiled PostgreSQL as:

# ./configure --with-java
# make
# make install

The driver (postgresql.jar) was compiled properly, and I have configured
some variables:

# export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:.
# export ANT_HOME=/usr/local/ant
# export JAVA_HOME=/usr/local/j2sdk1.4.1
# export PATH=${JAVA_HOME}:${PATH}:${ANT_HOME}/bin

So I think the environment is almost good, but I can't compile a source
that use jdbc.

Would you send to me a really simple example java source code using jdbc,
acessing a simple table at PostgreSQL? I'm really having problem with
this.

Thanks in advance and
Best Regards,

Marcelo Pereira

-- Remember that only God and ^[:w saves.
        __
       (_.\           © Marcelo Pereira     |
        / / ___       marcelo@pereira.com   |
       / (_/ _ \__    [Math|99]-IMECC       |
_______\____/_\___)___Unicamp_______________/


Re: JDBC

From
Jeffrey Melloy
Date:
Marcelo Pereira wrote:

>Would you send to me a really simple example java source code using jdbc,
>acessing a simple table at PostgreSQL
>

Giving credit where credit is due, this is Mark Liyanage's simple java
program, from www.entorpy.ch.  It was written for OS X, but there
shouldn't be a problem.

/*
 * TestPostgreSQL.java
 *
 *
 * History:
 *
 * When         Who               What
 * ==============================================================================
 * 2001-06-23   Marc Liyanage     First version
 *
 *
 * License:
 *
 * Copyright abandoned 2001 by Marc Liyanage
 * Do with this whatever you want.
 *
 */

import java.sql.*;

/**
 * The TestPostgreSQL class shows how to access the PostgreSQL
 * DB server on Mac OS X using the JDBC interface.
 * It assumes the installation has been performed according
 * to the instructions at http://www.entropy.ch/software/macosx/postgresql.
 *
 *
 * You compile it like this:
 *
 *   % javac TestPostgreSQL.java
 *
 * Make sure that the PostgreSQL server has been
 * started with the -i flag. This is not the case in
 * the example lines of the installation instructions mentioned
 * above and in the StartupItem package that's available
 * from the same location. The -i flag tells the DB server
 * to listen for connection requests from the network
 * and I have left it off by default for security reasons.
 *
 * If the server is running correctly (with -i), run the Test like this:
 * (in the same directory where you compiled the example)
 *
 *   % java -classpath /usr/local/pgsql/share/java/postgresql.jar:. TestPostgreSQL
 *
 * You should see the current date as returned by the DB server:
 *
 *   2001-06-23 16:31:49+02
 *
 *
 * @author   Marc Liyanage
 * @version  1.0
 */
public class TestPostgreSQL {


    public static void main(String argv[]) throws Exception {

        // Load the driver class
        //
        Class.forName("org.postgresql.Driver");

        // Try to connect to the DB server.
        // We tell JDBC to use the "postgresql" driver
        // and to connect to the "template1" database
        // which should always exist in PostgreSQL.
        // We use the username "postgres" and no
        // password to connect. Since we're not accessing
        // any tables but only an SQL function
        // this should work.
        //
        Connection conn = DriverManager.getConnection(
            "jdbc:postgresql:template1",
            "postgres",
            ""
        );

        // Set up and run a query that fetches
        // the current date using the "now()" PostgreSQL function.
        //
        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery("SELECT now();");

        // Iterate through the rows of the result set
        // (obviously only one row in this example) and
        // print each one.
        //
        while (rset.next()) {
            System.out.println(rset.getString(1));
        }

        // Close result set, statement and DB connection
        //
        rset.close();
        stmt.close();
        conn.close();

    }


}






Re: JDBC

From
Marcelo Pereira
Date:
Hi Jeffrey,

Thanks for your example code. I have compiled it without any problem, but
when I try to run I have the following:

$ javac TestPostgreSQL.java
$ java TestPostgreSQL.class
Exception in thread
"main" java.lang.NoClassDefFoundError: TestPostgreSQL/class

$ java -classpath
/usr/local/pgsql/share/java/postgresql.jar:. TestPostgreSQL.class
Exception in thread
"main" java.lang.NoClassDefFoundError: TestPostgreSQL/class

$ export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:.
$ java TestPostgreSQL.class
Exception in thread
"main" java.lang.NoClassDefFoundError: TestPostgreSQL/class

As you can see I have this problem, whatever I try to do. So I thought my
JDBC driver wasn't properly compiled, so I have downloaded the 7.3 JDBC
driver from PostgreSQL.org (pg73jdbc1.jar) and run all the tests above,
always with the same problem.

What am I doing wrong??

Thanks in advance and
Best Regards,

Marcelo Pereira

-- Remember that only God and ^[:w saves.
        __
       (_.\           © Marcelo Pereira     |
        / / ___       marcelo@pereira.com   |
       / (_/ _ \__    [Math|99]-IMECC       |
_______\____/_\___)___Unicamp_______________/

--- Jeffrey Melloy, with his fast fingers, wrote:

:> Marcelo Pereira wrote:
:>
:> >Would you send to me a really simple example java source code using jdbc,
:> >acessing a simple table at PostgreSQL
:> >
:>
:> Giving credit where credit is due, this is Mark Liyanage's simple java
:> program, from www.entorpy.ch.  It was written for OS X, but there
:> shouldn't be a problem.
:>
:> /*
:>  * TestPostgreSQL.java
:>  *
:>  *
:>  * History:
:>  *
:>  * When         Who               What
:>  * ==============================================================================
:>  * 2001-06-23   Marc Liyanage     First version
:>  *
:>  *
:>  * License:
:>  *
:>  * Copyright abandoned 2001 by Marc Liyanage
:>  * Do with this whatever you want.
:>  *
:>  */
:>
:> import java.sql.*;
:>
:> /**
:>  * The TestPostgreSQL class shows how to access the PostgreSQL
:>  * DB server on Mac OS X using the JDBC interface.
:>  * It assumes the installation has been performed according
:>  * to the instructions at http://www.entropy.ch/software/macosx/postgresql.
:>  *
:>  *
:>  * You compile it like this:
:>  *
:>  *   % javac TestPostgreSQL.java
:>  *
:>  * Make sure that the PostgreSQL server has been
:>  * started with the -i flag. This is not the case in
:>  * the example lines of the installation instructions mentioned
:>  * above and in the StartupItem package that's available
:>  * from the same location. The -i flag tells the DB server
:>  * to listen for connection requests from the network
:>  * and I have left it off by default for security reasons.
:>  *
:>  * If the server is running correctly (with -i), run the Test like this:
:>  * (in the same directory where you compiled the example)
:>  *
:>  *   % java -classpath /usr/local/pgsql/share/java/postgresql.jar:. TestPostgreSQL
:>  *
:>  * You should see the current date as returned by the DB server:
:>  *
:>  *   2001-06-23 16:31:49+02
:>  *
:>  *
:>  * @author   Marc Liyanage
:>  * @version  1.0
:>  */
:> public class TestPostgreSQL {
:>
:>
:>     public static void main(String argv[]) throws Exception {
:>
:>         // Load the driver class
:>         //
:>         Class.forName("org.postgresql.Driver");
:>
:>         // Try to connect to the DB server.
:>         // We tell JDBC to use the "postgresql" driver
:>         // and to connect to the "template1" database
:>         // which should always exist in PostgreSQL.
:>         // We use the username "postgres" and no
:>         // password to connect. Since we're not accessing
:>         // any tables but only an SQL function
:>         // this should work.
:>         //
:>         Connection conn = DriverManager.getConnection(
:>             "jdbc:postgresql:template1",
:>             "postgres",
:>             ""
:>         );
:>
:>         // Set up and run a query that fetches
:>         // the current date using the "now()" PostgreSQL function.
:>         //
:>         Statement stmt = conn.createStatement();
:>         ResultSet rset = stmt.executeQuery("SELECT now();");
:>
:>         // Iterate through the rows of the result set
:>         // (obviously only one row in this example) and
:>         // print each one.
:>         //
:>         while (rset.next()) {
:>             System.out.println(rset.getString(1));
:>         }
:>
:>         // Close result set, statement and DB connection
:>         //
:>         rset.close();
:>         stmt.close();
:>         conn.close();
:>
:>     }
:>
:>
:> }
:>
:>
:>
:>
:>
:>
:> ---------------------------(end of broadcast)---------------------------
:> TIP 6: Have you searched our list archives?
:>
:> http://archives.postgresql.org
:>


Re: [JDBC] JDBC

From
Dave Cramer
Date:
Marcelo,

Just type

java TestPostreSQL

to run it

Dave
On Wed, 2003-01-15 at 08:08, Marcelo Pereira wrote:
> Hi Jeffrey,
>
> Thanks for your example code. I have compiled it without any problem, but
> when I try to run I have the following:
>
> $ javac TestPostgreSQL.java
> $ java TestPostgreSQL.class
> Exception in thread
> "main" java.lang.NoClassDefFoundError: TestPostgreSQL/class
>
> $ java -classpath
> /usr/local/pgsql/share/java/postgresql.jar:. TestPostgreSQL.class
> Exception in thread
> "main" java.lang.NoClassDefFoundError: TestPostgreSQL/class
>
> $ export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:.
> $ java TestPostgreSQL.class
> Exception in thread
> "main" java.lang.NoClassDefFoundError: TestPostgreSQL/class
>
> As you can see I have this problem, whatever I try to do. So I thought my
> JDBC driver wasn't properly compiled, so I have downloaded the 7.3 JDBC
> driver from PostgreSQL.org (pg73jdbc1.jar) and run all the tests above,
> always with the same problem.
>
> What am I doing wrong??
>
> Thanks in advance and
> Best Regards,
>
> Marcelo Pereira
>
> -- Remember that only God and ^[:w saves.
>         __
>        (_.\           © Marcelo Pereira     |
>         / / ___       marcelo@pereira.com   |
>        / (_/ _ \__    [Math|99]-IMECC       |
> _______\____/_\___)___Unicamp_______________/
>
> --- Jeffrey Melloy, with his fast fingers, wrote:
>
> :> Marcelo Pereira wrote:
> :>
> :> >Would you send to me a really simple example java source code using jdbc,
> :> >acessing a simple table at PostgreSQL
> :> >
> :>
> :> Giving credit where credit is due, this is Mark Liyanage's simple java
> :> program, from www.entorpy.ch.  It was written for OS X, but there
> :> shouldn't be a problem.
> :>
> :> /*
> :>  * TestPostgreSQL.java
> :>  *
> :>  *
> :>  * History:
> :>  *
> :>  * When         Who               What
> :>  * ==============================================================================
> :>  * 2001-06-23   Marc Liyanage     First version
> :>  *
> :>  *
> :>  * License:
> :>  *
> :>  * Copyright abandoned 2001 by Marc Liyanage
> :>  * Do with this whatever you want.
> :>  *
> :>  */
> :>
> :> import java.sql.*;
> :>
> :> /**
> :>  * The TestPostgreSQL class shows how to access the PostgreSQL
> :>  * DB server on Mac OS X using the JDBC interface.
> :>  * It assumes the installation has been performed according
> :>  * to the instructions at http://www.entropy.ch/software/macosx/postgresql.
> :>  *
> :>  *
> :>  * You compile it like this:
> :>  *
> :>  *   % javac TestPostgreSQL.java
> :>  *
> :>  * Make sure that the PostgreSQL server has been
> :>  * started with the -i flag. This is not the case in
> :>  * the example lines of the installation instructions mentioned
> :>  * above and in the StartupItem package that's available
> :>  * from the same location. The -i flag tells the DB server
> :>  * to listen for connection requests from the network
> :>  * and I have left it off by default for security reasons.
> :>  *
> :>  * If the server is running correctly (with -i), run the Test like this:
> :>  * (in the same directory where you compiled the example)
> :>  *
> :>  *   % java -classpath /usr/local/pgsql/share/java/postgresql.jar:. TestPostgreSQL
> :>  *
> :>  * You should see the current date as returned by the DB server:
> :>  *
> :>  *   2001-06-23 16:31:49+02
> :>  *
> :>  *
> :>  * @author   Marc Liyanage
> :>  * @version  1.0
> :>  */
> :> public class TestPostgreSQL {
> :>
> :>
> :>     public static void main(String argv[]) throws Exception {
> :>
> :>         // Load the driver class
> :>         //
> :>         Class.forName("org.postgresql.Driver");
> :>
> :>         // Try to connect to the DB server.
> :>         // We tell JDBC to use the "postgresql" driver
> :>         // and to connect to the "template1" database
> :>         // which should always exist in PostgreSQL.
> :>         // We use the username "postgres" and no
> :>         // password to connect. Since we're not accessing
> :>         // any tables but only an SQL function
> :>         // this should work.
> :>         //
> :>         Connection conn = DriverManager.getConnection(
> :>             "jdbc:postgresql:template1",
> :>             "postgres",
> :>             ""
> :>         );
> :>
> :>         // Set up and run a query that fetches
> :>         // the current date using the "now()" PostgreSQL function.
> :>         //
> :>         Statement stmt = conn.createStatement();
> :>         ResultSet rset = stmt.executeQuery("SELECT now();");
> :>
> :>         // Iterate through the rows of the result set
> :>         // (obviously only one row in this example) and
> :>         // print each one.
> :>         //
> :>         while (rset.next()) {
> :>             System.out.println(rset.getString(1));
> :>         }
> :>
> :>         // Close result set, statement and DB connection
> :>         //
> :>         rset.close();
> :>         stmt.close();
> :>         conn.close();
> :>
> :>     }
> :>
> :>
> :> }
> :>
> :>
> :>
> :>
> :>
> :>
> :> ---------------------------(end of broadcast)---------------------------
> :> TIP 6: Have you searched our list archives?
> :>
> :> http://archives.postgresql.org
> :>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
--
Dave Cramer <Dave@micro-automation.net>


Re: [JDBC] JDBC

From
Marcelo Pereira
Date:
Hi Jeffrey, Dave and All,

Thanks for your answer, I got it running!

Thanks and Best Regards,

Marcelo Pereira

-- Remember that only God and ^[:w saves.
        __
       (_.\           © Marcelo Pereira     |
        / / ___       marcelo@pereira.com   |
       / (_/ _ \__    [Math|99]-IMECC       |
_______\____/_\___)___Unicamp_______________/

--- Dave Cramer, with his fast fingers, wrote:

:> Marcelo,
:>
:> Just type
:>
:> java TestPostreSQL
:>
:> to run it
:>
:> Dave
:> On Wed, 2003-01-15 at 08:08, Marcelo Pereira wrote:
:> > Hi Jeffrey,
:> >
:> > Thanks for your example code. I have compiled it without any problem, but
:> > when I try to run I have the following:
:> >
:> > $ javac TestPostgreSQL.java
:> > $ java TestPostgreSQL.class
:> > Exception in thread
:> > "main" java.lang.NoClassDefFoundError: TestPostgreSQL/class
:> >
:> > $ java -classpath
:> > /usr/local/pgsql/share/java/postgresql.jar:. TestPostgreSQL.class
:> > Exception in thread
:> > "main" java.lang.NoClassDefFoundError: TestPostgreSQL/class
:> >
:> > $ export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:.
:> > $ java TestPostgreSQL.class
:> > Exception in thread
:> > "main" java.lang.NoClassDefFoundError: TestPostgreSQL/class
:> >
:> > As you can see I have this problem, whatever I try to do. So I thought my
:> > JDBC driver wasn't properly compiled, so I have downloaded the 7.3 JDBC
:> > driver from PostgreSQL.org (pg73jdbc1.jar) and run all the tests above,
:> > always with the same problem.
:> >
:> > What am I doing wrong??
:> >
:> > Thanks in advance and
:> > Best Regards,
:> >
:> > Marcelo Pereira
:> >
:> > -- Remember that only God and ^[:w saves.
:> >         __
:> >        (_.\           © Marcelo Pereira     |
:> >         / / ___       marcelo@pereira.com   |
:> >        / (_/ _ \__    [Math|99]-IMECC       |
:> > _______\____/_\___)___Unicamp_______________/
:> >
:> > --- Jeffrey Melloy, with his fast fingers, wrote:
:> >
:> > :> Marcelo Pereira wrote:
:> > :>
:> > :> >Would you send to me a really simple example java source code using jdbc,
:> > :> >acessing a simple table at PostgreSQL
:> > :> >
:> > :>
:> > :> Giving credit where credit is due, this is Mark Liyanage's simple java
:> > :> program, from www.entorpy.ch.  It was written for OS X, but there
:> > :> shouldn't be a problem.
:> > :>
:> > :> /*
:> > :>  * TestPostgreSQL.java
:> > :>  *
:> > :>  *
:> > :>  * History:
:> > :>  *
:> > :>  * When         Who               What
:> > :>  * ==============================================================================
:> > :>  * 2001-06-23   Marc Liyanage     First version
:> > :>  *
:> > :>  *
:> > :>  * License:
:> > :>  *
:> > :>  * Copyright abandoned 2001 by Marc Liyanage
:> > :>  * Do with this whatever you want.
:> > :>  *
:> > :>  */
:> > :>
:> > :> import java.sql.*;
:> > :>
:> > :> /**
:> > :>  * The TestPostgreSQL class shows how to access the PostgreSQL
:> > :>  * DB server on Mac OS X using the JDBC interface.
:> > :>  * It assumes the installation has been performed according
:> > :>  * to the instructions at http://www.entropy.ch/software/macosx/postgresql.
:> > :>  *
:> > :>  *
:> > :>  * You compile it like this:
:> > :>  *
:> > :>  *   % javac TestPostgreSQL.java
:> > :>  *
:> > :>  * Make sure that the PostgreSQL server has been
:> > :>  * started with the -i flag. This is not the case in
:> > :>  * the example lines of the installation instructions mentioned
:> > :>  * above and in the StartupItem package that's available
:> > :>  * from the same location. The -i flag tells the DB server
:> > :>  * to listen for connection requests from the network
:> > :>  * and I have left it off by default for security reasons.
:> > :>  *
:> > :>  * If the server is running correctly (with -i), run the Test like this:
:> > :>  * (in the same directory where you compiled the example)
:> > :>  *
:> > :>  *   % java -classpath /usr/local/pgsql/share/java/postgresql.jar:. TestPostgreSQL
:> > :>  *
:> > :>  * You should see the current date as returned by the DB server:
:> > :>  *
:> > :>  *   2001-06-23 16:31:49+02
:> > :>  *
:> > :>  *
:> > :>  * @author   Marc Liyanage
:> > :>  * @version  1.0
:> > :>  */
:> > :> public class TestPostgreSQL {
:> > :>
:> > :>
:> > :>     public static void main(String argv[]) throws Exception {
:> > :>
:> > :>         // Load the driver class
:> > :>         //
:> > :>         Class.forName("org.postgresql.Driver");
:> > :>
:> > :>         // Try to connect to the DB server.
:> > :>         // We tell JDBC to use the "postgresql" driver
:> > :>         // and to connect to the "template1" database
:> > :>         // which should always exist in PostgreSQL.
:> > :>         // We use the username "postgres" and no
:> > :>         // password to connect. Since we're not accessing
:> > :>         // any tables but only an SQL function
:> > :>         // this should work.
:> > :>         //
:> > :>         Connection conn = DriverManager.getConnection(
:> > :>             "jdbc:postgresql:template1",
:> > :>             "postgres",
:> > :>             ""
:> > :>         );
:> > :>
:> > :>         // Set up and run a query that fetches
:> > :>         // the current date using the "now()" PostgreSQL function.
:> > :>         //
:> > :>         Statement stmt = conn.createStatement();
:> > :>         ResultSet rset = stmt.executeQuery("SELECT now();");
:> > :>
:> > :>         // Iterate through the rows of the result set
:> > :>         // (obviously only one row in this example) and
:> > :>         // print each one.
:> > :>         //
:> > :>         while (rset.next()) {
:> > :>             System.out.println(rset.getString(1));
:> > :>         }
:> > :>
:> > :>         // Close result set, statement and DB connection
:> > :>         //
:> > :>         rset.close();
:> > :>         stmt.close();
:> > :>         conn.close();
:> > :>
:> > :>     }
:> > :>
:> > :>
:> > :> }
:> > :>
:> > :>
:> > :>
:> > :>
:> > :>
:> > :>
:> > :> ---------------------------(end of broadcast)---------------------------
:> > :> TIP 6: Have you searched our list archives?
:> > :>
:> > :> http://archives.postgresql.org
:> > :>
:> >
:> >
:> > ---------------------------(end of broadcast)---------------------------
:> > TIP 6: Have you searched our list archives?
:> >
:> > http://archives.postgresql.org
:> --
:> Dave Cramer <Dave@micro-automation.net>
:>
:>
:> ---------------------------(end of broadcast)---------------------------
:> TIP 2: you can get off all lists at once with the unregister command
:>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
:>