Tuesday, July 8, 2008

Setup Java, Tomcat, MySQL on Ubuntu (JSP Hosting)

This tutorial will lead you to setup a simple JSP hosting on Ubuntu machine. You may want to consider this as a basic setup to host any of your web application which developed using Java - JSP or Servlet, Tomcat and MySQL.

** If you had downloaded Ubuntu 8.04 LTS Desktop Edition, you can skip the step below and goto Step 8. Desktop edition will be easy to install. All you need is to boot from the CD and follow the instruction closely.

Install Ubuntu Ubuntu 8.04 LTS Server
Step 1: Download the Ubuntu Server from http://www.ubuntu.com/
Step 2: Burn the ISO file into CD and boot your machine from CD.
Step 3: Follow the installation step closely and it is easy to do it
Step 4: Once install successfully, you will require to login.
Step 5: After login, perform the necessary updates. Make sure you have the internet connections. Type sudo apt-get update to do the update.

Install the GNOME desktop
Step 6: Type sudo apt-get install ubuntu-desktop to download and install GNOME.
Step 7: Type startx to start the GNOME

**You can install KDE desktop instead of GNOME. However, this is up to your taste. To install KDE, type the command sudo apt-get install kubuntu-desktop

Install JDK and JRE (Java)
Step 8: Open the terminal. Applications --> Accessories --> Terminal


Step 9: Type sudo apt-get install sun-java6-jdk


Step 10: After successful install, you can do some verification to find out the installed JDK and JRE version. At the terminal, type javac –version for JDK and java –version for JRE.



Install Apache Tomcat 5.5
**Before installing the Tomcat 5.5, you will need to ensure that you had installed JDK and JRE.


Step 11: Download the tomcat 5.5 from http://tomcat.apache.org/ and saved it to desktop. Here, apache-tomcat-5.5.26.tar.gz been used.
Step 12: Right click on the file and click on Extract Here


Step 13: Rename the folder to tomcat for easier to remember the folder name.
Step 14: Is good idea to move the tomcat to /usr/local directory.
Type the command sudo mv tomcat-on-your-desktop-path /usr/local


**You need to tell the tomcat where the JDK has been installed and to do this you need to define the JAVA_HOME environment variable in .bashrc file.

Step 15: You are about to edit the .bashrc file. Please backup the file first before make any changes. The .bashrc is a hidden file and is located in your home directory.




Step 16: Right click on the .bashrc and select Open with “Text Editor”.

**Alternatively, at terminal, type the command gedit ~/.bashrc (GNOME) or kate ~/.bashrc (KDE)

Step 17: At the end of the file type export JAVA_HOME=/usr/lib/jvm/java-6-sun and save the file.


Start and Stop tomcat
Step 18: At terminal, type sh /usr/local/tomcat/bin/startup.sh
Step 19: You can verify the success installation by open the Mozilla Firefox browser and browse to http://localhost:8080/
Step 20: At terminal, type sh /usr/local/tomcat/bin/shutdown.sh

Install the MySQL Server 5.0
Step 21: At terminal, sudo apt-get install mysql-server

** The step 22 and 23 are optional. Both are GUI which allow you to manage the database easily.

Install MySQL Query Browser.
Step 22: At terminal, type sudo apt-get install mysql-query-browser

Install MySQL Administrator
Step 23: At terminal, type sudo apt-get install mysql-admin

**I am assuming that you have the experience to do the deployment by yourself. All you need to do now, are:
-create all the necessary database, tables and fields,
-you will place your .war file into /usr/local/tomcat/webapps,
-start the tomcat....

Wednesday, June 18, 2008

Firefox 3 attempt to break the world record

The new version of Firefox 3 has been released at June 17, 2008.

Firefox is about to set the world record by breaking through the most downloadable software within 24 hours. Click here...





The moment I download the software, it's already hitting 3,331,796 of worldwide downloads.




Click on the banner below to donwload Firefox 3.

Download Day

Friday, June 13, 2008

Database Navigation using Next and Previous link - Part 2

This is enhanced sample of codes from previous post at http://www.leejeok.com/2008/06/navigate-database-record-using-next-and.html

I have reduce some JSP code by using the SQL script to get and return the number of rows from database. I am using MySQL as the database and using the LIMIT clause to retrieve the number of rows to display on single page.


<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>

<%!
static final String ctxstr = "java:comp/env/jdbc/navigationDB";
%>
<html>
<head>
<title>Database Navigation using Next and Previous button</title>
</head>
<body>
<form>
<h1>Database Navigation using Next and Previous link</h1>

<%
int id = 0;
String fullname = "";
String country = "";
int recordPerPage = 2;
int gotoPage = 0;

boolean moreResult = false;

Connection conn = null;
Statement stat = null;

//get page
try {
gotoPage = Integer.parseInt(request.getParameter("gotoPage"));
} catch (NumberFormatException e ) {
gotoPage = 0;
}

try {
//lookup for the JNDI datasource
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(ctxstr);

//create the database connection
conn = ds.getConnection();
stat = conn.createStatement();

String offset = String.valueOf(gotoPage * recordPerPage);
String rows = String.valueOf(recordPerPage);

//execute query using SELECT and LIMIT clause.
//LIMIT accept 2 argument.
//the first represent the offset of the first row to return
//the second represent the maximum number of rows to return
String sql = "SELECT id, fullname, country FROM customer LIMIT "+offset+" , "+rows;
ResultSet resultSet = stat.executeQuery(sql);

out.println("<table border='1'><th>ID</th><th>Name</th><th>Country</th>");

int totalRecord = 0;
for (; resultSet.next(); totalRecord++) {
id = (int)resultSet.getInt("id");
fullname = (String)resultSet.getString("fullname");
country = (String)resultSet.getString("country");
out.println("<tr><td>"+id+"</td><td>"+fullname+"</td><td>"+country+"</td></tr>");
}
out.println("</table>");
out.println("<br>");

if(totalRecord == recordPerPage)
moreResult = true;

//close connection
resultSet.close();
stat.close();
stat = null;
conn.close();
conn = null;

} catch(SQLException e) {
out.println("An error occured: " + e.toString());
return;
} finally{
if(conn != null)
conn.close();
}


if ( gotoPage == 0 ) {
out.print("Previous");
} else {
out.print("<a href=\"limit.jsp?gotoPage="+(gotoPage - 1)+"\"><b>Previous</b></font></a>");
}

out.print("&nbsp;&nbsp;");

if (!moreResult) {
out.print("Next");
} else {
out.print("<a href=\"limit.jsp?gotoPage="+(gotoPage + 1)+"\"><b>Next</b></a>");
}

%>

</form>
</body>
</html>

Sunday, June 8, 2008

Navigate Database using First, Previous, Next and Last Button

Here are some simple codes to display record within a page. User will click on the button to browse through the specific records.




<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>

<%!
static final String ctxstr = "java:comp/env/jdbc/navigationDB";
%>

<html>
<head>
<title>Database Navigation using Next and Previous button</title>
</head>
<body>
<form>
<h1>Database Navigation using Next and Previous link</h1>

<%
String fullname = "";
String country = "";

int counter = 0;

//total result for each page
int recordsPerPage = 5;

//the default page
int gotoPage = 1;

//get page
try {
gotoPage = Integer.parseInt(request.getParameter("gotoPage"));
} catch (NumberFormatException e ) {
gotoPage = 1;
}

Connection conn = null;
Statement stat = null;

try {
//lookup for the JNDI datasource
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(ctxstr);

//create the database connection
conn = ds.getConnection();
stat = conn.createStatement();

//execute query
String sql = "SELECT fullname, country FROM customer";
ResultSet resultSet = stat.executeQuery(sql);

//skip the records which not require within the page
int row = (gotoPage - 1) * recordsPerPage + 1;
for(int x=1; x<row; x++)
resultSet.next();

out.println("<table border='1'><th>Name</th><th>Country</th>");

//display only record which require within the page
while ( (counter < recordsPerPage) && resultSet.next() ) {
fullname = (String)resultSet.getString("fullname");
country = (String)resultSet.getString("country");
out.println("<tr><td>"+fullname+"</td><td>"+country+"</td></tr>");
counter++;
}

out.println("</table>");
out.println("<br>");

//create next and previous link
boolean moreResult = resultSet.next();
if ( gotoPage == 1 ) {
out.print("Previous");
} else {
out.print("<a href=\"index.jsp?gotoPage="+(gotoPage - 1)+"\"><b>Previous</b></font></a>");
}

out.print("&nbsp;&nbsp;");

if (!moreResult) {
out.print("Next");
} else {
out.print("<a href=\"index.jsp?gotoPage="+(gotoPage + 1)+"\"><b>Next</b></a>");
}

//close connection
resultSet.close();
stat.close();
stat = null;
conn.close();
conn = null;

} catch(SQLException e) {
out.println("An error occured: " + e.toString());
return;
} finally{
if(conn != null)
conn.close();
}
%>

</form>
</body>
</html>

If you are building the GUI application which similar to above functionalities, below are some quick ideas to what you can do to the ResultSet.

Next Record
if (!rs.isLast()) {
rs.next();
//retrieve the value
}


Previous Record
if (!rs.isFirst()) {
rs.previous();
//retrieve the value
}


Last Record
rs.last();

First Record
rs.first();

Goto Record
rs.absolute();

By keeping the ResultSet open, each code above can be define within the respective buttons’ ActionListener().

Navigate Database Record using Next and Previous Link

Here are some simple codes to display the fixed number of records within single page. Click on the previous and next link to browse through the records.




<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>

<%!
static final String ctxstr = "java:comp/env/jdbc/navigationDB";
%>

<html>
<head>
<title>Database Navigation using Next and Previous button</title>
</head>
<body>
<form>
<h1>Database Navigation using Next and Previous link</h1>

<%
String fullname = "";
String country = "";

int counter = 0;

//total result for each page
int recordsPerPage = 5;

//the default page
int gotoPage = 1;

//get page
try {
gotoPage = Integer.parseInt(request.getParameter("gotoPage"));
} catch (NumberFormatException e ) {
gotoPage = 1;
}

Connection conn = null;
Statement stat = null;

try {
//lookup for the JNDI datasource
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(ctxstr);

//create the database connection
conn = ds.getConnection();
stat = conn.createStatement();

//execute query
String sql = "SELECT fullname, country FROM customer";
ResultSet resultSet = stat.executeQuery(sql);

//skip the records which not require within the page
int row = (gotoPage - 1) * recordsPerPage + 1;
for(int x=1; x<row; x++)
resultSet.next();

out.println("<table border='1'><th>Name</th><th>Country</th>");

//display only record which require within the page
while ( (counter < recordsPerPage) && resultSet.next() ) {
fullname = (String)resultSet.getString("fullname");
country = (String)resultSet.getString("country");
out.println("<tr><td>"+fullname+"</td><td>"+country+"</td></tr>");
counter++;
}

out.println("</table>");
out.println("<br>");

//create next and previous link
boolean moreResult = resultSet.next();
if ( gotoPage == 1 ) {
out.print("Previous");
} else {
out.print("<a href=\"index.jsp?gotoPage="+(gotoPage - 1)+"\"><b>Previous</b></font></a>");
}

out.print("&nbsp;&nbsp;");

if (!moreResult) {
out.print("Next");
} else {
out.print("<a href=\"index.jsp?gotoPage="+(gotoPage + 1)+"\"><b>Next</b></a>");
}

//close connection
resultSet.close();
stat.close();
stat = null;
conn.close();
conn = null;

} catch(SQLException e) {
out.println("An error occured: " + e.toString());
return;
} finally{
if(conn != null)
conn.close();
}
%>

</form>
</body>
</html>