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.
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.
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
> 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?
> 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.
> 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...?