hibernate+mysql+tomcat 数据源配置(丢掉IDE)
问题起因:用eclipse开发的项目在IDE中运行,访问一切正常,关掉IDE把项目发布到tomcat中直接用Tomcat来运行的时候只要涉及跟数据库打交道的地方就提示:No suitable driver
....的异常,jar包,环境变量的配置完全正确,百思不得其解,最后冷静思考察觉到没有告诉tomcat跟mysql交互的配置信息。。。。。。。
hibernate mysql tomcat 数据源配置及乱码解决方案
首先配置tomcat数据源
1
server.xml
要注意的地方是URL,在xml文件中,&是转义字符,要用&代替。?useUnicode=true&characterEncoding=GB2312
-----------------------------------------------------------------------------------
………………
<GlobalNamingResources>
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/thankyoupin"/>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
………………
------------------------------------------------------------------------------------------
2. web.xml 文件,添加资源引用(注意:名字要一致,apache-tomcat-6.0.35\conf 路径下的web.xml <web-app>标签内添加如下红色标注内容)
------------------------------------------------------------------------------------------
<web-app>
<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>
------------------------------------------------------------------------------------------
3 、context.xml 文件(apache-tomcat-6.0.35\conf 路径下的),设置数据源
------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/thankyoupin"/>
</Context>
------------------------------------------------------------------------------------------
4、 hibernate.cfg.xml 文件,设置数据源
------------------------------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">
java:/comp/env/jdbc/TestDB
</property>
<!-- 映射文件 -->
<mapping resource="com/thankyou/po/Dinner.hbm.xml"/>
<mapping resource="com/thankyou/po/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
------------------------------------------------------------------------------------------
好了,重启Tomcat 访问mysql成功
浙公网安备 33010602011771号