进行开发中要利用数据库连接池提升性能。
在联系由于多次调试不通,错误如下:
Cannot load JDBC driver class 'org.gjt.mm.mysql.Driver'
特贴出解决办法:
1.将mysql-connector-java-5.1.23-bin.jar放到tomcat/lib下面,放到WEB-INFO/lib下没有效果。
2.配置Context,打开server.xml添加内容如下:
3.配置web.xml
4.添加测试脚本:datasource.jsp
在联系由于多次调试不通,错误如下:
Cannot load JDBC driver class 'org.gjt.mm.mysql.Driver'
特贴出解决办法:
1.将mysql-connector-java-5.1.23-bin.jar放到tomcat/lib下面,放到WEB-INFO/lib下没有效果。
2.配置Context,打开server.xml添加内容如下:
<Context path="/datasource" docBase="E:\Myeclipse\datasource\WebRoot" reloadable="true">
<Resource
name="jdbc/mldn"
type="javax.sql.DataSource"
password="123456"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="2"
maxWait="50"
username="root"
url="jdbc:mysql://localhost:3310/mldn"
maxActive="4"/>
</Context>
3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mldn</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
4.添加测试脚本:datasource.jsp
<%@ page language="java" contentType="text/html" pageEncoding="utf8"%>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'use_javabean_demo01.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
String DSNAME = "java:comp/env/jdbc/mldn"; //JNDI名称
Context ctx = new InitialContext(); //初始化名称查找上下文
DataSource ds = (DataSource)ctx.lookup(DSNAME); //取得DataSource的实例
Connection conn = ds.getConnection(); //取得数据库连接
%>
<%=conn%>
<%conn.close();%>
</body>
</html>