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

Introducing BNotepad 1.0.1.0

Writing an Application software on my own was a dream for me since the day one I started working with the software.Here I present you my first step towards it..
The BNotepad 1.0.1.0.



BNotepad is very simmilliar to the Notepad application that you find on the Microsoft windows.The difference being BNotepad is written by me... :).
BNotepad is written completely in java using JFC/Swing.The basic source code is taken from http://www.Planet-Source-Code.com/, Thanks to the developer for providing the skeleton code.
The difference that you find in the BNotepad is an additional Developer menu with menu items like
Convert JDBC to TNS
Convert TNS to JDBC
Wrap Sql View Definition and so on...
These are very specific functions which do some special tasks.
For example the "Convert JDBC to TNS" is a function which converts a given TNS String to a JDBC String.This is one of the very frequently made action for developers working on java and sql and BNotepad is Intended to make their life Simple.

I will be providing all the source code of the BNotepad online so the developers intresed in extending the tool or intrested to write their own functions can be ready to do that.
Any Suggestions/Comments/Bugs can be posted as the comments to this blog post.

You can use the BNotepad in two ways
1.Install the BNotepad as another other software using this link
or
2.Download the Jar file and run it directly.
You can choose to go whatever way you feel easy.

Have a happy coding.... :)

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

Monday, September 1, 2008

My Hello world JSP... :)

Here is a short tutorial of how to create a simple hello world servlet and deploy it in a ApacheTomcat Server.
This is intended for the people who are very new to the JSP and servlets.
Ppl who are experts in the JSP and Servlets find this post very childish.... :D.

On an Overall view we will be doing the following simple steps.
Create a Severlet and its Deployment descripter.
Deploy it in a web server.
Run the page to test the servlet.

Step 1: Create a Project directory some where on your Hard drive.
Example: Create the main project directory HelloWorldApp and three subdirectories with name classes,etc and src respectively.



Step 2: Create a file with name HelloWorldServlet.java, You can copy paste the following code.Your are a java developer... so you can add in some more code here as well... :)




import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class HelloWorldServlet extends HttpServlet{

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{

PrintWriter out = response.getWriter();
java.util.Date today = new java.util.Date();
out.println("<html>"+
"<body>"+
"<h1 align=left>Hello World !!!</h1>"+
"<br>"+"Now the date and time is : "+today+"</body>"+"</html>");
}


}


Step 3: Create a deployment descriptor file i.e web.xml in the etc folder of your project.
Copy paste the following code.






HelloWorld Servlet
HelloWorldServlet


HelloWorld Servlet
/SayHello




Step 4:Compile your servlet classe and generate the class file under the classes folder of your project


F:\DevCore\HelloWorldApp>javac -classpath C:\ApacheTomcat6018\lib\servlet-api.jar -d classes src\HelloWorldServlet.java






Step 5: Create a folder HelloWorldApp under the Tomcat webapps folder.
Create a subfolder WEB-INF under the HelloWorldApp folder
Create a subfolder classes under the WEB-INF folder

Step 6: Copy the web.xml folder from your project directory ot the HelloWorldApp/WEB-INF folder under the Tomcat.
Copy the HelloWorldServlet.class file to the HelloWorldApp/WEB-INF/classes folder under the Tomcat.






Step 7: Start/Restart Apache Tomcat

Step 8: Enter the url "http://localhost/HelloWorldApp/SayHello" into your favourite Web browser...



Hurrah!!!.., here is your JSP Page running...

Ref : Chapter-1, Head First JSP and Servlets

Tuesday, April 22, 2008

Building a Intra Home Network by cross cabling.

It was had been years that i had been using PC and which gone quite old and that was the time i felt to buy a latest one.
Everthing went fine i bought a brand new PC home with whatever the later configuration that i could get till date.but then came the question.., what am i going to do with my old pc??, Im i going to through it away ???... definitely that is not a better option as still being old my old machine works great.. with a Intel p IV processor and a 1GB ram and 80HDD,dvd writer.., everthing well and good.Then came the idea how about putting my new pcs into a network and play around a little of network stuff and learn a little bit of how we can hack on machines.. :D.
Then i was searching on alternatives.., i found that simple cross cabling would be the best option since it only requires a crossed ethernet wire with a little pc configuration and nothing else.You really save a lot of money by not buying a router for this purpose.
Use can use a thus formed network as a LAN and also for Internet sharing.
Here i give the simple steps how to build a intra home network in the linked doc
http://surnida.bhasker.googlepages.com/NetworkSettings.doc
The information about how to make a crossed ethernet cable can be found at the following links.
http://en.wikipedia.org/wiki/Ethernet_crossover_cable
http://www.homenethelp.com/web/explain/about-ethernet-crossover.asp
http://www.leeindy.com/connecting_two_computers_using_crossover_cables.shtml
http://www.experts-exchange.com/Networking/Q_20960308.html

Friday, March 14, 2008

Customizing editplus with java tools...

Customizing the applications to the way i need exactly had been an interest of me for a very long time..., I always feel that there is something always missing in the application that i'm using.., may be it is a ms word or photoshop or editplus or netbeans..
Then i felt that there is one way to go beyond this.., "plugins" or sometimes called extensions or user tools...
Here i'm going to give u a small example of how you can extend the functionality of editplus with the custom java plugins or tools that you write which makes ur life simpler...

Use Case: I'm going to use a simple use case which i felt was the most time consuming thing to do and which was a missing feature in the editplus.
Being a java developer most of the time i write a lot of System.out.println() (sop) statements in my java code which helps a lot in debugging and let me know how where my program is going.
At the same time it becomes a big burden for me when i have to remove these sops...
Here i'll give you an example of how you can remove the sop statments in a java source file with a custom tool..,in our case a simple java class file.(You can think of any other functionality that you might be concerned with may be adding or deleting content and anything more.., by allmeans you are playing with files...)

People who are familiar with editplus might be knowing how the editplus can be integrated with the java compiler and the java runtime.With the java compiler you can compile your java class file and with the java runtime you can actually run your compiled class file
more information on this please visit editplus faq:http://www.editplus.com/faq.html

Following are the snapshots on how you can do this..

In my scenario java is installed on my machine at the following location..
D:\Program Files\Java\jdk1.6.0_01
Location of javac.exe
D:\Program Files\Java\jdk1.6.0_01\bin\javac.exe
Location of java.exe
D:\Program Files\Java\jdk1.6.0_01\bin\java.exe

Java Compiler settings:

























Java Runtime settigs:


























These two settings help you compile a java src file and run it..., what we are looking for is writing a tool of our own and configure it with ediplus.
First we start with writing a java src file with exactly does the action we wanted to do.., place the whole logic in the main method and with the arguments if required.
Here im giving you the example i tried, the logic for my tool in RemoveSOP.java
(Click on the image for a better view to open it in a different window or tab)


/*
* RemoveSOP.java
*
* Created on March 15, 2008, 11:40 AM
*
* To change this template, choose Tools Template Manager
* and open the template in the editor.
*/

/**
*
* @author Bhasker
*/
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;

public class RemoveSOP {

/** Creates a new instance of RemoveSOP */
public RemoveSOP() {
}

public static void main(String[] args) throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;

try {
inputStream =
new BufferedReader(new FileReader(args[0]));
outputStream =
new PrintWriter(new FileWriter(args[0]+"_Modified"));

String l;
while ((l = inputStream.readLine()) != null) {
//System.out.println(l);
if(l.contains("System.out.println(")){

while (!(l.contains(");"))){
l = inputStream.readLine();
}
}
else{
outputStream.println(l);
System.out.println(l);
}
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}

}
}

}

Here is how you configure the JavaTool class file with its editplus as a user tool...


Here is my java source file MyJavaSrcFile.java with which im going to test for my javaplugintool...



/*
* MyjavaSrcFile.java
*
* Created on March 15, 2008, 11:47 AM
*
* To change this template, choose Tools Template Manager
* and open the template in the editor.
*/

/**
*
* @author Bhasker
*/
public class MyjavaSrcFile {

/** Creates a new instance of MyjavaSrcFile */
public MyjavaSrcFile() {
}

public static void main(String args[]){

System.out.println("****************************************************************************\n" +
" xdebug : " + " Welcome to the dummy program... :D ");
int num1 = 4;
int num2 = 5;
int num3;

System.out.println("****************************************************************************\n" +
" xdebug : " + " Ready to do the multiplication of "+num1+" with " +
""+num2+" times");

System.out.println("Result ... : "+num1+" X "+num2+" = "+num1*num2+"\n");

num3 = num1*num2;

System.out.println("****************************************************************************\n" +
" xdebug : " + " Have a nice day good bye.... :) ");


}

}



The log is as follows...



/*
* MyjavaSrcFile.java
*
* Created on March 15, 2008, 11:47 AM
*
* To change this template, choose Tools Template Manager
* and open the template in the editor.
*/

/**
*
* @author Bhasker
*/
public class MyjavaSrcFile {

/** Creates a new instance of MyjavaSrcFile */
public MyjavaSrcFile() {
}

public static void main(String args[]){


int num1 = 4;
int num2 = 5;
int num3;




num3 = num1*num2;




}

}

With the my java source file opened i click on the java tool button or ctrl+ so as to run the tool on this file.This will create a new file +_Modifed with all the sops removed which i can replace the whole of the source in my file after i confirm the changes with a compare tool like beyond compare.

Here are the results that you can compare....
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikfE_wcTaH_no_CTSvPfL4Q-Zw_pVBJq5FA-O90mp8LE3eplJ9yWr34uLT4xG8RQX0Wtu5NqmLg8nK8sbwuHlKUVqr6Isy4DiZl9Nc4JmpwBDxb1fdJoHzp-mqyKnvkYhJLPZxp3JldTo/s1600-h/compare.PNG
























He he..,the sops are removed in just second....
I came to hear from ppl that there are something called macros which does the same work more simply..., I'm yet to make my hands dirty with that....
Also the way i mentioned is not just limited to java tools but you can configure any other exe or jar or any file in a similar way..