Thread: I have a problem of abstraction...

I have a problem of abstraction...

From
Alfredo Rico
Date:
Hi friends, greetings.
Please apologize by my bad english ;-(

I need to resolve a problem of data type abstraction using JDBC.

In first place, please check out the following java code fragment:
/********************************/
//for each row that I want to insert
for(int i=1 ; i<= this.columnNumbers ; i ++)
{  
   this.insertColumn( columnNames[i] , columnValues[i] , i  );

}
/******************************/
In the code above, for each row that I want to insert:
columnNames is a String vector containing columns names for a given table.
columnValues is a String vector containing the values for each column for the same table.
Ok, this string vector could contain int, float, double, Date, character string, I mean basic types,
represented as string. The reason is due to this vector is fulfilled by users parameters
that has been sent via HttpServletRequest and method request.getParameter("parameter")
return its values as string.


Now, please look at the following java method:

//For each row that I want to insert:
public void insertColumn(String columnName, Object X , int i )
{
   try{
        if(i == 1) //only for the first column
           this.rs.moveToInsertRow();            
             
         this.rs.updateObject(columnName , X );  
           
         if( i == this.columnNumber )//if all columns (for one row) were updated
            this.rs.insertRow();               

        }catch(SQLException e )
         {
             System.out.println("Error: "+e);
         }        
}


Ok, if I'm using JDBC driver version 7.4 Build 216 there isn't problem with the data types.
The method ResultSet.updateObject works perfectly inserting Int, float, double, Date.
Evrything works very well.

But I need use JDBC driver verion 8.0 Build 312 for another reasons, and the java code above not works
because this version of JDBC driver is very strict about data types.

I really need insert my data in an abstract way, I mean without knowing the data types.
(Application requirements). I would not like to use a bulk of if setences for various data types
according to obtained by using ResultSetMetaData.

How could I overcome this situation ?

Beforehand thank you very much by your help and support.

Kind Regards.
Alfredo Rico.

Re: I have a problem of abstraction...

From
Dave Cramer
Date:
Alfredo,

The simplest and quickest way is to force v2 protocol

http://jdbc.postgresql.org/documentation/80/connect.html#connection-
parameters

Dave
On 6-Oct-05, at 9:27 AM, Alfredo Rico wrote:

> Hi friends, greetings.
> Please apologize by my bad english ;-(
>
> I need to resolve a problem of data type abstraction using JDBC.
>
> In first place, please check out the following java code fragment:
> /********************************/
> //for each row that I want to insert
> for(int i=1 ; i<= this.columnNumbers ; i ++)
> {
>    this.insertColumn( columnNames[i] , columnValues[i] , i  );
>
> }
> /******************************/
> In the code above, for each row that I want to insert:
> columnNames is a String vector containing columns names for a given
> table.
> columnValues is a String vector containing the values for each
> column for the same table.
> Ok, this string vector could contain int, float, double, Date,
> character string, I mean basic types,
> represented as string. The reason is due to this vector is
> fulfilled by users parameters
> that has been sent via HttpServletRequest and method
> request.getParameter("parameter")
> return its values as string.
>
>
> Now, please look at the following java method:
>
> //For each row that I want to insert:
> public void insertColumn(String columnName, Object X , int i )
> {
>    try{
>         if(i == 1) //only for the first column
>            this.rs.moveToInsertRow();
>
>          this.rs.updateObject(columnName , X );
>
>          if( i == this.columnNumber )//if all columns (for one row)
> were updated
>             this.rs.insertRow();
>
>         }catch(SQLException e )
>          {
>              System.out.println("Error: "+e);
>          }
> }
>
>
> Ok, if I'm using JDBC driver version 7.4 Build 216 there isn't
> problem with the data types.
> The method ResultSet.updateObject works perfectly inserting Int,
> float, double, Date.
> Evrything works very well.
>
> But I need use JDBC driver verion 8.0 Build 312 for another
> reasons, and the java code above not works
> because this version of JDBC driver is very strict about data types.
>
> I really need insert my data in an abstract way, I mean without
> knowing the data types.
> (Application requirements). I would not like to use a bulk of if
> setences for various data types
> according to obtained by using ResultSetMetaData.
>
> How could I overcome this situation ?
>
> Beforehand thank you very much by your help and support.
>
> Kind Regards.
> Alfredo Rico.


Re: I have a problem of abstraction...

From
Alfredo Rico
Date:
Thanks Dave, but could I switch to v2 just when I need it and then back to v3 afterwards whitout open a new connection ?
I can't use more than one connection per user becaues I'm using a pool conection.
Besides I'm using a ResultSet to update my database and so the conection already were established.


Kind Regards.-
Alfredo Rico.


On 10/6/05, Dave Cramer <pg@fastcrypt.com> wrote:
Alfredo,

The simplest and quickest way is to force v2 protocol

http://jdbc.postgresql.org/documentation/80/connect.html#connection-
parameters

Dave
On 6-Oct-05, at 9:27 AM, Alfredo Rico wrote:

> Hi friends, greetings.
> Please apologize by my bad english ;-(
>
> I need to resolve a problem of data type abstraction using JDBC.
>
> In first place, please check out the following java code fragment:
> /********************************/
> //for each row that I want to insert
> for(int i=1 ; i<= this.columnNumbers ; i ++)
> {
>    this.insertColumn( columnNames[i] , columnValues[i] , i  );
>
> }
> /******************************/
> In the code above, for each row that I want to insert:
> columnNames is a String vector containing columns names for a given
> table.
> columnValues is a String vector containing the values for each
> column for the same table.
> Ok, this string vector could contain int, float, double, Date,
> character string, I mean basic types,
> represented as string. The reason is due to this vector is
> fulfilled by users parameters
> that has been sent via HttpServletRequest and method
> request.getParameter("parameter")
> return its values as string.
>
>
> Now, please look at the following java method:
>
> //For each row that I want to insert:
> public void insertColumn(String columnName, Object X , int i )
> {
>    try{
>         if(i == 1) //only for the first column
>            this.rs.moveToInsertRow();
>
>          this.rs.updateObject(columnName , X );
>
>          if( i == this.columnNumber )//if all columns (for one row)
> were updated
>             this.rs.insertRow();
>
>         }catch(SQLException e )
>          {
>              System.out.println("Error: "+e);
>          }
> }
>
>
> Ok, if I'm using JDBC driver version 7.4 Build 216 there isn't
> problem with the data types.
> The method ResultSet.updateObject works perfectly inserting Int,
> float, double, Date.
> Evrything works very well.
>
> But I need use JDBC driver verion 8.0 Build 312 for another
> reasons, and the java code above not works
> because this version of JDBC driver is very strict about data types.
>
> I really need insert my data in an abstract way, I mean without
> knowing the data types.
> (Application requirements). I would not like to use a bulk of if
> setences for various data types
> according to obtained by using ResultSetMetaData.
>
> How could I overcome this situation ?
>
> Beforehand thank you very much by your help and support.
>
> Kind Regards.
> Alfredo Rico.


Re: I have a problem of abstraction...

From
Dave Cramer
Date:
Alfredo,

No, once you open the connection in v2 it stays in v2.

Dave
On 6-Oct-05, at 10:49 AM, Alfredo Rico wrote:

Thanks Dave, but could I switch to v2 just when I need it and then back to v3 afterwards whitout open a new connection ?
I can't use more than one connection per user becaues I'm using a pool conection.
Besides I'm using a ResultSet to update my database and so the conection already were established.


Kind Regards.-
Alfredo Rico.


On 10/6/05, Dave Cramer <pg@fastcrypt.com> wrote:
Alfredo,

The simplest and quickest way is to force v2 protocol

http://jdbc.postgresql.org/documentation/80/connect.html#connection-
parameters

Dave
On 6-Oct-05, at 9:27 AM, Alfredo Rico wrote:

> Hi friends, greetings.
> Please apologize by my bad english ;-(
>
> I need to resolve a problem of data type abstraction using JDBC.
>
> In first place, please check out the following java code fragment:
> /********************************/
> //for each row that I want to insert
> for(int i=1 ; i<= this.columnNumbers ; i ++)
> {
>    this.insertColumn( columnNames[i] , columnValues[i] , i  );
>
> }
> /******************************/
> In the code above, for each row that I want to insert:
> columnNames is a String vector containing columns names for a given
> table.
> columnValues is a String vector containing the values for each
> column for the same table.
> Ok, this string vector could contain int, float, double, Date,
> character string, I mean basic types,
> represented as string. The reason is due to this vector is
> fulfilled by users parameters
> that has been sent via HttpServletRequest and method
> request.getParameter("parameter")
> return its values as string.
>
>
> Now, please look at the following java method:
>
> //For each row that I want to insert:
> public void insertColumn(String columnName, Object X , int i )
> {
>    try{
>         if(i == 1) //only for the first column
>            this.rs.moveToInsertRow();
>
>          this.rs.updateObject(columnName , X );
>
>          if( i == this.columnNumber )//if all columns (for one row)
> were updated
>             this.rs.insertRow();
>
>         }catch(SQLException e )
>          {
>              System.out.println("Error: "+e);
>          }
> }
>
>
> Ok, if I'm using JDBC driver version 7.4 Build 216 there isn't
> problem with the data types.
> The method ResultSet.updateObject works perfectly inserting Int,
> float, double, Date.
> Evrything works very well.
>
> But I need use JDBC driver verion 8.0 Build 312 for another
> reasons, and the java code above not works
> because this version of JDBC driver is very strict about data types.
>
> I really need insert my data in an abstract way, I mean without
> knowing the data types.
> (Application requirements). I would not like to use a bulk of if
> setences for various data types
> according to obtained by using ResultSetMetaData.
>
> How could I overcome this situation ?
>
> Beforehand thank you very much by your help and support.
>
> Kind Regards.
> Alfredo Rico.



Re: I have a problem of abstraction...

From
"Kevin Grittner"
Date:
Greetings Alfredo,

Take a look at the JDBC spec.  In particular look at appendix B and
everything which is said about table B-2.  What you are attempting
is not supported by the standard.  Apparently, some drivers are more
lenient and support this even though the standard doesn't suggest
that it will work.

If you look at tables B-4 and B-5, you will see that the setObject
method of a PreparedStatement is supposed to handle some types
of conversions.  You might want to test this approach to see if the
defined conversions cover what you need and work as expected in
the PostgreSQL driver.

Another alternative would be for your generalized method to check
the ResultSetMetaData and create the appropriate object type for
the ResultSet.updateObject method.

I haven't tested that these methods work per standard, but on the
face of it, both techniques look like they should work.

If none of the above works, you might need to associate some sort
of custom metadata with your queries or web pages so that you can
create the appropriate object type for the setObject method.

I hope this helps.

-Kevin


>>> Alfredo Rico <alfredorico@gmail.com> 10/06/05 8:27 AM >>>
Hi friends, greetings.
Please apologize by my bad english ;-(

I need to resolve a problem of data type abstraction using JDBC.

In first place, please check out the following java code fragment:
/********************************/
//for each row that I want to insert
for(int i=1 ; i<= this.columnNumbers ; i ++)
{
this.insertColumn( columnNames[i] , columnValues[i] , i );

}
/******************************/
In the code above, for each row that I want to insert:
columnNames is a String vector containing columns names for a given table.
columnValues is a String vector containing the values for each column for
the same table.
Ok, this string vector could contain int, float, double, Date, character
string, I mean basic types,
represented as string. The reason is due to this vector is fulfilled by
users parameters
that has been sent via HttpServletRequest and method
request.getParameter("parameter")

return its values as string.


Now, please look at the following java method:

//For each row that I want to insert:
public void insertColumn(String columnName, Object X , int i )
{
try{
if(i == 1) //only for the first column
this.rs.moveToInsertRow();

this.rs.updateObject(columnName , X );

if( i == this.columnNumber )//if all columns (for one row) were updated
this.rs.insertRow();

}catch(SQLException e )
{
System.out.println("Error: "+e);
}
}


Ok, if I'm using JDBC driver version 7.4 Build 216 there isn't problem with
the data types.
The method ResultSet.updateObject works perfectly inserting Int, float,
double, Date.
Evrything works very well.

But I need use JDBC driver verion 8.0 Build 312 for another reasons, and the
java code above not works
because this version of JDBC driver is very strict about data types.

I really need insert my data in an abstract way, I mean without knowing the
data types.
(Application requirements). I would not like to use a bulk of if setences
for various data types
according to obtained by using ResultSetMetaData.

How could I overcome this situation ?

Beforehand thank you very much by your help and support.

Kind Regards.
Alfredo Rico.


Re: I have a problem of abstraction...

From
Marc Herbert
Date:
Alfredo Rico <alfredorico@gmail.com> writes:

> //for each row that I want to insert
> for(int i=1 ; i<= this.columnNumbers ; i ++)
> {  
>    this.insertColumn( columnNames[i] , columnValues[i] , i  );
> }

> //For each row that I want to insert:
> public void insertColumn(String columnName, Object X , int i )
> {
>              
>          this.rs.updateObject(columnName , X );  
>            

> I would not like to use a bulk of if setences for various data types
> according to obtained by using ResultSetMetaData.

Why not? You have to do this only once per ResultSet type.

I wrote something similar here:


https://forge.continuent.org/plugins/scmcvs/cvsweb.php/sequoia/src/org/continuent/sequoia/common/protocol/?cvsroot=sequoia

  in SQLDataSerialization#getSerializer()

The idea is that you, compute, store and then call back some abstract
"setters", one per column.

The advantage of typing earlier is that you catch input errors
earlier: in the driver instead of inside the engine.



Re: I have a problem of abstraction...

From
Marc Herbert
Date:
Marc Herbert <Marc.Herbert@emicnetworks.com> writes:

> The advantage of typing earlier is that you catch input errors
> earlier: in the driver instead of inside the engine.
>

I meant: inside your application instead of...

Earlier.