Skip to main content


Showing posts from February, 2010

New LookAndFeel in JDK7

In next release of JDK i.e. JDK 7, it contains many new features.
One of them is addition of new LookAndFeel - Nimbus.
Following screenshot shows the demo of Nimbus LookAndFeel.

You can download the Source Code for the demo which is shown in above picture.
Download - LookAndFeelDemo
For running this demo you must have JDK7 installed on your system.

Reading console output in Java

Have you ever needed to read console output from a Java program??
here is a simple code which shows how to read console output -

import*; class ConsoleReader { public static void main(String args[]) throws Exception { Process p=Runtime.getRuntime().exec("ping"); BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); String str=""; while(str!=null) { System.out.println(str); str=br.readLine(); } br.close(); } }

Loading Class which is not in Classpath

Java reflections gives us the ability to load a class, but using simple class loaders you can load a class which is there in classpath. But how to load the class which is not there in classpath?
Well, its not that hard.
URLClassLoader is there for us. Look at following code, which shows how to use URLClassLoader.
File file=new File("path to you directory in which your class is"); URL urls[]={ file.toURI().toURL() }; URLClassLoader loader=new URLClassLoader(urls); Class object=loader.loadClass("classname");

Importing Excel data to PostgreSQL

When it comes to transfer data from excel worksheet to PostgreSQL, the task is not that easy.
I tried to export file to CSV but it doesn't work because there were some columns which were not containing the data!!!
And even one problem exist, Column type. In Java using ResultSet I successfully fetched data from excel as ODBC database but in some column where it contains text in some rows while in some rows it contained numbers!! So Java 'getString(column)' method did not worked!!
So finally I imported that excel file into MS-Access and it worked, and then from there I read that data and inserted into PostgreSQL.
To fetch column from excel file as ODBC backend, following code will be useful.
select * from [WorksheetName$] To fetch only some data
select * from [WorksheetName$A7:F10] or
select First_row_data from [WorksheetName$] or if your excel file contains data in all columns then you can export it as CSV file and then can use PostgreSQL's COPY command.


Improving Performance in XML parsing

Yesterday I was building an application which was parsing xml files, after writing xml parsing code using XML API provided by JAVA, I found that my application is taking so much amount of time to parse a single file!!!
But when I wrote a simple XML file and parsed it using my application, it parsed within a second.
After reading xml files which I needed to parse I found that, those were containing DTD definition on web, and that was the reason why my application was taking so much amount of time to parse them.
So I changed my code for parsing xml in which I disabled DTD lookup on web. So while building an application which parses xml files, disable DTD lookup until you really need to follow them.

This will surely increase performance of your application.

using DatabaseMetaData

Java provides two classes "DatabaseMetaData" and "ResultSetMetaData" which helps a lot while retrieving data about tables in SQL.
So how to use these classes?
First establish connection with database
Class.forName(driver); Connection con=DriverManager.getConnection(url,user,password);
After establishing connection with database we can retrieve data about database
DatabaseMetaData dbmd=con.getMetaData();
After this to get the table list from database we can use following method

String types[]={ "TABLE" }; ResultSet rs=dbmd.getTables(con.getCatalog(),null,null,types); while( {  Vector v=new Vector();  System.out.println(rs.getString("TABLE_NAME")); }