jsp tomcat mysql connection
843859Dec 19 2007 — edited Dec 22 2007hello,
i have been trying to get a connection for accessing the database(mysql) and jsp through tomcat. I am using sql 5.0, jsp2.0, j2sdk1.4.2_16, tomcat 5.5.25, jconnector5.1.5, dbcp 1.2.2, collections 3.2, pool1.3.
the first program that i made was for checking the connection which is not giving any error but is also not getting connected. i hav added the default port for mysql also.
My coding is as follows:-
server.xml (addition)
<!-- Tomcat Root Context -->
<!--
<Context path="" docBase="ROOT" debug="0" />
-->
<Context path="/Advanced" docBase="Advanced"
debug="0" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_Advanced_log." suffix=".txt" timestamp="true" />
<Resource name="jdbc/books"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/books">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
<parameter>
<name>username</name>
<value>booksuser</value>
</parameter>
<parameter>
<name>password</name>
<value>bookspass</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/books</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeOut</name>
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
</ResourceParams>
</Context>
web.xml_
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<description>mySQL Test App </description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/books</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
connect.jsp
<html>
<head><title>Connection Test</title></head>
<body>
<%
com.wrox.library.Connect con = new com.wrox.library.Connect();
con.init();
%>
<h2>Connection Result</h2>
STAT<%= con.getStat() %>
BAR<%= con.getBar() %>
</body>
</html>
connect.java
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class Connect
{
String stat="Not Connected";
int bar=-1;
public void init()
{
try {
Context ctx = new InitialContext();
if(ctx == null)
throw new Exception("Oops - no Context");
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/books");
if (ds != null)
{
Connection conn = ds.getConnection();
if(conn != null)
{
stat = "Got Connection"+conn.toString();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select title_id,title,price from book");
if(rs.next())
{
stat=rs.getString(2);
bar=rs.getInt(3);
}
conn.close();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String getStat()
{
return stat;
}
public int getBar()
{
return bar;
}
}
I have been trying to get through the problem for past 2days but no use.....please help me ...
regards
haze