Tuesday, November 14, 2006

Spring JDBC connection

Download Spring.jar version 2.0

- configure the xml
- DriverManagerDataSource class help you to configuration and get the connection from database
- DataSourceTransactionManager class that help to create the transaction when you want to insert or update the database.
- Create your own Dao Class, it just like a class represent a table but i do not help you to maintain the Has-a relationship.

In my sample project (you can import to Eclipse)
App.java - contain main method to execute (but i am connecting SQL Server... you can teach the concert)
OrderDao - interface class to represent what method contain in OrderDaoImpl
OrderDaoImpl - contain code to process
Order - Order represent the table Orders in Database
OrderRowMap - It help you to mapping value from resultset to create the object instance

It contain the method call "public Object mapRow(ResultSet rs, int index)" from the name you know it for mapping.

Tuesday, October 24, 2006

Use single value to store multiple settings

example use binary compare:

static final int problem1 = 0x1;
static final int problem2 = 0x10;
static final int problem3 = 0x100;

- when user selected problem1 and problem2
we can store the value like that

int userSelVal;
userSelVal = userSelVal | problem1;
userSelVal = userSelVal | problem2;

-then you will ask how to know the user is selected problem1 and problem2

if((userSelVal & problem1) == problem1){
//represent user select problem1
System.out.println("User is selected problem1");
}

if((userSelVal & problem2) == problem2){
//represent user select problem2
System.out.println("User is selected problem2");
}

if((userSelVal & problem3) == problem3){
//represent user select problem3
System.out.println("User is selected problem3");
}

- via to compare the binary position value, each position represent one setting then we can store int binary size setting in one int.

Sunday, October 22, 2006

Create a signed jar

Create a signed Jar file

description:
need to use keytool and jarsigner

C:\> keytool -genkey -validity
C:\> keytool -selfcert -validity -keypass
C:\> jarsigner test.jar mykey

http://www.ccw.com.cn/htm/center/prog/02_7_8_4.asp
http://www.owasp.org/index.php/Signing_jar_files_with_jarsigner#Create_a_new_DSA_Key_Pair_for_Bob

Not all the detail, it will be edit later but you can use the upper command to finish it.

signed jar for web start application.

Ant jar file ( Do you remember the script? )

Very simple Ant to create jar file

*project name="XDoclet Generator" default="jar"*
*target name="jar" description="Create binary distribution after xxx"*
*!-- Create application JAR file --*
*mkdir dir="MyJarDir"*
*jar jarfile="MyJarDir/MyJarFile.jar" basedir="classes"*
*/jar*
*/mkdir*

Simple easy under stand but let me write down.... thx. I easy forget

Saturday, October 21, 2006

readExif php script

It is very simple sample only a few lines of code.

- description
It read the JPEG file's EXIF information, and use the DateTime value to separate the image file to different folder.

-execution
1. Set php installed folder in envirnment variable.
2. then you can directly type "php" to execute php script on local computer.
3. enable the in php.ini (it may locate on Windows / WINNT folder):
extension=php_mbstring.dll
extension=php_exif.dll

4. put readExif.php into the image folder.
5. type "php readExif.php"
6. It will process.

Thursday, October 19, 2006

convert number to English words

I am finding the sample code to convert number to words.
I find one java sample:

reference: site
my own copy and modify code: my modify source file

It is quite simple to convert, just use a little bit time to understand

I think it can help to ppl no need to convert number when write cheque.

Wednesday, October 18, 2006

one-to-one bidirectional (in hibernate example) xDoclet

create xDoclet tag both one side
1. assume each user only have one car

user class reference car
car class reference user

2. identify From & To direction

assume that user (from) - car (to)
then user primary key store to car table (foreign key)

3. xDoclet tag

== code ==
car class:
/**
* @hibernate.many-to-one
* class = "local.sample.oo.User"
* column = "userId"
*/
public User getUser()
{
return _user;
}
== code ==

car is to --> so that is many-to-one relationship (from logic it is one-to-one but if both side one to one cannot be identify to & from)
column -> represent the user primary key store column on car table

== code ==
user class:
/**
* @hibernate.one-to-one
* cascade = "save-update"
*/
public Car getCar()
{
return _car;
}
== code ==

user is from -> so that is one-to-one relationship
cascade -> when update user, it also request car to update (if car is modified)

Monday, October 16, 2006

Simple Sample to use Spring, xDoclet and Hibernate

I just revision how to use Hibernate, you need to understand about the one-to-one, one-to-many, many-to-many before use the Hibernate. Hibernate is not very complicate but you don't use any tools to help develop the application with Hibernate, you will feel hard.
In my experience, I suggest to use Eclipse 3.1.x & JBoss IDE plugins to develop the Hibernate and Spring application.

I will provide the simple project. It demo how to create one-to-one, one-to-many, many-to-many.
You need to pay attention on :
  1. persistent classes xDoclect's Hibernate tag.
  2. Spring configuration xml file ( hibernate.xml) - datasource, hbm config, bean of spring
  3. xDoclet configuration. Project > right click > property > XDoclect configuration
Hope this example can give you idea to use Spring and Hibernate.
** Sample download**

Useful resource to futher study Hibernate.
- HibernateGossip

Any problem, please feel free to contact me:
mingkit@gmail.com

Mylar is eclipse plugin for project management

Interest! I will read about it and write some article.
http://www-128.ibm.com/developerworks/java/library/j-mylar1/?ca=dgr-jw01Mylar-1

http://www.hibernate.org/43.html

MySQL add new user account

It is a very simple procedure but i usually forget. We also can search ther result from MYSQL homepage. Please login by root account.
mysql -u root

1. create user account.
- CREATE USER mingkit IDENTIFIED BY 'password';

2. grant right for the accout.
- for localhost
GRANT ALL PRIVILEGES ON *.* TO 'mingkit'localhost'IDENTIFIED BY 'password' WITH GRANT OPTION;

- for worldwide or certain ip
GRANT ALL PRIVILEGES ON *.* TO 'mingkit'%'IDENTIFIED BY 'password' WITH GRANT OPTION;