简介
下面这些的MySQL和JDBC驱动的版本可以运作:
- MySQL 3.23.47, MySQL 3.23.47 using InnoDB,, MySQL 3.23.58, MySQL 4.0.1alpha
- Connector/J 3.0.11-stable (the official JDBC Driver)
- mm.mysql 2.0.14 (an old 3rd party JDBC Driver)
在你开始之前别忘记复制一份JDBC驱动的jar文件到$CATALINA_HOME/common/lib里面
$CATALINA_HOME/common/lib
MySQL 配置
你一定要遵循这些指导说明因为变动会产生问题
产生一个新的测试用户一个新的数据库和一个测试表格你的MySQL用户必须要有个指派的密码如果你试图用空密码连接驱动会失败
mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@localhost -> IDENTIFIED BY 'javadude' WITH GRANT OPTION; mysql> create database javatest; mysql> use javatest; mysql> create table testdata ( -> id int not null auto_increment primary key, -> foo varchar(25), -> bar int); 注意:当测试完成以后 上面的用户应该被删除掉
下一步在testdata表格中加入一些测试数据
mysql> insert into testdata values(null, 'hello', 12345); Query OK, 1 row affected (0.00 sec) mysql> select * from testdata; +----+-------+-------+ | ID | FOO | BAR | +----+-------+-------+ | 1 | hello | 12345 | +----+-------+-------+ 1 row in set (0.00 sec) mysql>
server.xml 配置
通过向$CATALINA_HOME/conf/server.xml里面加入一个声明来在Tomcat里配置JNDI DataSource
$CATALINA_HOME/conf/server.xml
在例子的</Context>标志与关闭局部主机的</Host>标志之间添加它
</Context>
</Host>
<Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true" crossContext="true"> <!-- maxActive: Maximum number of dB connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to 0 for no limit. --> <!-- maxIdle: Maximum number of idle dB connections to retain in pool. Set to -1 for no limit. See also the DBCP documentation on this and the minEvictableIdleTimeMillis configuration parameter. --> <!-- maxWait: Maximum time to wait for a dB connection to become available in ms, in this example 10 seconds. An Exception is thrown if this timeout is exceeded. Set to -1 to wait indefinitely. --> <!-- username and password: MySQL dB username and password for dB connections --> <!-- driverClassName: Class name for the old mm.mysql JDBC driver is org.gjt.mm.mysql.Driver - we recommend using Connector/J though. Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver. --> <!-- url: The JDBC connection url for connecting to your MySQL dB. The autoReconnect=true argument to the url makes sure that the mm.mysql JDBC Driver will automatically reconnect if mysqld closed the connection. mysqld by default closes idle connections after 8 hours. --> <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/> </Context>
web.xml 配置
现在为这个测试程序产生一个
WEB-INF/web.xml
文件
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>MySQL Test App</description> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
测试代码
现在产生一个简单的
test.jsp
页面供以后使用
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <sql:query var="rs" dataSource="jdbc/TestDB"> select id, foo, bar from testdata </sql:query> <html> <head> <title>DB Test</title> </head> <body> <h2>Results</h2> <c:forEach var="row" items="${rs.rows}"> Foo ${row.foo}<br/> Bar ${row.bar}<br/> </c:forEach> </body> </html>
JSP页面利用JSTL 's SQL 和 Core taglibs 你可以从Sun's Java Web Services Developer Pack 得到它或者从Jakarta Taglib Standard 1.1 project 得到它——确保你拿到的是1.1.x发行版当你有了JSTL以后
jstl.jar
和standard.jar
复制到你的网络程序的WEB-INF/lib
目录里最后你的网络程序作为
的warfile形式或以名叫DBTest的子目录形式部署到
DBTest
.war$CATALINA_HOME/webapps
里面当部署完成后在一个浏览器里指向
http://localhost:8080/DBTest/test.jsp
就可以看到你辛勤劳动的成果了
jndi mysql
最新推荐文章于 2021-02-17 04:06:30 发布