Problem with retrieve ResultSet.getBytes("columnName");

Hi....Can I specify the encoding with ResultSet.getBytes("columnName") method.If not then how can I specify the desired encoding while fetching the values from DB?Thanks
[204 byte] By [Java_persona] at [2007-11-27 4:58:01]
# 1

Bytes are bytes are bytes and are not characters. You can create a String from the bytes usingString value = new String(ResultSet.getBytes("columnName") ,"the desired encoding");

but I would used String value = ResultSet.getString("columnName");

so that the JDBC driver takes care of it.

sabre150a at 2007-7-12 10:13:47 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

You only need encoding when you want to convert it to or from a String. And I really shouldn't use getString(), you are never sure of the type encoding applied (which is generally the one from the JVM). Rather use the constructor of String which accepts the charset.

byte[] bytes = resultSet.getBytes("columnname");

String encodedString = new String(bytes, charset);

The database-side encoding capabilities only applies to String fields (Varchar, Text, etc) by the way. You don't need to care them for the byte arrays.

BalusCa at 2007-7-12 10:13:47 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

THxs.

I have already tried ur defined solutions but I couldnt got desired answer.

Actually I am trying to Store the Urdu Language(National language of Pakistan) bytes in to DB.

When I retrieve it , I retrieve it in the following manner

byte[] unicode = ResultSet.getBytes("Column name");

String st = new String(unicode, "UTF-8");

jTextArea1.setText(st);

but this displays garbage to me.

I have the Urdu font installed on my computer.

I have also set the Urdu font for the JTextArea using setFont().

What should I do to accomplish my task?

Message was edited by:

Java_person

null

Java_persona at 2007-7-12 10:13:47 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

> What should I do to accomplish my task?

>

First of all prove that the bytes stored in the database are the Urdu bytes you expect. There is not point in worrying about how to get them from the database until you are certain that you have store what you think you have stored.

Again, why are you using bytes and not characters?

sabre150a at 2007-7-12 10:13:47 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

> jTextArea1.setText(st);

>

> but this displays garbage to me.

Then you need start by understanding that the above code has nothing to do with JDBC and that that code itself could be the problem.

All of the following are possible problem

- The input data is wrong.

- You are storing it wrong

- The driver does something to it when storing

- The database does something to it when storing

- The database does something to it when retreiving

- The driver does something to it when retreiving

- You are retrieving it wrong.

- You are displaying it wrong.

jschella at 2007-7-12 10:13:47 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

> Again, why are you using bytes and not characters?

I have tried everything that I could do.

I have also tested with characters as well.

For storing character I did the following steps

STEP-1

Change the column datatye to NVARCHAR

STEP-2

To reterieve I use the following code

ResultSet.getString("column name");

When I call getString("columnName") on the DB column which is NVARCHAR, it returns null even though a value exists there.

Is there any driver problem or what? But I dont think so that driver can be the problem...

What to do next...... ?

Where am I going wrong...?

Java_persona at 2007-7-12 10:13:47 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...