Amazon

Tuesday, October 6, 2009

Storing Multicountry character sets in DB - ISO-8599-1, UTF-8 ETC

Use the function below to store the String or value in DB in Encoded form.

void encodeStringInPstmt(PreparedStatement pstmt, int index, String st) throws IPHException{
if (st== null ){
st= "" ;
}
InputStream inp = new ByteArrayInputStream(st.getBytes());

Reader reader = null ;

try {
//Correct encoding type need to be set before storing in DB. No need to change the DB field datatype from Varchar2 to NVarchar2.
reader = new BufferedReader( new InputStreamReader(inp, "UTF-8" ));
pstmt.setCharacterStream(index, reader, st.length());

} catch (UnsupportedEncodingException e) {
log .fatal( "encodeStringInPstmt() | UnsupportedEncodingException = " +e);
} catch (SQLException s){
log .fatal( "encodeStringInPstmt() | SQLException = " +s);
}
}


This function converts the encoding type of the value to be printed on JSP. The value retrieved from DB, which was stored using function above. The value need to be converted to correct encoding type before printing on JSP. Don't forget to set the encoding type of JPS to UTF-8.

public static final String utf8Convert(String utf8String) throws java.io.UnsupportedEncodingException {

if (utf8String== null ){
utf8String = "" ;
}
byte [] bytes = new byte [utf8String.length()];
for ( int i = 0; i < utf8String.length(); i++) {
bytes[i] = ( byte ) utf8String.charAt(i);
}

String outputString = new String(bytes, "UTF-8" );
return outputString;
}

Monday, October 5, 2009

Easy to Play with Unicode Chars

package test.unicode;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OdbcAccessConnection {
/**
* Method to set convert the String to CharacterStream so that it could be stored in DB with proper
* Unicode Conversion
* @param pstmt
* @param st
* @throws SQLException
*/
static void encodeStringInPstmt(PreparedStatement pstmt, String st) throws SQLException{
InputStream inp = new ByteArrayInputStream(st.getBytes());
Reader reader = new InputStreamReader(inp) ;
pstmt.setCharacterStream(1, reader, st.length());
//Closing the readers
try {
reader.close();
inp.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void main(String [] args) {
Connection con = null;
Statement stmt = null;
ResultSet rset = null;
PreparedStatement pstmt = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;

// Connect with a url string
con = DriverManager.getConnection("jdbc:odbc:HY_ACCESS");
System.out.println("Connection ok.");

//String with Unicode ISO-8859-1 unicode characters
String st = "5 && eev ç Ü Ø Å sanj Character À Á 11 Â Ã Ä Å Æ Ç È É Ê Ë Ì Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ø ù ú û ü ý þ ÿ";
pstmt = con.prepareStatement("insert into test (charset) values (?)");
for (long i = 0; i <>
st = i+" && eev ç Ü Ø Å sanj Character À Á 11 Â Ã Ä Å Æ Ç È É Ê Ë Ì Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ø ù ú û ü ý þ ÿ";
encodeStringInPstmt(pstmt, st); //Special treatment to Unicode Char String
pstmt.executeUpdate(); //Storing in table
}
stmt = con.createStatement();
rset = stmt.executeQuery("select * from test");
File f = new File("c:\\sanjeev\\unicode.txt");
//Reading the Unicode string from DB and storing in file.
FileOutputStream fout = new FileOutputStream(f);
while(rset.next()){
String s = new String(rset.getBytes("charset"));//Read in form of Bytes. It will come as it is in DB.
System.out.println(s);
fout.write(s.getBytes());
fout.write('\n');

}
fout.close();

} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}
finally{
try {
pstmt.close();
rset.close();
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}

Amazon Best Sellors

TOGAF 9.2 - STUDY [ The Open Group Architecture Framework ] - Chap 01 - Introduction

100 Feet View of TOGAF  What is Enterprise? Collection of Organization that has common set of Goals. Enterprise has People - organized by co...