eclipse mysql jndi_eclipse+mysql+tomcat配置JNDI

配置环境:Tomcat5.5、MySQL4.1 、mysql-connector-java-5.0.0-beta 、commons-dbcp-1.2.1、 JDK1.5以上的“东东”在网上都能够下到的。

然后在D:\Tomcat5.5\webapps\新建目录DBtest文件夹,DBtest下建这样几个目录:WEB-INF、META-INF,在WEB-INF下创建两个文件夹和两个文件分别为classes、lib、web.xml、Test.jsp,在META-INF下创建context.xml(为什么要创建这个文件我也不太清楚 ,哪位高手知道还请多指教)

接下来就要把JDBC驱动程序mysql-connector-java-5.0.0-beta和commons-dbcp-1.2.1解压,分别将解压后的.jar文件分别复制到D:\Tomcat5.5\common\lib下,以上工作做完就可以进行连接池的配置了。

1、配置D:\Tomcat 5.5\conf\server.xml在server.xml中的中添加如下配置信息:

type="javax.sql.DataSource"

driverClassName="com.mysql.jdbc.Driver"

password="yang"

maxIdle="2"

maxWait="5000"

username="root"

url="jdbc:mysql://localhost:3306/math"

maxActive="4"/>

注:name是连接池的名字可任取,type,是数据源的类型,driverClassName是驱动程序的类名好像得这么写,url是数据库的路径math为数据库名。

2、配置D:\Tomcat 5.5\webapps\DBtest\WEB-INF\web.xml在中加入如入信息

DB Connection

Test

javax.sql.DataSource

Container

注:其中的res-ref-name必须和前面的连接池名相同。

3、配置D:\Tomcat 5.5\webapps\DBtest\META-INF\context.xml全部内容如下

debug="5"

reloadable="true"

crossContext="true">

注:path是工作区的路径,ResourceLink name和global好像都得和前面的连接池的名字相同,这里的global有什么用我也不太清楚啦,不好意思,嘻嘻!!

java 代码package app;

import java.sql.*;

import javax.naming.*;

import javax.sql.DataSource;

/*

public class dbManager

{

public static synchronized Connection getConnection() throws Exception

{

try

{

Context initctx = new javax.naming.InitialContext();

Context envctx = (Context)initctx.lookup("java:comp/env");

DataSource ds = (DataSource)envctx.lookup("jdbc/Test");

return ds.getConnection();

}

catch (Exception ex)

{

throw ex;

}

}

}

*/

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

public class dbManager

{

final static private boolean VERBOSE = true; //打印控制台控制

//static Logger logger = Logger.getLogger(dbManager.class.getName());

private Context initCtx = null;

private Context ctx = null;

private DataSource ds = null;

private long timeout = 5000;

public dbManager()

{

try

{

initCtx = new InitialContext();

//init context,read config web.xml

if (initCtx == null)

{

throw new Exception("Initial Failed!");

}

ctx = (Context) initCtx.lookup("java:comp/env");

//find "jdbc/SqlServerDB" object  this configruation in the SERVER.XML of Tomcat

if (ctx != null)

{

ds = (DataSource) ctx.lookup("jdbc/Test");

}

if (ds == null)

{

throw new Exception("Look up DataSource Failed!");

}

}

catch (Exception e)

{

log(e, "Can’t get the Context!");

}

}

public synchronized Connection getConnection() {

//get connection and set to delay time

long startTime = new java.util.Date().getTime();

Connection con = null;

while (con == null) {

con = newConnection();

if (con != null) {

//log("Create New Connection!");

break;

}

try {

log("连接超时,重新连接,等待" + timeout + "ms");

wait(timeout);

}

catch (InterruptedException e) {

log(e, "连接超时!");

}

if ( (new java.util.Date().getTime() - startTime) >= timeout) {

log("Connection timeout!");

break;

}

}

return con;

}

private Connection newConnection() {

Connection con = null;

try {

con = ds.getConnection();

if (con == null) {

throw new Exception("Create Connection Failed!");

}

}

catch (Exception e) {

log("Create Connection Failed!");

System.out.println(e.getMessage());

}

return con;

}

public synchronized void freeConnection(Connection conn,

Statement stmt,

PreparedStatement pstmt) {

try {

//close Statement

if (stmt != null) {

stmt.close();

stmt = null;

//log("Close Statement......");

}

//close  PreparedStatement

if (pstmt != null) {

pstmt.close();

pstmt = null;

//log("Close PreparedStatement......");

}

}

catch (Exception e) {

System.out.println(e.getMessage());

}

try {

//close Connection

if (conn != null) {

conn.close();

conn = null;

//log("Close Connection......");

}

}

catch (SQLException e) {

log(e, "释放资源出错!");

}

}

/************************************

* write log file.

* @param s String

************************************/

private void log(String s)

{

if (VERBOSE)

{

System.out.println(new java.util.Date() + ":" + s);

//logger.info(new java.util.Date()+s);

}

}

private void log(Throwable e, String msg)

{

System.out.println(new java.util.Date() + ": " + msg);

}

}

triman.rar

描述:

eclipse+mysql+tomcat配置JNDI实现例子

icon_clip.gif

下载

文件名:

triman.rar

文件大小:

1007 KB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值