Solved

How to connect to MS Access using JNDI with Tomcat 5 ?

Posted on 2003-10-28
7
306 Views
Last Modified: 2010-04-01
Hi,

I got


/usr/local/tomcat5/webapps/Advanced/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>
<%= con.getstat() %>
</body>
</html>
=====================================================


/usr/local/tomcat5/webapps/Advanced/WEB-INF/classes/com/wrox/libraryConnect.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("java:comp/env/jdbc/BooksDB");

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/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/BooksDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
============================================================

/usr/local/tomcat5/conf/server.xml
============================================================
.......................

<Context path="/Advanced" docBase="Advanced" debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" 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.BasicDataSourceFactory</value>
</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</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</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</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.1.3.139:3306/books?autoReconnect=true</value>
</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.odbc.JdbcOdbcDriver");
stat = " 2";
conn = DriverManager.getConnection("jdbcdbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=FRONT.mdb","Admin","123456");
stat = " 3";


stat = " Got Connection "+conn.toString();
conn.close();


}
catch(ClassNotFoundException classnotfoundexception)
{
System.out.println("Could not find class: " + classnotfoundexception);
return;
}
catch(SQLException sqlexception)
{
System.out.println(sqlexception + "");
return;
}
catch(Exception e) {
e.printStackTrace();
}
}

public String getstat() {
return stat;
}
}
===========================================

but it stuck between stat = "2" and stat = " 3"

pls help !!
0
Comment
Question by:spacetime
  • 5
7 Comments
 
LVL 3

Expert Comment

by:applekanna
ID: 9636940
JNDI is of rdirecoty systems

chek this thread
http://www.mail-archive.com/j2ee@p2p.wrox.com/msg00432.html
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9636951
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!
0
 
LVL 3

Accepted Solution

by:
applekanna earned 125 total points
ID: 9636962
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9646718
Hi spacetime,
I think I spent some time and tried answering you question earlier before in the thread

http://oldlook.experts-exchange.com/Web/Web_Languages/JSP/Q_20774349.html

and it doesnt seem to help either ....
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9653762
spacetime/eexcel

I am bring a mod in

Cheers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference between member and local variables. 29 126
I am having a difficulty creating a responsive Page for my website. 10 83
reflection example 3 92
countXY challenge 28 146
What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now