To continue with my first and second JDBC article, Today I found something interesting in Java which I never read about.

Actually we want to provide an interface which can be used to create,modify or even delete tables in Oracle.We needed such interface based design for ease in administration and flexibility. In order to achieve this,I've created required configuration form with Column name,column data type,column width.Along with that admin can select action,like Create,Alter or Delete table.It looks something like,




While writing Java routine for the same interface found first interesting thing is "Java doesn't support String in Switch statement", Can you believe it ?? While compiling my routine with below switch statement , got an error "Incompatible type".

Document cdoc=entry.getDocument();
String frm=cdoc.getItemValueString("Form");
Switch(frm){
Case "Form1":
Table11 t1 = new Table1();
t1.ManageTable(conn,db,cdoc);
break;
Case "Form2":
Table12 t2 = new Table2();
t2.ManageTable(conn,db,cdoc);
break;
}


I've tried all the way but never doubted until my wife pointed me Java doesn't support Strings in Switch case.It was shock for me because, I always believe Java gives everything which you need to build powerful application.I've got some workaround on net like using enum or hashcode , but both are mapping based solution where program can crash if mapping went wrong. Finally I've replaced Switch with If..else statement :).

Second interesting thing was string comparison in Java.When I've compile my routine with below code snippet,

Document cdoc=entry.getDocument();
String frm=cdoc.getItemValueString("Form");
if(frm=="Form1"){
Table11 t1 = new Table1();
t1.ManageTable(conn,db,cdoc);
}
else if(frm=="Form3"){
Table12 t2 = new Table2();
t2.ManageTable(conn,db,cdoc);
}


Neither I see my code creating new table in Oracle nor any error.After some debugging found the valid reason,

To compare Strings for equality, don't use ==. The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value (have exactly the same characters in them). Use the .equals() method to compare strings for equality. Similarly, use the .compareTo() method to test for unequal comparisons..

Finally I've changed my code to ,

Document cdoc=entry.getDocument();
String frm=cdoc.getItemValueString("Form");
if ("Form1".equals(frm)){
Table11 t1 = new Table1();
t1.ManageTable(conn,db,cdoc);
}
else if ("Form2".equals(frm)){
Table12 t2 = new Table2();
t2.ManageTable(conn,db,cdoc);
}


So here are two very basic problems encountered by me while building my application.I hope this could be useful to other Lotus Notes developer to overcome such minor technology differences.

To be continued....

Leave a Reply

preload preload preload