Chinese To English     英文 转 中文             
         
随笔-27  评论-53  文章-0  trackbacks-0

        Tomcat数据源的配置是件很有意义的事情,因为它可给我程序提供更好的性能,所以决定写这篇随笔给java初学者一个参考。磨刀不误砍柴工,我们先来看一个档。
        启动Tomcat6.x ——在IE中输入http://localhost:8080——点左边的Tomcat Documentation超链接——再点击JNDI ResourcesJDBC DataSources两个链接到此Tomcat数据源的配置答案应该以经找到了,文档中写的很清楚!下面我给出一个MySQL数据源配置的示例:

1、将MySQL的jdbc驱动包考贝到%CATALINA_HOME%/lib目录下;
2、打开%CATALINA_HOME%/conf/context.xml文件,并在context元素之间添加以下代码:

<Resource name="jdbc/MySQL" auth="Container" type="javax.sql.DataSource"
               maxActive
="100" maxIdle="30" maxWait="10000"
               username
="root" password="mysql" driverClassName="com.mysql.jdbc.Driver"
               url
="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>

3、打开MyEclipse新建一些个WEB工程,修改index.jsp页面如下:

 1<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>
 3
 4<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 5<html>
 6    <head>
 7        <title>My JSP 'index.jsp' starting page</title>
 8    </head>
 9
10    <body>
11            <%
12                Context initCtx = null;
13                final String JNDINAME = "java:comp/env/jdbc/MySQL";//java:comp/env/是固定的,jdbc/MySQL则是我们配置的JNDI名称
14                Connection conn = null;
15                try {
16                    initCtx = new InitialContext();
17                    DataSource ds = (DataSource) initCtx.lookup(JNDINAME);
18                    conn = ds.getConnection();
19                    out.println("数据连接为:" + conn);
20                }
 catch (NamingException e) {
21                    e.printStackTrace();
22                }
 catch (SQLException e) {
23                    e.printStackTrace();
24                }
 finally {
25                    if (conn != null{
26                        try {
27                            conn.close();
28                        }
 catch (SQLException e) {
29                            e.printStackTrace();
30                        }

31                    }

32                }

33            %>
34        <br>
35    </body>
36</html>
37
4、访问http://localhost:8080/工程名/index.jsp页面,输出如下:

1数据连接为:jdbc:mysql://localhost:3306/test?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver 

至此,大功告成!

杰森 
邮箱:json.shen(at)gmail.com
网站:www.shenjia.org
posted on 2008-05-11 20:07 杰森 阅读(2000) 评论(12)  编辑  收藏 所属分类: JavaEE

评论:
# re: Tomcat6.x 数据源配置 2008-05-12 19:41 | 银河使者
在tomcat6.x中有很多位置可以配置数据源。如在<tomcat安装目录>\conf\Catalina\localhost目录中放一个和上下文路径同名的xml文件,并使用如下的格式配置:

<Context path="/samples" docBase="samples" debug="0">
<Resource name="jdbc/jdbcdemo" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK"
username="root"
password="1234"
maxActive="200"
maxIdle="50"
maxWait="3000"/>

</Context>

  回复  更多评论
  
# re: Tomcat6.x 数据源配置 2008-05-12 23:10 | Jak.Shen
@银河使者

多谢回复!同意你的说法,我写的只是其中的一种!
  回复  更多评论
  
# re: Tomcat6 数据源配置 2008-11-12 23:22 | lizn
我已经按照设置全弄好了 运行jsp 他说找不到 com.mysql.jdbc.Driver 我已经放在lib里了 是那个jar包 我用DriverManager.getConnection()都可以连接数据库的,就是数据源这个不行,能帮帮忙吗  回复  更多评论
  
# re: Tomcat6 数据源配置 2008-11-12 23:24 | lizn
噢 他说cannot load driverclass  回复  更多评论
  
# re: Tomcat6 数据源配置 2008-11-13 19:37 | Huaxu's
@lizn
按照上面的配置正常的话应该是可以成功的!
贴一下你的代码吧,这样我才好看。
  回复  更多评论
  
# re: Tomcat6 数据源配置 2008-11-15 18:53 | lizn
@Huaxu's
ok 谢谢了
<Context>


<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/bookstore" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="123456" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bookstore?autoReconnect=true"/>

</Context> //这是context文件

//以下是index.jsp 转换的时候从conn=ds.connection()抛出异常 说
//can not load"com.mysql.jdbc.Driver"
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*,javax.sql.*,javax.naming.*" %>

<html>
<head>
<title>留言板</title>
</head>
<body>
<a href="say.html">我要留言</a>
<%
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
Connection conn=ds.getConnection();

Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery("select * from guestbook order by gst_time desc");
rs.last();

int rowCount=rs.getRow();
if(rowCount==0){
out.println("当前没有任何留言");
return;
}

String strCurPage=request.getParameter("page");

int curPage;

if(strCurPage==null){
curPage=1;
}
else
curPage=Integer.parseInt(strCurPage);

int countPerPage=5;

int pageCount=(rowCount+countPerPage-1)/countPerPage;

rs.absolute((curPage-1)*countPerPage+1);

if(curPage==1){
%>
第一页&nbsp;&nbsp;&nbsp;&nbsp;
上一页&nbsp;&nbsp;&nbsp;&nbsp;
<%
}
else{
%>
<a href="index.jsp?page=<%=1%>">第一页</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="index.jsp?page=<%=curPage-1%>">上一页</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<%
}
if(curPage==pageCount){
%>
下一页&nbsp;&nbsp;&nbsp;&nbsp;
最后页&nbsp;&nbsp;&nbsp;&nbsp;
<%
}
else{
%>
<a href="index.jsp?page=<%=curPage+1%>">下一页</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="index.jsp?page=<%=pageCount%>">最后页</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<%

}

int i=0;

while(i<countPerPage&&!rs.isAfterLast()){
out.println("<hr color='blue' size='2'><br>");
out.println("用户名:"+rs.getString("gst_user"));
out.println("&nbsp;&nbsp;");

Timestamp ts=rs.getTimestamp("gst_time");
long lms=ts.getTime();
Date date=new Date(lms);
Time time=new Time(lms);

out.println("留言时间:"+date+" "+time);

out.println("&nbsp;&nbsp;");
out.println("用户IP: "+rs.getString("gst_ip")+"<br>");
out.println("主题:"+rs.getString("gst_title")+"<br>");
out.println("内容:"+rs.getString("gst_content"));
i++;
rs.next();
}

rs.close();
stmt.close();
conn.close();
%>
</body>
</html>  回复  更多评论
  
# re: Tomcat6 数据源配置 2008-11-16 00:58 | Huaxu's

@lizn


测试了你的代码,配置是没有问题的。测试代码如下(只是数据库换成了test,密码换成我的mysql):

tomcat配置如下:


1<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource" 

2maxActive="100" maxIdle="30" maxWait="10000" 

3username="root" password="mysql" driverClassName="com.mysql.jdbc.Driver" 

4url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>



jsp代码如下:


 1<%@ page contentType="text/html;charset=gb2312"

 2 import="java.sql.*,javax.sql.*,javax.naming.*"
%>

 3

 4<html>

 5 <head>

 6  <title>留言板</title>

 7 </head>

 8 <body>

 9  <href="#">我要留言</a>

10  <%

11   Context ctx = new InitialContext();

12   DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/test");

13   Connection conn = ds.getConnection();

14   out.println(conn);

15  
%>

16 </body>

17</html>

18

输出为:


1org.apache.tomcat.dbcp.dbcp.PoolableConnection@160c4b0 



我用的驱动为:mysql-connector-java-5.0.8 下载地址:http://dev.mysql.com/downloads/connector/j/5.0.html

Tomcat为:5.5.29版



建议你换个驱动试一下。


  回复  更多评论
  
# re: Tomcat6 数据源配置 2008-11-16 21:03 | lizn
@Huaxu's
试了一下 还是那个问题 我也是那个驱动 估计tomcat6就不一样 我换5.5吧 谢谢了  回复  更多评论
  
# re: Tomcat6 数据源配置 2008-11-17 23:37 | Huaxu's
@lizn
不是的。tomcat6是可以的。测试结果为:
jdbc:mysql://localhost:3306/test?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver
你找一下其它的原因。  回复  更多评论
  
# re: Tomcat6 数据源配置 2008-11-18 23:05 | lizn
我也不清楚我这里为什么不行 我换了tomcat 5.5就ok了  回复  更多评论
  
# Virusnyi Marketing 2009-05-18 12:25 | Virusnyi Marketing
This blog is very informative and thanks for the updates and the blog colour and desiging is very beautiful after seeing this u must be a creative man.
I am from Vietnam and learning to read in English, please tell me right I wrote the following sentence: "The article describes the effectiveness of seo analysis as one of the essentials.Expert search engine optimisation services by our seo works company consultants deliver firm page google organic results for companies across australia."

Regards :o Gerda.  回复  更多评论
  
# re: Tomcat6 数据源配置[未登录] 2010-02-21 22:53 | zhou
我的是莫名其妙的好了,刚开始以为是驱动的问题,后来换了竟然通过了,可是后来又换回去还是通过了,无聊。只是还有个问题,myeclipse内置的猫为什么不行,提示无法加载驱动类?  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
嗨117