Hi Alpesh, everyone,
On Wed, 29 Sep 1999, ALPESH KOTHARI wrote:
> I have written one program in java2 to write the data
> in postgresql. I am storing one integer and one string
> in the database. When i read the string from the file
> it is proper. But when i store it, it gives the
> following exception:
> java.sql.SQLException: ERROR: parser: parse error at
> or near ""
>
> I am not able to detect the error. Can any one run the
> code and correct the problem?
> When I assign the string test1 some fixed value and
> store it then the program works fine.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This should give you a clue. This has *nothing* to do with JDBC, rather
your code to read the string from the file is incorrect! And also, your
code to write the string to the file is incorrect.
> Here is the code:
...
> pid1=din.readInt();
> System.out.println(pid1);
> int len1=din.readByte();
> byte[] lent=new byte[len1+2];
> for(int i=0;i<=len1+1;i++)
> {
> lent[i]=din.readByte();
insert here: System.out.print(lent[i]); System.out.print(" ");
> }
> test1=new String(lent);
> System.out.println(test1);
...
First of all, you can see that your loop is wrong. If len1 is the length
of the string, then the loop should read i < len1 NOT i<=len1+1, and the
byte array should be byte[] lent=new byte[len1].
Second of all, if you insert the print statements I note above, you will
find out that you have binary characters in your string! THis is what is
causing postgres to barf. Most likely, you have a hidden null or a
control character in it, which is why printing it doesn't show up.
I bet you used DataOutputStream.writeUTF() to write the string to the test
file in the first place. On a unix system, try looking at the output
using "od -c". On a Windows system, use a binary editor to look at the
test file. writeUTF() actually puts an int indicating the length of the
string first.
So instead of writeUTF() or whatever you're using, use
DataOutputStream.writeBytes() instead.
Or conversely, use readUTF() to read it back.
. . . Sean will charge you for the next question =)
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]