Saturday, September 13, 2008

Convert TNS to JDBC String or TNS String to JDBC

For the developers who costantly work with the sql editor and then the java code (jsp or servlets), changing the connections string from tns to JDBC or JDBC to tns is a very common exercise .
Here im presenting some code to do this conversion.
Hope this will be help for some of u...

Convert JDBC to TNS

public void convertJDBC2TNS(){
System.out.println("In My button action listener");

//str is something like
//jdbc:oracle:thin:@ADDRESS:PORT:DBNAME
//we want the output to be something like
//XE=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=XE)))


String str = n.getTextArea().getText();
String str2= new String();
System.out.println("My text is : \n"+str);

for(int i=0;i(lessthan)str.length();i++){
char c = str.charAt(i);
if(c==' '||c=='\t'||c=='\n'){
// do nothing
}
else{
char cl = java.lang.Character.toLowerCase(c);
char[] charArray ={cl};
str2 = str2.concat( new String(charArray));
}
}

System.out.println("String = "+str2+"\nlength = "+str2.length());
//XE=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=XE)))

int hostIndex = str2.indexOf(":@");
int portIndex = str2.indexOf(":", hostIndex+1);
int sidIndex = str2.indexOf(":", portIndex+1);
System.out.println("hostIndex = "+ hostIndex+"\n portIndex = "+ portIndex+"\n sidIndex = "+ sidIndex );

int closingIndex;
closingIndex = str2.indexOf(":", hostIndex+1);
String host = str2.substring(hostIndex+2, closingIndex);
System.out.println("host = "+host);

closingIndex = str2.indexOf(":", portIndex+1);
String port = str2.substring(portIndex+1, closingIndex);
System.out.println("port = "+port);

//closingIndex = str2.indexOf(")", sidIndex);
String sid = str2.substring(sidIndex+1);
System.out.println("sid = "+sid);

//jdbc:oracle:thin:@ADDRESS:PORT:DBNAME
String tnsString = sid.toUpperCase()+"=(DESCRIPTION="+"\n\t"+
"(ADDRESS=(PROTOCOL=tcp)(HOST="+host+")(PORT="+port+"))"+"\n\t"+
"(CONNECT_DATA=(SID="+sid.toUpperCase()+"))"+"\n\t"+
")";
System.out.println("My text after conversion is : \n"+tnsString);
n.getTextArea().setText(tnsString);
}



Convert TNS to JDBC


public void convertTNS2JDBC(){
System.out.println("In My button action listener");

//your str is something like
//XE=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=XE)))
//and you want to convert it to
//jdbc:oracle:thin:@ADDRESS:PORT:DBNAME

String str = n.getTextArea().getText();
String str2= new String();
System.out.println("My text is : \n"+str);

for(int i=0;i(lessthan)str.length();i++){
char c = str.charAt(i);
if(c==' '||c=='\t'||c=='\n'){
// do nothing
}
else{
char cl = java.lang.Character.toLowerCase(c);
char[] charArray ={cl};
str2 = str2.concat( new String(charArray));
}
}

System.out.println("String = "+str2+"\nlength = "+str2.length());
//mz5ms202=(description=(address=(protocol=tcp)(host=ap6005rems)(port=1575))(connect_data=(sid=mz5ms202))

int hostIndex = str2.indexOf("host");
int portIndex = str2.indexOf("port");
int sidIndex = str2.indexOf("sid");
System.out.println("hostIndex = "+ hostIndex+"\n portIndex = "+ portIndex+"\n sidIndex = "+ sidIndex );

int closingBraceIndex;
closingBraceIndex = str2.indexOf(")", hostIndex);
String host = str2.substring(hostIndex+5, closingBraceIndex);
System.out.println("host = "+host);

closingBraceIndex = str2.indexOf(")", portIndex);
String port = str2.substring(portIndex+5, closingBraceIndex);
System.out.println("port = "+port);

closingBraceIndex = str2.indexOf(")", sidIndex);
String sid = str2.substring(sidIndex+4, closingBraceIndex);
System.out.println("sid = "+sid);

//jdbc:oracle:thin:@ADDRESS:PORT:DBNAME
String jdbcString = "jdbc:oracle:thin:@"+host+":"+port+":"+sid+"";
System.out.println("My text after conversion is : \n"+jdbcString);
n.getTextArea().setText(jdbcString);
}

No comments: