Posts Tagged ‘Swing’

javax.imageio.ImageIO and JTDS JDBC Driver to the Rescue

October 19, 2010 1 comment

A friend of mine back home in Nigeria, a VB developer hollered at me sometimes last month to ask how he can retrieve blob data containing images stored in a MS SQL database and save them to the local disk. The database contains over 6,000 records. I’m sure there is a way to achieve this in VB, apparently he does not know. . . I completed the task in minutes. . .

It’s been a long time I worked with MSSQL and I think I only used it once since I graduated from school in 2003. Don’t ask me why, ask MySQL. . .lol

First, I don’t have MSSQL installed on my laptop which means I have to write a database independent code which should work for both MySQL and MSSQL. .

I quickly setup a database, created a table and populated it some of my pictures. At the end, I had 30 records in my table. First, we run our select query. .

//To store the image ID
String imageID;

// To store the binary stream retrieved from the database
InputStream in;

//Path to save Image on local disk
String imagePath="c:\pictures\";
File file;

//Will be used to store the image;
ImageIcon icon;
Image image;

PreparedStatement st=cn.prepareStatement("select ImageID, BlobImage from tblImages");
ResultSet rs=st.executQuery();

	//Store the retrieved image ina byte array
	byte[] b= new byte[1024];

	//Store the bytes in java.awt.Image object

	//Convert java.awt.Image to javax.swing.ImageIcon;

	icon= new Imageicon(image);

	//Convert the java.awt.Image to a BufferedImage
	BufferedImage bi= new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
	Graphics2d g2d= bi.createGraphics(icon.getImage(),0,0,null);

	//Create the file name. Our encoding is JPEG
	imageName= imageID+".jpg";
	file= new File(imagePath+imageName);

	//Now lets write out the image to our file path
	ImageIO.write(bi, "jpg", file);


This worked perfectly well with mySQL but there was a problem when he used it with MS SQL. He complained that only one image is retrieved and saved, and then the ResultSet closes. I figured out that the relationship between JDBC and MSSQL is not a rosy one. I tried JTDS driver, repackaged the application and sent to him. It worked perfect and retrieved the 6000 images in the database in 25 minutes but there was another problem. he could not log in to MSSQL with windows authentication. I sent him a dll file that was shipped with the JTDS package to paste in c:\wondows\system32 and that fixed it.

Below is the UI


GuiGenie – My Choice for Swing UI Design

September 2, 2010 4 comments

I have been doing desktop application development with the Swing Framework for over six years. In those six years, my focus has always been to develop applications that are rich in functionalities and with nice looking GUIs.

NetBeans, Eclipse, JCreator, JBuilder et al are all wonderful IDEs in fact; I love NetBeans. I use NetBeans for almost all my web projects because it makes all the processes from development to deployment as easy as it can get. Thanks to my friends Segun and Tope for helping me find my way around the IDE.

As much as I enjoy using NetBeans for web applications development, it’s the opposite for desktop development. Reasons? Well I will discuss that in later posts reasons why I prefer GuiGenie in designing my user interfaces.


Developed by Mario Awad, GuiGenie is a Java Gui builder that is light (less than 400KB) and very easy to use. It’s free too and lets you create Guis in minutes. Plus it has a very simple UI,

GuiGenie Screen

The Code View

GuiGenie Code View

Some Of My Works With The Genie

FRO (Fund Release Order) For The Federal Government



JStore – Fixed Asset Management Solution

JStore Main Screen

New Supply

CareWare – Hospital Management Solution

Careware Screen

JInventory – Accounts Receivables System

JInventory Screen

Customer Screen

JEmployee – Payroll, HR, Electronic Filing and Pension Management Solutio

JEmployee - Payroll, HR, Electronic Filing and Pension Management Solution

JEmployee Salay Screen

Electronic Filing Screen

Quiet Office – Intranet Collaboration App

Electronic Filing Screen


Looking at the screen shots of my apps, you will discover that most of the components used are not found in GuiGenie (refer the GuiGenie screen shot above). This of course is a limitation of the builder and if Mario releases the source code hopefully, I will include those components like Tables, Trees and some custom components I developed.

The trick is to replace your intending component with either a list or a JTextArea. Place the JTextArea in the exact width, height and location you wish to place your table, tree etc. When you are done, generate your source code and edit the generated code to suit your requirements. Copy and paste the code wherever you desire or in any other IDE of your choice or Notepad. Code your event listeners and viola!!!. . .Your application is ready.

So I am using this opportunity to reach out to Mario to release the source code so that I and other users of GuiGenie can contribute to its development. At least we have all in one way or the other benefited from using the builder, it’s time to contribute. . .
Wow. . It’s already half past 2 am here in Addis Ababa. . .time to go sleep. . Oh. . We are in the last 10 days of Ramadan fast so I better observe some prayers before going to bed . . . that’s after a cup of hot Cappuccino. . .lol

Don’t forget to leave your comments. . .

Categories: Java Tags: , , ,