Lists: | pgsql-jdbc |
---|
From: | "Sean Duffy" <sean(dot)duffy(at)programmer(dot)net> |
---|---|
To: | "Paulo Merson" <paulo(at)summa-tech(dot)com>, <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | Re: Handling null values in char fields from JDBC |
Date: | 2002-01-22 18:34:18 |
Message-ID: | 20020122183419.35472.qmail@mail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-jdbc |
-----Original Message-----
From: "Paulo Merson" <paulo(at)summa-tech(dot)com>
Date: Tue, 22 Jan 2002 09:46:44 -0500
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: [JDBC] Handling null values in char fields from JDBC
Paulo -
I am using ResultSet.getString but Im not
sure how I can use ResultSet.wasNull. Is
there an example I could look at?
Thanks :)
Sean -
> Are you using ResultSet.getString and then ResultSet.wasNull?
>
> Paulo Merson
> Summa Technologies - www.summa-tech.com
>
> -----Original Message-----
> From: pgsql-jdbc-owner(at)postgresql(dot)org
> [mailto:pgsql-jdbc-owner(at)postgresql(dot)org] On Behalf Of Sean Duffy
> Sent: Tuesday, January 22, 2002 9:22 AM
> To: pgsql-jdbc(at)postgresql(dot)org
> Subject: [JDBC] Handling null values in char fields from JDBC
>
>
>
> I am running Postgresql 7.1.3 and using the 7.1-1.2 jar jdbc driver. I
> noticed that when I used a ResultSet.getString("charfield") call to
> fetch null data from the char field I get a null pointer exception
> error. My question is two fold..
> 1) Is this normal behavior for the jdbc driver?
> 2) Is there a recommended way of trapping for a null value in a char
> field (besides just going into the db with psql and setting all null
> values to '')?
>
> I have looked thru two different Postgresql manuals but have not been
> able to find anything referring to null value handling at the jdbc
> level.
>
> If anyone can pt me in the right direction I
> would really appreciate it.
>
> Thanks!
> --
>
> _______________________________________________
> Sign-up for your own FREE Personalized E-mail at Mail.com
> http://www.mail.com/?sr=signup
>
>
> 1 cent a minute calls anywhere in the U.S.!
>
> http://www.getpennytalk.com/cgi-bin/adforward.cgi?p_key=RG9853KJ&url=htt
> p://www.getpennytalk.com
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
>
--
_______________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup
1 cent a minute calls anywhere in the U.S.!
http://www.getpennytalk.com/cgi-bin/adforward.cgi?p_key=RG9853KJ&url=http://www.getpennytalk.com
From: | "Paulo Merson" <paulo(at)summa-tech(dot)com> |
---|---|
To: | <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | Re: Handling null values in char fields from JDBC |
Date: | 2002-01-22 19:11:38 |
Message-ID: | 002d01c1a378$9e602a70$c501a8c0@PAULO |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-jdbc |
You should use wasNull after each getXxx method if the column being read
allows null. It's as simple as:
text = rs.getString(2);
if (rs.wasNull()) {
System.out.println("This column is SQL null");
} else {
// process data
}
It's particularly useful when you are reading values to basic data type
variables (not objects) because there is no value that uniquely
represents the SQL null. For example getInt returns 0 (zero) if the
value is null but it would also return 0 when the value is zero.
(ResultSet.getString must return null when the column value is null; I'm
using it in Posrgres JDBC driver and it works fine.)
Check
http://java.sun.com/j2se/1.4/docs/api/java/sql/ResultSet.html#wasNull()
Paulo Merson
Summa Technologies - www.summa-tech.com
-----Original Message-----
From: pgsql-jdbc-owner(at)postgresql(dot)org
[mailto:pgsql-jdbc-owner(at)postgresql(dot)org] On Behalf Of Sean Duffy
Sent: Tuesday, January 22, 2002 1:34 PM
To: Paulo Merson; pgsql-jdbc(at)postgresql(dot)org
Subject: Re: [JDBC] Handling null values in char fields from JDBC
-----Original Message-----
From: "Paulo Merson" <paulo(at)summa-tech(dot)com>
Date: Tue, 22 Jan 2002 09:46:44 -0500
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: [JDBC] Handling null values in char fields from JDBC
Paulo -
I am using ResultSet.getString but Im not
sure how I can use ResultSet.wasNull. Is
there an example I could look at?
Thanks :)
Sean -
> Are you using ResultSet.getString and then ResultSet.wasNull?
>
> Paulo Merson
> Summa Technologies - www.summa-tech.com
>
> -----Original Message-----
> From: pgsql-jdbc-owner(at)postgresql(dot)org
> [mailto:pgsql-jdbc-owner(at)postgresql(dot)org] On Behalf Of Sean Duffy
> Sent: Tuesday, January 22, 2002 9:22 AM
> To: pgsql-jdbc(at)postgresql(dot)org
> Subject: [JDBC] Handling null values in char fields from JDBC
>
>
>
> I am running Postgresql 7.1.3 and using the 7.1-1.2 jar jdbc driver.
I
> noticed that when I used a ResultSet.getString("charfield") call to
> fetch null data from the char field I get a null pointer exception
> error. My question is two fold..
> 1) Is this normal behavior for the jdbc driver?
> 2) Is there a recommended way of trapping for a null value in a char
> field (besides just going into the db with psql and setting all null
> values to '')?
>
> I have looked thru two different Postgresql manuals but have not been
> able to find anything referring to null value handling at the jdbc
> level.
>
> If anyone can pt me in the right direction I
> would really appreciate it.
>
> Thanks!
> --
>
> _______________________________________________
> Sign-up for your own FREE Personalized E-mail at Mail.com
> http://www.mail.com/?sr=signup
>
>
> 1 cent a minute calls anywhere in the U.S.!
>
>
http://www.getpennytalk.com/cgi-bin/adforward.cgi?p_key=RG9853KJ&url=htt
> p://www.getpennytalk.com
>
>
>
> ---------------------------(end of
broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
majordomo(at)postgresql(dot)org
>
>
> ---------------------------(end of
broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
>
--
_______________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup
1 cent a minute calls anywhere in the U.S.!
http://www.getpennytalk.com/cgi-bin/adforward.cgi?p_key=RG9853KJ&url=htt
p://www.getpennytalk.com
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster
From: | "Sulakshana Awsarikar" <sulakshana(at)mithi(dot)com> |
---|---|
To: | <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | Getting exact string length from char fields in JDBC |
Date: | 2002-01-23 13:02:58 |
Message-ID: | 008a01c1a40e$48244b00$0500a8c0@vsnl.net.in |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-jdbc |
Hi
I have recently shifted from using MySQL to PostgreSQL. I have a field in my
table which is of type char(10) .
When I do a getString() for this field using JDBC, I get a string of 10
characters even if the actual data is say 5 characters in length. The
remaining 5 characters are spaces.
This was not happening while using MySQL.
How can I get the actual data without spaces when I do a getString() without
having to add any kind of trimming code in the Java code.
regards
Sulaksana
From: | Ola Sundell <ola(at)miranda(dot)org> |
---|---|
To: | Sulakshana Awsarikar <sulakshana(at)mithi(dot)com> |
Cc: | pgsql-jdbc(at)postgresql(dot)org |
Subject: | Re: Getting exact string length from char fields in JDBC |
Date: | 2002-01-23 13:28:46 |
Message-ID: | Pine.LNX.4.21.0201230826060.3011-100000@miranda.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-jdbc |
On Wed, 23 Jan 2002, Sulakshana Awsarikar wrote:
> Hi
>
> I have recently shifted from using MySQL to PostgreSQL. I have a field in my
> table which is of type char(10) .
>
> When I do a getString() for this field using JDBC, I get a string of 10
> characters even if the actual data is say 5 characters in length. The
> remaining 5 characters are spaces.
>
> This was not happening while using MySQL.
>
> How can I get the actual data without spaces when I do a getString() without
> having to add any kind of trimming code in the Java code.
>
> regards
> Sulaksana
You can either change the datatype to VARCHAR, or add a TRIM() to the
column in the select statement.
Ola
--
Ola Sundell
ola(at)miranda(dot)org - olas(at)wiw(dot)org - ola(dot)sundell(at)personalchemistry(dot)com
http://miranda.org/~ola