Friday, October 14, 2011

Performance in JDBC

http://www.precisejava.com/javaperf/j2ee/JDBC.htm#JDBC110

I found this very useful link on net.
I wrote a program to grab 5million rows of data from DB and to store it on my harddrive.
It took almost 12 hours

I used the following function from ResultSet to improve the performance.
resultSet.setFetchSize(2000);

By default the FetchSize is 10 rows, By varying size you see the performance change.

Below are the statistics for pulling 100000 rows
setFetchSize      Minutes
10                      12
2000                  4.5
20000                2.6
100000              java.lang.OutOfMemoryError: requested array is larger than heap

Saturday, June 25, 2011

CachedRowSet

 ResultSet resultSet = stmt.executeQuery(query);  
 CachedRowSet crs = new CachedRowSetImpl(); //com.sun.rowset.CachedRowSetImpl  
 crs.populate(resultSet);  

Access DB Connection from standalone application

 public static Connection getDatabaseConn() throws NamingException{  
      Context ctx=null;  
      Connection conn = null;       
      Properties env = new Properties();   
      env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");   
      env.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001");   
      ctx = new InitialContext(env);   
      try {  
           dataSource = (DataSource) ctx.lookup("ExtDevDB");     //jndi datasource name  
      } catch (NamingException e) {  
           e.printStackTrace();  
      }  
      try{  
      conn = dataSource.getConnection();  
      }catch(Exception e){  
           e.printStackTrace();  
      }finally{  
      }  
      return conn;       
 }  

Use t3 protocol to connect Weblogic server.

Sunday, May 22, 2011

Document to String

 DOMSource domSource = new DOMSource(document); //org.w3c.dom.Document document = null;  
 StringWriter writer = new StringWriter();  
 StreamResult result = new StreamResult(writer);  
 TransformerFactory tranFact = TransformerFactory.newInstance();  
 Transformer transfor = tranFact.newTransformer();  
 transfor.transform(domSource, result);  
 String returnXML=writer.toString();  

Add CDATA in JavaScript

 var newCdata=dataXmlDom.createCDATASection("data to append");  
 newContextElem.appendChild(newCdata);  

Tuesday, April 5, 2011

XSLT on Client

 function xmlTranform(){  
      var xml=file.xml;  
      var xsl=file.xsl;  
      if(window.ActiveXObject)   
      {  
           ex=xml.transformNode(xsl);  
           jDivObj.append(ex);  
      }else if(document.implementation && document.implementation.createDocument){  
           var xsltProcessor=new XSLTProcessor();  
           xsltProcessor.importStylesheet(xsl);  
           var resultDocument=xsltProcessor.transformToFragment(xml,document);  
           var i=resultDocument.firstChild.innerHTML;  
           $("#testDiv").html(i);                                 
      }  
      tb_show("xslt", "#TB_inline?&height=485&width=750&inlineId=testDiv","");// thickbox overlay  
      $("#testDiv").empty();  
 }  

Saturday, March 19, 2011

RegExp in JavaScript

 <script type="text/javascript">  
 var str = "mastertablemastertext";  
 var patt1 = /master/i;  
//i is modifier for case-insensitive and g for Global search
 alert(str.match(patt1));  
 </script>  

Arrays in Javascript

 function start() {  
      var example=new Array();  
      example[0]="first";  
      example[1]="esc";  
      for(var i=0;i<example.length;i++){  
           alert(example[i]);  
      }  
      example[example.length]="third";  
      alert(example.length+" "+example["esc"]);  
 }  

Thursday, January 27, 2011

Split a String in Unix

 name="TEST_THE_CODE"  
 var=$(echo $name | awk -F "THE" '{print $1 $2}')  
 set -- var  
 echo $1  
 echo $2  

 output would be  
 TEST_  
 _CODE  

Saturday, January 22, 2011

Parsing using javascript

SAX parser links:
http://code.google.com/p/jssaxparser/

Dom parser links:
http://www.w3schools.com/Dom/dom_parser.asp

Degrees Minutes Seconds to Decimal Degrees

 public class LongLat {  
      public static void main(String args[]){  
           double LongDouble=0;  
           double direction=0;  
           int checkDegree=0;  
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
           String input=null;  
           System.out.println("Enter Longitude or Latitude");  
           try{  
                input=br.readLine().toUpperCase();  
           }catch(Exception e){  
                System.out.println("IO error");  
                System.exit(1);  
           }            
           Pattern p=Pattern.compile("[NEWS]");  
           Matcher m=p.matcher(input);            
           if(input.contains("N") || input.contains("E"))  
                direction=1;  
           else if(input.contains("W") || input.contains("S"))  
                direction=-1;  
           if(input.contains("N") || input.contains("S"))  
                checkDegree=90;  
           else if(input.contains("W") || input.contains("E"))  
                checkDegree=180;  
           input=m.replaceFirst("");  
           input=input.trim();  
           String[] LongDMS=input.split(" ");  
           if(!(Integer.parseInt(LongDMS[0])<=checkDegree)){  
                System.out.println("Degree should be less than "+checkDegree);  
                System.exit(1);  
           }  
           if(!(Integer.parseInt(LongDMS[1])<=60)){  
                System.out.println("Minutes should be in between 0 and "+60);  
                System.exit(1);  
           }  
           if(!(Integer.parseInt(LongDMS[2])<=60)){  
                System.out.println("Second should be in between 0 and "+60);  
                System.exit(1);  
           }  
           if(LongDMS.length==3)  
                LongDouble=Double.valueOf(LongDMS[0])+((Double.valueOf(LongDMS[1])*60)+Double.valueOf(LongDMS[2]))/3600;  
           LongDouble=LongDouble*direction;  
           System.out.println(LongDouble);  
      }  
 }  

 Enter Longitude or Latitude  
 W87 43 41  
 -87.72805555555556  

Tuesday, January 18, 2011

CKEditor

link:
http://docs.cksource.com/CKEditor_3.x/Developers_Guide