Tuesday, August 28, 2012

Using JDO with MySQL

I recently worked with Java Data Object( JDO ) on my machine to store and retrieve data from a local MySQL database. Following is the procedure how I did it. I used Eclipse IDE for writing JAVA codes.

*** Important: To build and run the project this post is useful.

First I created a property file that contains necessary information for connecting to the local MySQL and used by JDO api to create PersistenceManagerFactory class. Put the file in project's root folder.
File    : datanucleus.properties
Code : http://pastebin.com/0npQb4ag
Update username , password ,db-name according to yours.

Then I created a class named "UserInfo" to store username , password , email-address.
File    : UserInfo.java
Code : http://pastebin.com/CAMth7db
For the annotaions' guideline visit here 
Then I created a class named PMF.java to make sure that only one instance of PersistenceManagerFactory  is used through out the application. That file uses datanucleus.properties
file.
File    : PMF.java
Code : http://pastebin.com/24tcVgGk
Finally to test everything works I write TestJDO.java to test every thing works fine.
File    : TestJDO.java
Code :  http://pastebin.com/zeJgpcp9

Monday, August 27, 2012

Eclipse , JDO and MySQL Integration using Ant

I did it in a Linux machine( Ubuntu 10.10 ).I am assuming the following programs are already installed in the machine.

Eclipse JAVA IDE
MySQL

DataNucleus is used as an implementation for JDO( Java Data Object ). Follow the link and download
datanucleus-accessplatform-rdbms-<version>.zip from the latest release (and extarct) . I worked with version= 3.1.0-release.

Also download JDO-API from apache. Find the Latest version and download jdo-api-<jdo-version>.jar.
MySQL JDBC driver is also needed. 

Now create a Java Project in Eclipse and add the following jars to the project's CLASSPATH.

From ....../datanucleus-accessplatform-rdbms-<version>/dep/ folder
       asm-<asm-version>.jar
       log4j-<log4j-version>.jar

From ....../datanucleus-accessplatform-rdbms-<version>/lib/ folder
       datanucleus-api-jdo-<version>.jar
       datanucleus-core-<version>.jar
       datanucleus-enhancer-<version>.jar
       datanucleus-rdbms-<version>.jar

....../jdo-api-<jdo-version>.jar
....../mysql-connector-java-<mysql-version>-bin.jar (For MySQL JDBC driver).

Now Export the created Java project to General > AntBuildFiles . Click next and finish nothing much is needed to be done. A build.xml file has been created in the project's root directory. Open it with Eclipse Ide.

Add following snippet of xml to it

<target
        name="-post-compile"
        depends="init" >

        <taskdef
            name="datanucleusenhancer"
            classname="org.datanucleus.enhancer.tools.EnhancerTask"
            classpathref="<ProjectName>.classpath" />

        <echo message="start datanucleusenhancer" />

        <datanucleusenhancer
            classpathref="<ProjectName>.classpath"
            dir="bin"
            verbose="true" >

            <fileset dir="bin" >

                <include name="**/*.class" />
            </fileset>
        </datanucleusenhancer>

        <echo message="end datanucleusenhancer" /> 
</target>  

add this under <project> tag . Change <ProjectName>  according to yours.

Find <javac> tag and add the following line inside <javac></javac> tag.
<compilerarg line=" -processor org.datanucleus.enhancer.EnhancerProcessor" />
Add an attribute verbose="on" to javac tag.

Now find the following xml

<target
        name="build"
        depends="build-subprojects,build-project"/>


Turn it to
<target
        name="build"
        depends="build-subprojects,build-project,-post-compile" />


Now go to project --- properties -- builders and select new. Then select "Ant Builder" . Add a name and in "Buildfile" tab point the build.xml file. Mark only the newly created builder in the "Builders" list.

Friday, August 3, 2012

Configure Eclipse and Java 3D in Linux( Ubuntu 10.10 )

At the very beginning we need to install Java 3d.

Installation:
- Open the terminal ( Applications >> Accessories >> Terminal  )
- Type the following command
       sudo apt-get install libjava3d-java

After that we need to copy some files.
Copying Files:
- Copy the following jar files 
                j3dcore-1.5.2+dfsg.jar , 
                j3dcore.jar , 
                j3dutils-1.5.2+dfsg.jar , 
                j3dutils.jar , 
                vecmath.jar
to  $JAVA_HOME/jre/lib/ext folder. I found ( hope you also will ) the above files in 
/usr/share/java folder. You can use the following commands to do that
       sudo cp -i /usr/share/java/j3d*.jar $JAVA_HOME//jre/lib/ext
       sudo cp -i /usr/share/java/vecmath.jar $JAVA_HOME//jre/lib/ext
In my case , $JAVA_HOME = /usr/lib/jvm/java-6-sun-1.6.0.24 .


- Copy the libj3dcore-ogl.so to any of the directory that has been pointed out in LD_LIBRARY_PATH . The file is located in /usr/lib/jni/ folder . (This folder may already be in your LD_LIBRARY_PATH. Then copying is not needed ) .  I copied it to 
$JAVA_HOME/java-6-sun-1.6.0.24/jre/lib/i386. You can use the following command 
        sudo cp -i /usr/lib/jni/libj3dcore-ogl.so $JAVA_HOME//jre/lib/i386.


Eclipse Integration:
- Create a new Java Project.
- At project's build path select "Add external archive".
- Point out the following jar files 
       j3dcore.jar , 
       j3dutils.jar , 
       vecmath.jar         


Testing:
You can find a sample program in the tutorial hosted in site.