Re: problem in connetting using reflection - Mailing list pgsql-jdbc

From Giuseppe Sacco
Subject Re: problem in connetting using reflection
Date
Msg-id 4078F8C5.8000209@eppesuigoccas.homedns.org
Whole thread Raw
In response to Re: problem in connetting using reflection  (Kris Jurka <books@ejurka.com>)
List pgsql-jdbc
Kris Jurka wrote:
>
> On Sun, 11 Apr 2004, Giuseppe Sacco wrote:
>>[... my error code..]
> You haven't included the code that is actually mentioned in the
> stacktrace.  You'll need to either provide the rest of the code in
> question or even better a simpler example of the problem you are seeing.
>
> Kris Jurka

You are right. Today I made a smaller script, that is attached, and
found my error :-)
Now it works with PostgreSQL, Oracle and mySQL.

Thanks,
Giuseppe
import java.util.Properties;
import java.io.*;
import javax.sql.*;
import java.sql.*;

public class JDBCDAOFactory
{

    static final String datasourceclassname = "org.postgresql.jdbc3.Jdbc3ConnectionPool";
    static final String dbservername = "localhost";
    static final String dbserverportnumber = "5432";
    static final String dbname = "database";

/*
 * It should work with
#
# Oracle
#
#db.dataSourceClassName=oracle.jdbc.pool.OracleConnectionPoolDataSource
#db.servername=localhost
#db.serverportnumber=1521
#
# PostgreSQL
#
db.dataSourceClassName=org.postgresql.jdbc3.Jdbc3ConnectionPool
db.servername=localhost
db.serverportnumber=5432
#
# mySQL
#
#db.dataSourceClassName=gwe.sql.gweMysqlConnectionPoolDataSource
#db.servername=localhost
#db.serverportnumber=3306
 *
 */

    static private ConnectionPoolDataSource connectionPoolDataSource;

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

    connectToPg("myuser","mypassword");

    /*
     * Now we are supposed to be connected, so create a statement and execute it
     */

    doQuery();
    }

    static void doQuery() throws Exception {

    PooledConnection pooledConn = connectionPoolDataSource.getPooledConnection();
    Statement stmt = pooledConn.getConnection().createStatement();
    ResultSet rs;

    rs = stmt.executeQuery("SELECT CURRENT_DATE");
    stmt.close();
    }

    static void connectToPg(String login, String password) throws Exception {

         Class classe = Class.forName(datasourceclassname);
         Object conDS = classe.newInstance();
         java.lang.reflect.Method metodo;
         Class argomenti[] = new Class[1];
         String args[] = new String[1];

         argomenti[0] = String.class;
         metodo = classe.getMethod("setServerName", argomenti);
         args[0] = dbservername;
         metodo.invoke(conDS, args);

         if (dbserverportnumber != null) {
             Integer iargs[] = new Integer[1];
             iargs[0] = new Integer(dbserverportnumber);

             try {
                 argomenti[0] = Class.forName("java.lang.Integer");
                 metodo = classe.getMethod("setPortNumber", argomenti);
                 metodo.invoke(conDS, iargs);
             }
             catch (Exception e) { /* this only works in mySQL driver */ }

             try {
                 argomenti[0] = int.class;
                 metodo = classe.getMethod("setPortNumber", argomenti);
                 metodo.invoke(conDS, iargs);
             }
             catch (Exception e) { /* this work on all other drivers */ }
         }

         argomenti[0] = String.class;
         metodo = classe.getMethod("setDatabaseName", argomenti);
         args[0] = dbname;
         metodo.invoke(conDS, args);

         try {
             argomenti[0] = String.class;
             metodo = classe.getMethod("setDriverType", argomenti);
             args[0] = "thin";
             metodo.invoke(conDS, args);
         }
         catch (Exception e) { /* funziona solo con oracle e fallisce con gli altri */ }

         argomenti[0] = String.class;
         metodo = classe.getMethod("setUser", argomenti);
         args[0] = login;
         metodo.invoke(conDS, args);

         argomenti[0] = String.class;
         metodo = classe.getMethod("setPassword", argomenti);
         args[0] = password;
         metodo.invoke(conDS, args);

         try {
             metodo = classe.getMethod("getURL", null);
             args[0] = (String)(metodo.invoke(conDS, null));
         }
         catch (Exception e) {
             try {
                 metodo = classe.getMethod("getUrl", null);
                 args[0] = (String)(metodo.invoke(conDS, null));
             }
             catch (Exception ee) {
                 args[0] = null;
             }
         }
         if (args[0] != null)
             System.err.println("getUrl() = " + args[0]);

    connectionPoolDataSource = (ConnectionPoolDataSource)conDS;
    }
}

pgsql-jdbc by date:

Previous
From: Kris Jurka
Date:
Subject: Re: Column size BUG with text/bytea with 7.4 JDBC Driver
Next
From: Oliver Jowett
Date:
Subject: Re: problem in connetting using reflection