如下文档如果无特别声明,%TOMCAT-HOME%代表TOMCAT安装目录。
%MYSQL%代表MYSQL安装目录。
1)相关平台
Tomcat5.5.4+MySql5.0+Jdk1.5
2)相关的安装就不多说。其中需要说明的是把
mysql-connector-java-3.0.16-ga-bin.jar放到{%TOMCAT-HOME%/COMMON/LIB} 这个目录中
其中mysql-connector-java-3.0.16-ga-bin.jar是MYSQL的官方JDBC DRIVER 。
在CMD命令中来到%MYSQL%/BIN 目录
mysql(对于在本机器中安装MYSQL,如果不能进入数据库是因为你设置了密码:这个时间你用mysql -uroot不带密码就可以进入)
创建数据库:hibernate
create database hibernate;
use hibernate;
CREATE TABLE cat (
cat_id varchar(20) NOT NULL PRIMARY KEY ,
name varchar(20) NOT NULL,
sex char(1),
weight float
);
insert cat values('1', 'hibernate', 'F', 5);
insert cat values('2', 'test', 'F', 10);
在MYSQL数据库中建 USER{hibernate}PWD{hibernate}
grant all privileges on *.* to hibernate@localhost identified by 'hibernate' with grant option(这个是建用户)
3)找到%TOMCAT-HOME%CONF/SERVER.XML
在</HOST>前添加(具体可以参照TOMCAT文档中的DBCP配置)
<Context path="/hibenate" docBase="hibernate"
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/hibernate" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="hibernate" password="hibernate" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/hibernate?autoReconnect=true"/>
</Context>
注意上面红色的地方和你的JDBC DRIVER有关。蓝色的和你的MYSQL数据库有关
在%TOMCAT-HOME/WEBAPPS目录下建一个hibernate应用。
具体结构:
|hibernate
| ---classes
| ---lib
|web.xml(这个可以先不放内容)
|test.jsp(这个JSP是为测试DBCP是否成功的!)
test.jsp具体内容:
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.sql.*,javax.sql.DataSource,javax.naming.*"%>
<html>
<head><title>test.jsp</title></head>
<body bgcolor="#ffffff">
<h1>test Tomcat</h1>
<%
try
{
Context initCtx=new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/hibernate");
Connection conn=ds.getConnection();
out.println("data from database:<br>");
Statement stmt=conn.createStatement();
ResultSet rs =stmt.executeQuery("select * from cat");
while(rs.next())
{
out.println(rs.getString(1));
out.println(rs.getString(2));
}
rs.close();
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
如果成功,说明DBCP成功了!
今天先写到这里。谢谢!
学习HIBERNATE
最新推荐文章于 2024-08-08 16:26:38 发布