spacetime
asked on
How to connect to MS Access using JNDI with Tomcat 5 ?
Hi,
I got
/usr/local/tomcat5/webapps /Advanced/ connect.js p
========================== ========== =====
<html>
<head>
<title>Connection Test</title>
</head>
<body>
<%
com.wrox.library.Connect con = new com.wrox.library.Connect() ;
con.init();
%>
<h2>Connection Result</h2>
<%= con.getstat() %>
</body>
</html>
========================== ========== ========== =======
/usr/local/tomcat5/webapps /Advanced/ WEB-INF/cl asses/com/ wrox/libra ryConnect. java
========================== ========== ========== ======
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class Connect {
String stat = "Not Connected !!";
public void init() {
try {
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Oops - No Context");
DataSource ds = (DataSource)ctx.lookup("ja va:comp/en v/jdbc/Boo ksDB");
if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) {
stat = " Got Connection "+conn.toString();
conn.close();
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public String getstat() {
return stat;
}
}
========================== ========== ========== ========== ====
/usr/local/tomcat5/webapps /Advanced/ WEB-INF/we b.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/BooksDB </res-ref- name>
<res-type>javax.sql.DataSo urce</res- type>
<res-auth>Container</res-a uth>
</resource-ref>
</web-app>
========================== ========== ========== ========== ====
/usr/local/tomcat5/conf/se rver.xml
========================== ========== ========== ========== ====
.......................
<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.cata lina.logge r.FileLogg er" prefix="Wrox_Advanced_log. "
suffix=".txt" timestamp="true"/>
<Resource name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource "/>
<ResourceParams name="jdbc/BooksDB">
<parameter>
<name>factory</name>
<value>org.apache.commons. dbcp.Basic DataSource Factory</v alue>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>removeAbandoned</nam e>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeo ut</name>
<value>180</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>username</name>
<value>booksuser</value>
</parameter>
<parameter>
<name>password</name>
<value>bookspass</value>
</parameter>
<parameter>
<name>driverClassName</nam e>
<value>org.gjt.mm.mysql.Dr iver</valu e>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1. 3.139:3306 /books?aut oReconnect =true</val ue>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
========================== ========== ========== ========== ====
The above is to connect to MySQL database. How about if I wanna connect to MS Access DB ? How should I change the above code and setting or configuration ? Where should I put the MS Access file ?
FYI, I m using Tomcat 5.0.11 !!
I was trying to change the Connect.java to
========================== =======
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class Connect {
String stat = "Not Connected !!";
public void init() {
try {
Connection conn = null;
stat = " 1";
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
stat = " 2";
conn = DriverManager.getConnectio n("jdbcdbc :;DRIVER=M icrosoft Access Driver (*.mdb);DBQ=FRONT.mdb","Ad min","1234 56");
stat = " 3";
stat = " Got Connection "+conn.toString();
conn.close();
}
catch(ClassNotFoundExcepti on classnotfoundexception)
{
System.out.println("Could not find class: " + classnotfoundexception);
return;
}
catch(SQLException sqlexception)
{
System.out.println(sqlexce ption + "");
return;
}
catch(Exception e) {
e.printStackTrace();
}
}
public String getstat() {
return stat;
}
}
========================== ========== =======
but it stuck between stat = "2" and stat = " 3"
pls help !!
I got
/usr/local/tomcat5/webapps
==========================
<html>
<head>
<title>Connection Test</title>
</head>
<body>
<%
com.wrox.library.Connect con = new com.wrox.library.Connect()
con.init();
%>
<h2>Connection Result</h2>
<%= con.getstat() %>
</body>
</html>
==========================
/usr/local/tomcat5/webapps
==========================
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class Connect {
String stat = "Not Connected !!";
public void init() {
try {
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Oops - No Context");
DataSource ds = (DataSource)ctx.lookup("ja
if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) {
stat = " Got Connection "+conn.toString();
conn.close();
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public String getstat() {
return stat;
}
}
==========================
/usr/local/tomcat5/webapps
==========================
<?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/BooksDB
<res-type>javax.sql.DataSo
<res-auth>Container</res-a
</resource-ref>
</web-app>
==========================
/usr/local/tomcat5/conf/se
==========================
.......................
<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.cata
suffix=".txt" timestamp="true"/>
<Resource name="jdbc/BooksDB" auth="Container" type="javax.sql.DataSource
<ResourceParams name="jdbc/BooksDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>removeAbandoned</nam
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeo
<value>180</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>username</name>
<value>booksuser</value>
</parameter>
<parameter>
<name>password</name>
<value>bookspass</value>
</parameter>
<parameter>
<name>driverClassName</nam
<value>org.gjt.mm.mysql.Dr
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1.
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
==========================
The above is to connect to MySQL database. How about if I wanna connect to MS Access DB ? How should I change the above code and setting or configuration ? Where should I put the MS Access file ?
FYI, I m using Tomcat 5.0.11 !!
I was trying to change the Connect.java to
==========================
package com.wrox.library;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class Connect {
String stat = "Not Connected !!";
public void init() {
try {
Connection conn = null;
stat = " 1";
Class.forName("sun.jdbc.od
stat = " 2";
conn = DriverManager.getConnectio
stat = " 3";
stat = " Got Connection "+conn.toString();
conn.close();
}
catch(ClassNotFoundExcepti
{
System.out.println("Could not find class: " + classnotfoundexception);
return;
}
catch(SQLException sqlexception)
{
System.out.println(sqlexce
return;
}
catch(Exception e) {
e.printStackTrace();
}
}
public String getstat() {
return stat;
}
}
==========================
but it stuck between stat = "2" and stat = " 3"
pls help !!
are you trying to do connection pooling for a db? There are other ways to do it also .
You can write your own connection pool or u can use the tutorial from java
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html
Hope this helps
Cheers!
You can write your own connection pool or u can use the tutorial from java
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html
Hope this helps
Cheers!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi spacetime,
I think I spent some time and tried answering you question earlier before in the thread
http://oldlook.experts-exchange.com/questions/20774349/JNDI-DataSource-unable-to-work.html
and it doesnt seem to help either ....
I think I spent some time and tried answering you question earlier before in the thread
http://oldlook.experts-exchange.com/questions/20774349/JNDI-DataSource-unable-to-work.html
and it doesnt seem to help either ....
spacetime/eexcel
I am bring a mod in
Cheers!
I am bring a mod in
Cheers!
chek this thread
http://www.mail-archive.com/j2ee@p2p.wrox.com/msg00432.html