mysql tomcat 配置

1. MySQL configuration

Ensure that you follow these instructions as variations can cause problems.

Create a new test user, a new database and a single test table. Your MySQL user must have a password assigned. The driver will fail if you try to connect with an empty password.

mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置
mysql <wbr>tomcat <wbr>配置
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);
mysql <wbr>tomcat <wbr>配置
mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置
Note: the above user should be removed once testing is complete!

 

Next insert some test data into the testdata table.

mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置
mysql <wbr>tomcat <wbr>配置
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>
mysql <wbr>tomcat <wbr>配置
mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置

 

2. Context configuration

Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to your Context.

For example:

mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置
mysql <wbr>tomcat <wbr>配置
<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 -1 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.
         -->

  <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"/>

</Context>
mysql <wbr>tomcat <wbr>配置
mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置

 

3. web.xml configuration

Now create a WEB-INF/web.xml for this test application.

mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置mysql <wbr>tomcat <wbr>配置
mysql <wbr>tomcat <wbr>配置
<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>

放在server.xml中

<Context path="/seam" reloadable="false" docBase="D:\workspace\tomcatTest\WebContent" workDir="D:\workspace\tomcatTest\work" >

         <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://127.0.0.1:3306/test"/>
     </Context>

 

放在hibernate.cfg.xml中

 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
       
       <property name="hbm2ddl.auto">create </property>
        <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
        <property name="transaction.flush_before_completion">true</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值