Re: a little disillusioned - Mailing list pgsql-jdbc

From David Wilbur
Subject Re: a little disillusioned
Date
Msg-id C259254F-5133-11D8-9A8D-0003931D9176@cybermesa.com
Whole thread Raw
In response to Re: a little disillusioned  (David Wilbur <wildboar@cybermesa.com>)
Responses Re: a little disillusioned
List pgsql-jdbc
at the risk of being over kill ... i started over from scratch after
getting the mysql version working... on the first try.



here is the layout of the files in the project directory i have (and is
the same for the mysql project except it is DBTest)

./build
./build/index.jsp
./build/WEB-INF
./build/WEB-INF/classes
./build/WEB-INF/classes/foo
./build/WEB-INF/classes/foo/PGTest.class
./build/WEB-INF/lib
./build/WEB-INF/web.xml
./build.xml
./src
./src/foo
./src/foo/PGTest.java
./web
./web/index.jsp
./web/WEB-INF
./web/WEB-INF/web.xml


the build.xml file has one line changed in it from the one that the
tomcat doc suggests you use:

   <property name="catalina.home" value="/usr/local/jakarta/tomcat"/>


after i build i

cp -r build  $CATALINA_HOME/webapps/PGTest

and then

$CATALINA_HOME/bin/startup.sh

which results in the page saying that you are not connected... but in
reality is not finding the driver if i place a try block around
Connection conn = ds.getConnection();





here are the relevant files in the reworked version:


more ./web/WEB-INF/web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
     <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
   <description>PostgreSQL Test App</description>
   <resource-ref>
       <description>PG DB Connection</description>
       <res-ref-name>jdbc/TestPG</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
   </resource-ref>
</web-app>



more ./web/index.jsp


<html>
   <head>
     <title>PG Test</title>
   </head>
   <body>

   <%
     foo.PGTest tst = new foo.PGTest();
     tst.init();
   %>

   <h2>Results</h2>
     Foo <%= tst.getFoo() %><br/>
     Bar <%= tst.getBar() %>

   </body>
</html>



more ./src/foo/PGTest.java


package foo;

import javax.naming.*;
import javax.sql.*;
import java.sql.*;

public class PGTest {

   String foo = "Not Connected";
   int bar = -1;

   public void init() {
     try{
       Context ctx = new InitialContext();
       if(ctx == null )
           throw new Exception("Boom - No Context");

       DataSource ds =
             (DataSource)ctx.lookup(
                "java:comp/env/jdbc/TestPG");

       if (ds != null) {
         Connection conn = ds.getConnection();

         if(conn != null)  {
             foo = "Got Connection "+conn.toString();
             Statement stmt = conn.createStatement();
             ResultSet rst =
                 stmt.executeQuery(
                   "select id, foo, bar from testdata");
             if(rst.next()) {
                foo=rst.getString(2);
                bar=rst.getInt(3);
             }
             conn.close();
         }
       }
     }catch(Exception e) {
       e.printStackTrace();
     }
  }

  public String getFoo() { return foo; }
  public int getBar() { return bar;}
}



cat /usr/local/jakarta/tomcat/conf/server.xml

[snip]

<Context path="/PGTest" docBase="PGTest" debug="5" reloadable="true"
crossContext="true">

   <Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_PGTest_log." suffix=".txt" timestamp="true"/>

   <Resource name="jdbc/TestPG" auth="Container"
type="javax.sql.DataSource"/>

   <ResourceParams name="jdbc/TestPG">
     <parameter>
       <name>factory</name>
       <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
     </parameter>

     <parameter>
       <name>maxActive</name>
       <value>100</value>
     </parameter>

     <parameter>
       <name>maxIdle</name>
       <value>30</value>
     </parameter>

     <parameter>
       <name>maxWait</name>
       <value>10000</value>
     </parameter>

     <parameter>
      <name>username</name>
      <value>jakarta</value>
     </parameter>
     <parameter>
      <name>password</name>
      <value>tomcat</value>
     </parameter>

     <parameter>
        <name>driverClassName</name>
        <value>org.postgres.Driver</value>
     </parameter>

     <parameter>
       <name>url</name>
       <value>jdbc:postgresql://127.0.0.1:5432/test</value>
     </parameter>
   </ResourceParams>
</Context>

[snip]



here is the environment that the server is started with:



ANT_HOME=/usr/local/jakarta/ant
BASH=/bin/bash
BASH_VERSINFO=([0]="2" [1]="05b" [2]="0" [3]="1" [4]="release"
[5]="powerpc-apple-darwin7.0")
BASH_VERSION='2.05b.0(1)-release'
CATALINA_HOME=/usr/local/jakarta/tomcat
CLASSPATH=/Library/Java/Home/lib:/usr/local/jakarta/ant/lib
COLUMNS=141
DIRSTACK=()
DYLD_LIBRARY_PATH=/usr/local/lib:/usr/X11R6/lib:/usr/lib:/usr/local/
pgsql/lib:/usr/local/mysql/lib
EUID=504
GROUPS=()
HISTFILE=/Users/jakarta/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/Users/jakarta
HOSTNAME=Halley.local
HOSTTYPE=powerpc
IFS=$' \t\n'
JAVA_HOME=/Library/Java/Home
LINES=76
MACHTYPE=powerpc-apple-darwin7.0
MAILCHECK=60
MANPATH=/usr/local/man:/usr/X11R6/man:/usr/share/man:/usr/local/pgsql/
man:/usr/local/mysql/man
MYSQL_DATA=/usr/local/mysql/var
MYSQL_HOME=/usr/local/mysql
OLDPWD=/Users/jakarta/Projects
OPTERR=1
OPTIND=1
OSTYPE=darwin7.0
PATH=/usr/local/bin:/usr/X11R6/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/
local/jakarta/ant/bin:/usr/local/pgsql/bin:/usr/local/mysql/bin
PGDATA=/usr/local/pgsql/data
PGHOME=/usr/local/pgsql
PIPESTATUS=([0]="0")
PPID=8049
PS1='\h:\w \u\$ '
PS2='> '
PS4='+ '
PWD=/Users/jakarta/Projects/jakarta
SHARED_SETUP_FILES=/usr/local/share/setup
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-
comments:monitor
SHLVL=1
TERM=xterm-color
UID=504
USER=jakarta
_=pgtest.junk/src/foo/DBTest.java



here is an example that demonstrates that out of tomcat that the
postgres.jar  that is in the proper place for tomcat works for a
regular java app and that the account and url in the xml files are
correct:

echo $CLASSPATH
/Library/Java/Home/lib:/usr/local/jakarta/ant/lib

export
CLASSPATH=${CLASSPATH}:/usr/local/jakarta/tomcat/common/lib/
postgresql.jar:.

javac -classpath ${CLASSPATH} example/basic.java
Note: example/basic.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

java  example.basic jdbc:postgresql://127.0.0.1:5432/test jakarta tomcat
PostgreSQL basic test v6.3 rev 1

Connecting to Database URL = jdbc:postgresql://127.0.0.1:5432/test
Connected...Now creating a statement

Running tests:
Inserted row with oid 17246
Updated 4 rows
deleted 2 rows
performing a query
   a=3 b=8
   a=4 b=8
   a=4 b=2
   a=4 b=3
   a=4 b=4
performing another query
   a=3 b=8
   a=4 b=8
   a=4 b=2
   a=4 b=3
   a=4 b=4
performing a query limited to 3
   a=3 b=8
   a=4 b=8
   a=4 b=2
Now closing the connection


pgsql-jdbc by date:

Previous
From: David Wilbur
Date:
Subject: Re: a little disillusioned
Next
From: Jeremy Buchmann
Date:
Subject: Re: getting primary key values for inserted records?