Java, Sql Server & encodings

  • Thread starter Electric Natcho
  • Start date
E

Electric Natcho

Hi, I'm working on an application that uses JBoss 3.2, Tomcat 4.1,
IIS, SQL Server 2000 and the MS JDBC driver. It's a typical web app
where the client can use a browser to save data to the database & then
view it later.

Our servlets respond using utf-8 so I'm assuming subsequent requests &
data from the client will be encoded using utf-8 as well. According to
the SQL Server documentation, unicode is saved using the ucs-2
encoding and utf-8 is not supported
(http://support.microsoft.com/default.aspx?scid=kb;en-us;232580). I've
also read that Java uses ucs-2 internally.

Will I have to explicitly convert encodings for all parameters (that
came from the client) when using prepared statements to insert into
the database? Is this conversion normally left up to the JDBC driver
(our driver has an option to use unicode instead of the default db
encoding)?
 
A

Anton Spaans

When you read strings from the database, they will be returned as
java.lang.String objects by the JDBC drivers.

DataBase --JDBC--> java.lang.String --Servlet-Response--> UTF8-byte-array
UTF8-byte-array --Servlet-Request--> java.lang.String --JDBC--> Database

When you finally write these Strings, use the Servlet's Writer
(response.getWriter()):

String stringDataToWrite = .....

Writer sWriter = pResponse.getWriter();
pResponse.setContentType("text/html; charset="+HTML_ENC);
sWriter.write(stringDataToWrite);

Where the HTML_ENC is the name/id of the desired encoding. E.g. HTML_ENC =
"UTF-8".
The Servlet container should take care of the proper byte-array encoding.
Note that you need to call 'setContentType(...)' before writing any data.
-- Anton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top