Search

Google


How do i increase performance of Java Desktop application dbase con...



Hi :

I am currently working with desktop application , is there any way to capture the data from the database and store it temporarily and use it wherever necessary in the Desktop application.

I mean something that works similar to cache or session in the web application , i need a solution for desktop application.

Thanks in Advance,
silviya




# Solution 1


Hi joanesilviyaj,

  I have attached a snip code that function closely to web session.
It uses hashtable to store data. And you can save the hashtable to
a text file before the application exit and read it again when the
application start.



CODE Snippet :


import java.util.*;

import java.io.*;
import java.beans.*;
class AppSession
{
// save session to a file
static public Hashtable<String, String> getSession(String file) throws Exception
{
FileInputStream fstream = new FileInputStream(file);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String strLine = null;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
sb.append(strLine);
//Close the input stream
in.close();
// decode to hashtable
ByteArrayInputStream bis = new ByteArrayInputStream(sb.toString().getBytes());
XMLDecoder decoder = new XMLDecoder(bis);
Hashtable<String, String> session = (Hashtable<String, String>)decoder.readObject();
decoder.close();
return session;
}
// read session from a file
static public void setSession(Hashtable<String, String> session, String fileName) throws Exception
{
// encode to xml
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(bos);
encoder.writeObject(session);
encoder.close();
String encode = new String(bos.toByteArray());
// ensure parent directory exist
File file = new File(fileName);
String parentPath = file.getParent();
if( parentPath!=null )
{
File fileParent = new File(parentPath);
fileParent.mkdirs();
}
// create file
FileWriter fstream = new FileWriter(fileName);
BufferedWriter out = new BufferedWriter(fstream);
out.write(encode);
out.close();
}
static public void main(String arg[]) throws Exception
{
Hashtable<String, String> session = new Hashtable<String, String>();
session.put("Key1","Value1");
session.put("Key2","Value2");
session.put("Key3","Value3");

// for debugging: before
System.out.println(session);
// save session
setSession(session, "c:/session.txt");
// read session
session = getSession("c:/session.txt");

// for debugging: after
System.out.println(session);

}
}




# Solution 2


joanesilviyaj,

A message has been sent to some of our more experienced experts asking them to review your question. We will check back again to see if you are getting the help you need.

This request included Experts from Open Source Programming, Database, Windows Programming, Programming Languages.

Please do not respond to this comment; we will be monitoring your question for activity from the Experts.

Thank you for using Experts Exchange,

ModernMatt
Moderator, Experts Exchange
http://www.experts-exchange.com/Q_23967014.html


# Solution 3


Use a cache, such as ehcache for maintaining a local copy.



# Solution 4


Are you maintaining the connection?  Or using a connection pool?

Maybe it's not required

Tim

PS:  If you want and need a cache, objects has pointed you to one of the best ;-)


# Solution 5


Hello bluebelldiscovery:

Thanks for the reply.Let me work on this code and see is that works for my Java desktop Applicaiton.

Thanks a lot for taking a step to reply me...!!!!!!!

silviya


# Solution 6


Hi Silviya

You are welcome. Let me know if you have any doubt.