Wednesday, November 26, 2008

Finding Differences in two DB Tables

During my work i switch between a lot of databases and many a time an application which is working fine in one db might not work the same in a different db.Many a times it gives some error saying "Invalid Identifier",We get an "Invalid Identifier" error in sql when we are trying to query a column from a table which does not have it.(In this case the column exists in one db not in other.)
Here I'm posting a simple sql query which gives a simple difference of two database table.
It checks the column name,data type,length and precision.
The script is meant to find the differences of two tables in the same DB. You can extend the script to find differences in tables from different databases simply by creating a db link from one db to the other db.

 
--- Combined query
-- common columns
(select '1_SAME' as COL_TYPE,
e1.column_name T1_column_name, e1.data_type T1_data_type,e1.data_length t1_data_length,
e1.data_precision t1_data_precision,
e2.column_name T2_column_name, e2.data_type T2_data_type,e2.data_length t2_data_length,
e2.data_precision t2_data_precision
from all_tab_columns e1 , all_tab_columns e2
where e1.table_name='EMP'
and e2.table_name='EMP2'
and e1.column_name = e2.column_name
and e1.data_type = e2.data_type
and e1.data_length = e2.data_length
and e1.data_precision = e2.data_precision)
UNION
-- indifferent columns
(select '2_DIFF' as COL_TYPE,
e1.column_name T1_column_name, e1.data_type T1_data_type,e1.data_length t1_data_length,
e1.data_precision t1_data_precision,
e2.column_name T2_column_name, e2.data_type T2_data_type,e2.data_length t2_data_length,
e2.data_precision t2_data_precision
from all_tab_columns e1 , all_tab_columns e2
where e1.table_name='EMP'
and e2.table_name='EMP2'
and (e1.column_name = e2.column_name
and (e1.data_type <> e2.data_type
OR e1.data_length <> e2.data_length
OR e1.data_precision <> e2.data_precision)))
UNION
-- TAB 1 COLUMNS
(select '3_TAB1' as COL_TYPE,
e1.column_name T1_column_name, e1.data_type T1_data_type,e1.data_length t1_data_length,
e1.data_precision t1_data_precision,
null T2_column_name, null T2_data_type,null t2_data_length,
null t2_data_precision
from all_tab_columns e1
where e1.table_name='EMP'
and e1.column_name NOT IN (SELECT e2.column_name FROM all_tab_columns e2 WHERE e2.table_name='EMP2'))
UNION
-- TAB 2 columns
(select '4_TAB2' as COL_TYPE,
null T1_column_name, null T1_data_type,null t1_data_length,
null t1_data_precision,
e2.column_name T2_column_name, e2.data_type T2_data_type,e2.data_length t2_data_length,
e2.data_precision t2_data_precision
from all_tab_columns e2
where e2.table_name='EMP2'
and e2.column_name NOT IN (SELECT e1.column_name FROM all_tab_columns e1 WHERE e1.table_name='EMP'));


Individual queries

select * from emp;
select * from emp2;

select * from all_tab_columns where table_name in ('EMP','EMP2');
select emp.ename , emp2.ename
from emp , emp2
where emp = emp2

-- common columns
select 'SAME' as COL_TYPE,
e1.column_name T1_column_name, e1.data_type T1_data_type,e1.data_length t1_data_length,
e1.data_precision t1_data_precision,
e2.column_name T2_column_name, e2.data_type T2_data_type,e2.data_length t2_data_length,
e2.data_precision t2_data_precision
from all_tab_columns e1 , all_tab_columns e2
where e1.table_name='EMP'
and e2.table_name='EMP2'
and e1.column_name = e2.column_name
and e1.data_type = e2.data_type
and e1.data_length = e2.data_length
and e1.data_precision = e2.data_precision

-- indifferent columns
select 'DIFF' as COL_TYPE,
e1.column_name T1_column_name, e1.data_type T1_data_type,e1.data_length t1_data_length,
e1.data_precision t1_data_precision,
e2.column_name T2_column_name, e2.data_type T2_data_type,e2.data_length t2_data_length,
e2.data_precision t2_data_precision
from all_tab_columns e1 , all_tab_columns e2
where e1.table_name='EMP'
and e2.table_name='EMP2'
and (e1.column_name = e2.column_name
and (e1.data_type <> e2.data_type
OR e1.data_length <> e2.data_length
OR e1.data_precision <> e2.data_precision))

-- TAB 1 COLUMNS
select 'TAB1' as COL_TYPE,
e1.column_name T1_column_name, e1.data_type T1_data_type,e1.data_length t1_data_length,
e1.data_precision t1_data_precision,
null T2_column_name, null T2_data_type,null t2_data_length,
null t2_data_precision
from all_tab_columns e1
where e1.table_name='EMP'
and e1.column_name NOT IN (SELECT e2.column_name FROM all_tab_columns e2 WHERE e2.table_name='EMP2');

-- TAB 2 columns
select 'TAB2' as COL_TYPE,
null T1_column_name, null T1_data_type,null t1_data_length,
null t1_data_precision,
e2.column_name T2_column_name, e2.data_type T2_data_type,e2.data_length t2_data_length,
e2.data_precision t2_data_precision,
from all_tab_columns e2
where e2.table_name='EMP2'
and e2.column_name NOT IN (SELECT e1.column_name FROM all_tab_columns e1 WHERE e1.table_name='EMP');

-- Start of DDL Script for Table FUSION.EMP
-- Generated 24-Nov-2008 18:25:32 from FUSION@XE

CREATE TABLE emp2
(empno NUMBER(4,0) NOT NULL,
ename VARCHAR2(10),
job VARCHAR2(12),
mgr2 NUMBER(6,0),
hiredate DATE,
sal NUMBER(7,2),
comm2 NUMBER(7,2),
deptno NUMBER(2,0))
PCTFREE 10
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
MINEXTENTS 1
MAXEXTENTS 2147483645
)
NOCACHE
MONITORING
/
-- End of DDL Script for Table FUSION.EMP

Saturday, September 20, 2008

Simple and free Database for Development - Choose Oracle XE or MySQL

Any Web Application, may be that of a moderate size or a huge one needs a DataBase to maintain data.A Database makes a Developer/User/Maintenance person job easier as the data is always seperate from your application.Its a a simple file that is storing your data.You can always retreive your data with the code that you write and is completely up to us on how we get our data .. and most of all its fast and is secure.
Comming to the selection of a database that you need for your development i suggest the developers to take up Oracle XE database which is free version of oracle DB or MySql which is again a free open source database.
Here are some links which might help you.

http://www.oracle.com/technology/products/database/xe/index.html
http://oss.oracle.com/oracle-database-xe.html

http://www.mysql.com/
http://en.wikipedia.org/wiki/MySQL

Jar Explorer

http://jarbrowser.sourceforge.net/
http://sourceforge.net/projects/jarbrowser/



I recently had an encounter with the JarBrower s/w on the sourceforge.net and that was which could help me a lot at time.
A give a simple explanation a jar browser is something very simmilliar to that of a winzip.With winzip you can browser through all the files in the zip files.Simmillarly with jar broswer you can browse through all the class files in a jar file.No just that , jar browser had a an in build jad the famous open source java decompiler with it.You can not only browse through the jar file for the class files but also see the source code with in that.
However is felt a gap in this s/w ,we can see the src in a class file, but the we are not able to see the other simple text files like xml, properties file in the browser.Also we cannot view the images.I feel adding this feature would make the Jar Explorer more powerful and more and more useful.



Sunday, September 14, 2008

JAR - Command Reference

Common JAR file operations

To create a JAR file
jar cf jar-file input-file(s)

To view the contents of a JAR file
jar tf jar-file

To extract the contents of a JAR file
jar xf jar-file

To extract specific files from a JAR file
jar xf jar-file archived-file(s)

To run an application packaged as a JAR file
(requires the Main-class manifest header)
java -jar app.jar

To invoke an applet packaged in a JAR file to be displayed in your web page,put the following Tags.


<applet height="height" archive="JarFileName.jar" width="width" code="AppletClassName.class">
<param name="_cx" value="26">
<param name="_cy" value="26">
</applet>



Ref:http://java.sun.com/docs/books/tutorial/deployment/jar/

Tweaking MS Windows Command Prompt

Change the Font in the Command Prompt
http://smallvoid.com/article/winnt-cmd-add-font.html
http://phatness.com/node/1643

Add the "Open Command Prompt Here" Option in Windows Exploper
http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm

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);
}

Syntax Highlighting the Source Code in Html/Blogs

Your text here...

<pre name="code" class="sql">
put your code here
</pre>

rest of ur text goes here...

Best Posts
http://en.blog.wordpress.com/2007/09/03/posting-source-code/
http://faq.wordpress.com/2007/09/03/how-do-i-post-source-code/


More...
http://blog.felho.hu/posting-source-code-in-wordpress-escaping-and-syntax-highlighting-the-inserted-code.html