jndi mysql

简介

下面这些的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

在例子的&lt;/Context&gt;标志与关闭局部主机的&lt;/Host&gt;标志之间添加它  </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页面供以后使用

&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %&gt; 
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; 

&lt;sql:query var="rs" dataSource="jdbc/TestDB"&gt; 
select id, foo, bar from testdata 
&lt;/sql:query&gt; 

&lt;html&gt; 
&lt;head&gt; 
&lt;title&gt;DB Test&lt;/title&gt; 
&lt;/head&gt; 
&lt;body&gt; 

&lt;h2&gt;Results&lt;/h2&gt; 

&lt;c:forEach var="row" items="${rs.rows}"&gt; 
Foo ${row.foo}&lt;br/&gt; 
Bar ${row.bar}&lt;br/&gt; 
&lt;/c:forEach&gt; 

&lt;/body&gt; 
&lt;/html&gt;

 

JSP页面利用JSTL 's SQL 和 Core taglibs 你可以从Sun's Java Web Services Developer Pack 得到它或者从Jakarta Taglib Standard 1.1 project 得到它——确保你拿到的是1.1.x发行版当你有了JSTL以后jstl.jarstandard.jar复制到你的网络程序的WEB-INF/lib目录里

最后你的网络程序作为DBTest.war的warfile形式或以名叫DBTest的子目录形式部署到$CATALINA_HOME/webapps 里面

当部署完成后在一个浏览器里指向http://localhost:8080/DBTest/test.jsp就可以看到你辛勤劳动的成果了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值