Tuesday, May 27, 2008

Simple JNDI Accessing MySQL Datasource

Base on the previous post, I have used the struts-config.xml to define the datasource to access to the MySQL database. The datasource class is code within the Action class (controller layer) and map to the struts-config.xml.

In order to improve the programming pratice, the datasource can be created via JNDI and the java code should be sit within the model layer. As a result, business logic will separate from the controller layer and making the Action class without knowing any relation with business class at all.

Click here to read more...

Monday, May 26, 2008

Struts - Database Access by Defining Datasource within the struts-config.xml

A sample program which connecting to the MySQL database to display the data. I am using the Struts framework to developed this program. The main idea is of this post is to use the struts-config.xml to define the datasource and using Action class to access and select the data.

Please bear in mind, the Action class which represent as controller layer, should not contain any business logic code and database access code. The best pratice is to apply the DAO mechanism by putting your business logic or database access code within the model layer.
The datasource should only define within the struts-config.xml when there is a limitation of connectivity to the database within the model layer. Alternative, you can use the JNDI to define the datasource and that would be the best recommendation in best pratice. However, that will be my next post on my blog.



Here are the summary of development been used.

  • Struts 1.2.9 framework

  • index.jsp, selectBook.jsp, SelectBookActionForm.java - view layer

  • SelectBookAction.java - controller layer

  • Book.java - data layer

  • Defining datasouce at struts-config.xml

  • MySQL database
Read more and download the code: Click here

Sunday, May 25, 2008

Struts: Build HTML Options Tag Elements from MySQL data

Some sample program developed using STRUTS framework to access the MySQL database and retrieve data to build the html options elements. This post is consider as an enhancement from the previous post Struts: HTML Options Tag Sample Code.

The previous post was hardcoded to build the html options tag. Meanwhile, this post will build the html options’ elements by retrieving data from MySQL database.

I have used the struts-config.xml to define the datasouce to access to the MySQL database.

Click here to read more.

Wednesday, May 21, 2008

Java MySQL - Mass Update Using Batch Updating

Some sample of the code to perform the mass update of database.

The ideas behind of this code is to improve the database performance by reduce the cost of query executions. This is refer to the INSERT, UPDATE and DELETE of any rows within the database.

Some developer may execute the query statement within the WHILE or FOR loop condition.

With batch updating, a set of SQL query can be writen and execute together to improve the performance.

Example of the code:

//Create the prepared statement
String updateSQL = “UPDATE customer SET country = ? WHERE id = ?”;
stat = conn.prepareStatement(updateSQL);

//Insert the country and hardcoded id.
for(int i = 0; i <= 5; i++){
stat.setString(1, “Malaysia”);
stat.setInt(2, i);
stat.addBatch();
}

//Execute the batch.
int [] updateCounts = stat.executeBatch();


Full code available here: http://leejeok.wordpress.com/2008/05/21/java-mysql-mass-update-using-batch-updating/

Monday, May 19, 2008

Struts: HTML Options Tag Sample Code

I have been doing some research on the internet recently and notice not much of the sample code been provided to build the drop down menu using struts html option tag. Here are the sample program which I develop using Struts framework by applying the html options tag. The screenshot will look similar as below.



Please click here to view the code:
http://leejeok.wordpress.com/2008/05/14/struts-html-options-tag-sample-code/

Wednesday, May 7, 2008

MySQL Workbench

One of the tools which I like to use to design my database is known as MySQL Workbench. I have used it to design the Entity Relationship Diagram and export the design into PDF or PING image. Once you complete the design, you can generate the physical database which included with the forward engineering capabilities or exporting it to SQL text file. This easy tool also assists you to document the database design into text and html form.

The MySQL Workbench deliver in 2 editions.
The Standard Edition will provide you all the needs which I mentioned above.
If you looking for a simple and free tools for you visualization and design of database, or you want to export your design to PDF, PNG or SQL query, perhaps you may want to try the Community Edition - this might be the one that you looking for.