CONFIGURE TOMCAT 6 WITH JSP,MYSQL
Step By Step :
- Download mysql driver (connector j) from https://www.mysql.com/products/connector/
- Extract end paste mysql-connector-java-8.0.11.jar into
Folder apache-tomcat-6.0.13\lib
- Install Xamp (get php & mysql services)
- Login phpmyadmin (http:// localhost/phpmyadmin)
- Create new example database. example: javatest & create new table with 3 colums (nama, alamat, alamat2)
- Create xml data source with context style descriptor (more secure):
Navigate into this folder:
apache-tomcat-6.0.13\conf\Catalina\localhost
& create new xml files.let say .- mysql.xml which is contained as below :
<Context path=”/example” docBase=”D:\MySQLApps\web” debug=”5″ reloadable=”true”>
<Resource name=”jdbc/TestDB” auth=”Container” type=”javax.sql.DataSource”
maxActive=”100″ maxIdle=”30″ maxWait=”10000″
username=”root” password=”” driverClassName=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://localhost:3306/javatest”/>
</Context>
jdbc/TestDB = data source name- you can put anything
docBase=”D:\MySQLApps\web” = location to deploy web apps
username & password = mysql access
url : jdbc:mysql://localhost:3306/javatest = point to mysql database
database name = javatest (example)
- Create parent apps directory
D:\MySQLApps\web
& create web apps folder
Example (your system application name)
And standard java folder file WEB-INF
sample:
- Setting data source jdbc/ TestDB
Create web.xml file in the WEB-INF
<web-app xmlns=”http://java.sun.com/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
version=”2.5″>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
- In the web apps folder /example, create jsp code files.
<%@ page session=”true” %> <%@ page import=”common.*” %> <%@ page import=”java.sql.*” %> <%@ page import=”javax.sql.*” %> <%@ page import=”javax.naming.*” %> <%@ page import=”java.util.*” %> <%@ page import=”java.text.*” %> <%@page import=”java.io.*” %> <%
Connection conn = null;
try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup( “java:comp/env” ); DataSource ds = (DataSource)envCtx.lookup( “jdbc/TestDB” ); conn = ds.getConnection();
}
catch( Exception e ) { out.println (e.toString()); }
%>
<% if(conn !=null) {
String sql = “select nama,alamat,alamat2 from testdata”; try
{ PreparedStatement pstmt = conn.prepareStatement(sql); //pstmt.setString(1,staffid); ResultSet rset = pstmt.executeQuery (); while (rset.next()) { //staff_title = rset.getString (1); %> <%=rset.getString (1) %>- <%=rset.getString (2) %>- <%=rset.getString (3) %>
<% }
rset.close (); pstmt.close (); }
catch (SQLException e)
{ out.println (“ERROR GET STAFF TITLE: ” + e.toString ()); }
} %>
|
- Start tomcat &
Navigate internet browser (http://localhost:8888/mysql/example/test2.jsp)
.
& data from mysql database will be displayed in the browser.
Thanks You
Sabrisoft
Recent Comments