1)启动Tomcat服务器,打开浏览器,输入http://localhost:8080/admin(其中localhost是名称服务器或称为主机),
进入管理界面的登陆页面,这时候请输入原来安装时要求输入的用户名和密码,登陆到管理界面,
2)选择Resources-Data sources进入配置数据源界面,选择
Data Source Actions ->选择Create New Data Source,进入配置详细信息界面
主要内容例如下:
JNDI Name: ->jdbc/mysql
Data Source URL ->jdbc:mysql://localhost:3306/test
JDBC Driver Class-> org.gjt.mm.mysql.Driver
3)修改\conf\Catalina\localhost目录下建立一个xml文件,名称为你所发布的web应用的名称.xml,(如testpool.xml)打开添加内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
password="123456"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="2"
maxWait="50"
username="root"
url="jdbc:mysql://localhost:3306/test"
maxActive="4"/>
</Context>
内容同conf/server.xml中<GlobalNamingResources>
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
password="123456"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="2"
maxWait="50"
username="root"
url="jdbc:mysql://localhost:3306/test"
maxActive="4"/>
</GlobalNamingResources>
少了这一步会报错:Cannot create JDBC driver of class '' for connect URL 'null'
4)修改web.xml
打开%TOMCAT_HOME%\conf\web.xml或yourwebapp/web-inf/web.xml,添加以下内容:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
注意res-ref-name填写的内容要与在上文提到的JNDI Name名称一致。
到这里,配置工作就基本完成了!
5)引用JNDI时用"java:comp/env/jdbc/mysql";
建立文件测试 test.jsp:
<%@page contentType="text/html;charset=utf-8" %>
<%@page import="java.sql.*" %>
<%@page import="javax.sql.*" %>
<%@page import="javax.naming.*" %>
<html>
<head>
<title>Tomcat连接池测试</title>
</head>
<body>
<%
Context ctx=new InitialContext();
Connection conn=null;
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn=ds.getConnection();
Statement stmt=conn.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("select * from testexample");
while(rs.next()){
out.println(rs.getInt(1));
out.println(rs.getString(2));
out.println(rs.getString(3));
}
out.println("数据库操作成功!");
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
转自:http://www.blogjava.net/flustar/archive/2007/04/17/111362.html
分享到:
相关推荐
包含以下java源文件: ...com.google.gson.JsonNull.class com.google.gson.JsonObject.class com.google.gson.JsonParseException.class com.google.gson.JsonParser.class com.google.gson.JsonPrimitive.class ...
tomcat jndi 的困惑 Cannot create JDBC driver of class '' for connect URL 'null'
mapgis67补丁_解决cannot错误.rar
javax.naming.NamingException: Cannot create resource instance类加载异常,希望可以帮助跟我一样错误的人。
启动报错:javax.servlet.ServletException:javax.xml.stream.FactoryConfigurationError: Provider for class javax.xml.stream.XMLInputFactory cannot be created。引入jar包解决问题
带详细解决方案说明文档,提供三种解决方式,另外包含stax2-api.jar,stax2-api-source.jar,woodstox-core-asl-4.4.1.jar,stax-ex.jar,wstx-asl-3.2.9.jar等jar文件,亲测有效。
将文件解压到D盘,navicat选择OCI路径是D:\instantclient_11_2\oci.dll sqlpuls选择 d:\instantclient_11_2\sqlplus.exe
这是在struts1的练习中总结出来的错误,然后找各种的资料得到了如下的总结,希望帮到有需要的程序猿。错误原现: Name jdbc is not bound in this ...Cannot create JDBC driver of class '' for connect URL 'null'
解决Failed to connect to remote VM com.sun.jdi.connect.spi.ClosedConnectionException错误,tomcat远程调试
主要介绍了解决ubuntu vps安装docker时报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.问题的相关资料,文中介绍非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
Test6.java:26: cannot find symbol symbol : variable j location: class Test6 if(j ) { 4. 找不到类的错误 Main.java:4: cannot find symbol symbol : class T1 location: class Main T1 a = new T1();
解决Cannot find Graphviz.zip
在vc6上运行程序的时候提示 NMSQL.DLL This required file cannot be loaded. Please re-install Microsoft Visual C++ 压缩包中有解决办法
NULL 博文链接:https://aimema.iteye.com/blog/1273262
解决The type org.springframework.dao.support.DaoSupport cannot be resolved.bao报错
解决mongo数据插入时 报错问题 mogodb插入数据时报错Can't find a codec for class java.math.BigDecimal
在linux上启动jmeter-server报错: ...Server failed to start: java.rmi.RemoteException: Cannot start. localhost is a loopback address. An error occurred: Cannot start. localhost is a loopback address.
NULL 博文链接:https://corrinejtt.iteye.com/blog/608248
一个CXF webservice 实例,兼容tomcat和weblogic; 已解决了报错:javax.xml.ws.soap.SOAPFaultException: Cannot create a secure XMLInputFactory
jce.jar/jsse.jar解决The import javax.crypto cannot be resolved的问题