Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert DB2 binary data to UTF-8 at query level

Tags:

java

database

db2

I am connected to IBM DB2 database with java but data is stored as binary format in database so when I fetch any value it comes as binary or hexdecimal format. How can I convert this in binary data in utf-8 at query level.

Sample code to fetch data -

String sql = "SELECT poMast.ORDNO  from AMFLIBL.POMAST AS poMast ";
Class.forName("com.ddtek.jdbc.db2.DB2Driver");
String url = "jdbc:datadirect:db2://hostname:port;DatabaseName=dbName;";
Connection con = DriverManager.getConnection(url, "username","password");
PreparedStatement preparedStatement = con.prepareStatement(sql);
ResultSet rs = preparedStatement.executeQuery();
System.out.println("ResultSet : \n");
System.out.println(" VNDNO");
while (rs.next())
{
   System.out.println(rs.getString("ORDNO"));
}
like image 765
Milan Soni Avatar asked Oct 30 '22 16:10

Milan Soni


1 Answers

You probably need to use the CAST expression:

SELECT CAST(poMast.ORDNO as VARCHAR(50))  from AMFLIBL.POMAST AS poMast

Adjust the VARCHAR length to your needs. The string is in the database codepage (often UTF-8 these days) and converted to the client/application codepage when fetched.

like image 58
data_henrik Avatar answered Nov 09 '22 10:11

data_henrik