Wednesday, June 24, 2009

Insert Double Byte Characters to Database

We cannot insert the double byte characters (eg: Chinese, Russian, Japanese etc) into a database directly. This is not possible even if we configure the database to Unicode encoding.

All the editors we use to run the sql does not recognize the double byte characters. They support only ASCI characters. So if we try to run a query which contains a double byte character, say Chinese, then the editor will replace the characters with question marks (???) means it does not recognize the symbol.

For Eg: if we copy the following query to an sql editor,

insert into () values ('支付宝');

then the editor will convert it as

insert into () values ('???');


There is a solution for this issue.

We can use the sql function compose() and we can pass the unicode of the characters to the function.

Eg: insert into () values (compose(unistr('\652F\4ED8\5B9D')));

This will insert the double character to the db.

The characters can be converted to the unicode using the following site
http://rishida.net/scripts/uniview/conversion.php


.

Friday, June 12, 2009

How to generate class-path variable in Manifest file of a jar

The project has dependency on some external jar files. To create a build file in such a way that build.xml doesn’t need to be modified when a new jar file is added to the lib folder.

For that we need to generate Class-Path attribute in the MANIFEST.MF dynamically. The following code shows how to do that.


1. Define a path element say, classpath

<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

The above code will set the absolute path of all the jar files in the lib folder to the path element 'classpath'

2. Convert the path element values into relative path and store it in a string property, say jar.classpath

<pathconvert property="jar.classpath" refid="classpath" pathsep=" ">
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>


3. Use the property 'jar.classpath' to define the manifest file while creating the jar.

<target name="jar" depends="clean,compile">
<mkdir dir="${jar.dir}"/>
<mkdir dir="${jar.lib.dir}"/>
<!-- Copying the external libraries to build/jar/lib folder. -->
<copy todir="${jar.lib.dir}">
<fileset dir="${lib.dir}" excludes="**/*.svn"/>
</copy>
<jar destfile="${jar.dir}/${jar.file}" basedir="${classes.dir}" excludes="**/*.svn">
<manifest>
<attribute name="Class-Path" value=". ${jar.classpath}" />
</manifest>
</jar>
</target>

Note :- The lib directory with the external jar files should be in the same directory as that of the project jar file.

We can't put the external jar files inside the project jar files with the above method.

Tuesday, June 9, 2009

Reusability of Thread.

Can you resuse a Thread Object? Can you restart it by calling start() again?

No. Once a thread's run() method has completed, the thread can never be restarted. In fact, at that point the thread moves into a state - dead. In dead state, the thead has finished its run()method and can never be restarted. The Thread object might still be on the heap, as aliving object that you can call other methods on (if appropriate), but the Thread object has permanently lost its 'threadness'. In other words, there is no longer a seperate call stack, and the Thread object is no longer a thread. It's Just an object, at that point, like all other objects.

But there are design patterns for making a pool of threads that you can keep using to perform different jobs. But you don't do it by retsarting a dead thread.

How to Implement a scheduler in java

Use java.util.Timer and java.util.TimerTask to Implement a scheduler in java.

The following are all the Timer methods you can use to schedule repeated executions of tasks:

schedule(TimerTask task, long delay, long period)
schedule(TimerTask task, Date time, long period)
scheduleAtFixedRate(TimerTask task, long delay, long period)
scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

When scheduling a task for repeated execution, use one of the schedule methods when smoothness is important and a scheduleAtFixedRate method when time synchronization is more important.

schedule method is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run.

While in scheduleAtFixedRate, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up."

Call Timer.cancel() to stop the Timer.

Example:

public class AnnoyingBeep {
Toolkit toolkit;
Timer timer;

public AnnoyingBeep() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1000); //subsequent rate
}

class RemindTask extends TimerTask {
int numWarningBeeps = 3;

public void run() {
if (numWarningBeeps > 0) {
toolkit.beep();
System.out.format("Beep!%n");
numWarningBeeps--;
} else {
toolkit.beep();
System.out.format("Time's up!%n");
//timer.cancel(); //Not necessary because
//we call System.exit
System.exit(0); //Stops the AWT thread
//(and everything else)
}
}
}
...
}

To Move a File from One directory to another in Java

We can use File.renameTo() method to move a file from one folder to another.

// File (or directory) to be moved
File file = new File("filename");

// Destination directory
File dir = new File("directoryname");

// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));

if (!success) {
// File was not successfully moved
}