>Connection Pooling, Thanks Mas Ponco

>ada pepatah mengatakan rumahku adalah istanaku…

aku udah nyoba nyari sampe keliling-keliling dunia maya, keberbagai server di berbagai negara.. eh ternyata yang bisa beri cahaya terang datangnya dari orang sendiri.. ya dialah mas ponco yang udah repot-repot buat tutorial mengenai tomcat jdbc connection pool “Step by Step Tutorial, create JNDI JDBC Datasource in Tomcat 5.5” yang berhasil aku dapat dari situs http://www.fornewbie.com/article/java/step_by_step_tutorial_create_jndi_jdbc_datasource_in_tomcat_5.5.html.

memang ga nyangka juga sih… tapi aku seneng banget, setidaknya ada satu titik terangđŸ˜‰ ;^) keep keen…

tapi waktu hari ini mau aku kunjungi lagi halamannya buat ngucapin makasih, eh halamannya udah ga ada lagi… padahal bagus banget kan tutorialnya… tapi ga apalah… sebagai rasa terima kasihku buat mas ponco, tutorialnya aku pajang lagi nih di blog ku…

Step by Step Tutorial, create JNDI JDBC Datasource in Tomcat 5.5
Written by Ponco, Thursday, 07 September 2006

JNDI resource configuration has changed somewhat between Tomcat 5.0.x and Tomcat 5.5.x In this tutorial we will use MySQL database.

Download mysql jdbc driver, extract it then copy the jar into $CATALINA_HOME/common/lib

Create new database for testing,


create database jspTutorial;
CREATE TABLE `employee` (
`EmployeeID` int(8) NOT NULL auto_increment,
`EmployeeName` varchar(128) NOT NULL,
`Address` text,
PRIMARY KEY (`EmployeeID`)
) ENGINE=MyISAM;
insert into `employee` values (1,'Agung Apriyanto','Jl. Merbabu Selatan #34'),
(2,'Slamet Riyadi','Jl. GandulWangi Selatan #23'),
(3,'Ahmad Hartono','Jl. Sumatra Barat #12');

Create context.xml in $CATALINA_HOME/webapps/jspTutorial/META-INF/ directory


<?xml version="1.0" encoding="UTF-8"?>
<Context path="/jspTutorial" docBase="jspTutorial"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/jspTutorial" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/jspTutorial?autoReconnect=true"/>
</Context>

Create jspTutorial3.jsp. this page will retrieve data from mysql database through jndi datasource


<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
ResultSet rs = null;
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/jspTutorial");
Connection conn = ds.getConnection();
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM employee");
rs = pstmt.executeQuery();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
<html>
<head><title>JNDI Datasource tutorial</title></head>
<body>
<table border="1">
<tr><td>EmployeeID</td><td>Name</td><td>Address</td></tr>
<% while (rs.next()) { %>
<tr>
<td><%= rs.getString("EmployeeID")%></td>
<td><%= rs.getString("EmployeeName")%></td>
<td><%= rs.getString("Address")%></td>
</tr>
<% } %>
</table>
</body>
</html>

The page will display


+------------+-----------------+-----------------------------+
| EmployeeID | EmployeeName | Address |
+------------+-----------------+-----------------------------+
| 1 | Agung Apriyanto | Jl. Merbabu Selatan #34 |
| 2 | Slamet Riyadi | Jl. GandulWangi Selatan #23 |
| 3 | Ahmad Hartono | Jl. Sumatra Barat #12 |
+------------+-----------------+-----------------------------+

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s