JDBC types vs postgres types correspondation

Lists: pgsql-jdbc
From: "Marco Solinas" <solinas(dot)marco(at)gmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: JDBC types vs postgres types correspondation
Date: 2008-05-10 17:14:13
Message-ID: e90f35740805101014k6358c096l5064054791eac901@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Dear list,

We have a problem with the JDBC types. We need to find out the
correspondation table between jdbc types and postgresql type.
What we're trying to do is to build a CREATE TABLE query where a column has
to be of type "character varying", but we're not able to find the
corresponding JDBC type.
Here you have the related part of out Java code:

*******************************************************************************************************************
public String getTargetType (int jdbctype) throws SQLException {
targettype.beforeFirst(); // targettype is the ResultSet obtained by
DatabaseMetaData
while (targettype.next()) {
if (targettype.getInt ("DATA_TYPE") == jdbctype) {
return targettype.getString("TYPE_NAME");
}
}
return null;
}

public void creaTabellaCommesse () {
String sql = "CREATE TABLE Commesse (";

try {
String dbChar = getTargetType(Types.VARCHAR);
sql += "ID_Commessa " + getTargetType(Types.INTEGER) + " PRIMARY
KEY, ";
sql += "Prodotto " + dbChar + ", ";
sql += "Finitura " + dbChar + ", ";
sql += "DenominazioneUso " + dbChar + ", ";
sql += "DestinazioneUso " + dbChar + ", ";
sql += "PaeseDiDestinazione " + dbChar + ")";

int n = st.executeUpdate(sql); //the Statement st is allocated
elsewhere of my code
}
catch (SQLException ex) {
eccezione (ex);
}
}
****************************************************************************************************************

The jdbc driver we downloaded is postgresql-8.3-603.jdbc4.jar; the
postgresql server version we installed is 8.3.
Our code doesn't provide me any error, but the table columns are of type
"name", which is too short in length to be effective in storing the most
part of our program string.
Any idea of what we're missing?

Thanks a lot for your support.
Regards,
Marco and Rita.


From: Kris Jurka <books(at)ejurka(dot)com>
To: Marco Solinas <solinas(dot)marco(at)gmail(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: JDBC types vs postgres types correspondation
Date: 2008-05-12 07:05:39
Message-ID: Pine.BSO.4.64.0805120302190.15690@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

On Sat, 10 May 2008, Marco Solinas wrote:

> We have a problem with the JDBC types. We need to find out the
> correspondation table between jdbc types and postgresql type.
> What we're trying to do is to build a CREATE TABLE query where a column has
> to be of type "character varying", but we're not able to find the
> corresponding JDBC type.

You are getting hit by a bug in the JDBC driver. The output of
DatabaseMetaData.getTypeInfo is supposed to be sorted by closest match to
DATA_TYPE, but it is not. So you're just getting a random match for a
datatype that can represent text.

Kris Jurka