C:\Program Files\Apache Software Foundation\Tomcat 5.0\bin\bootstrap.jar;C:\Program Files\Java\jdk1.5.0\lib\tools.jarMySQL目前版本为 4.0.21
c:\mysql\bin>mysqld //启动mysql的服务进程 c:\mysql\bin\mysql -uroot //连接到mysql数据库 mysql>update user set password=PASSWORD('<new_root_password>') where user='root'; mysql>flush privileges; 上面的操作是修改root用户的密码,'<new_root_password>'是你设置的密码。 mysql>create database gamedb; //创建gamedb库 mysql>connect gamedb; //连接到gamedb库 mysql>source gamedb.sql; //gamedb初始化建表 mysql>GRANT SELECT, INSERT, UPDATE, DELETE, INDEX, ALTER, CREATE, DROP, REFERENCES, LOCK TABLES, CREATE TEMPORARY TABLES ON gamedb.* TO gamedb@localhost IDENTIFIED BY '<gamedb_password>'; mysql>flush privileges; 上面是建立gamedb用户,<gamedb_password>为gamedb用户的密码。 mysql>exit; //退出
第一步: http://127.0.0.1:8080,进入tomcat页,用tomcat的 Tomcat Administration--->Resources--->Data Sources页面添加,参数如下:
JNDI Name: jdbc/mysql Data Source URL: jdbc:mysql://localhost:3306/gamedb?autoReconnect=true&useUnicode=true&characterEncoding=GB2312 JDBC Driver Class: com.mysql.jdbc.Driver User Name: gamedb Password: gamedb Max. Active Connections: 4 Max. Idle Connections: 2 Max. Wait for Connection:5000 Validation Query ://不添
第二步: 试页内加入代码你自己在mysql里建个表检单测试一下吧,我这里以ght_users表为例,在你的应用下做一个测试用的test.jsp,然后http访问这个jsp页,test.jsp代码如下
<%@ page contentType = "text/html; charset=gb2312" %> <%@ page import = "java.sql.*" %> <%@ page import = "javax.naming.*" %> <%@ page import = "javax.sql.*" %> <% Context ctx=null; DataSource ds=null; Connection conn=null; Statement stmt=null; ResultSet rs=null; try{ ctx = new InitialContext(); if( ctx == null ) out.println("no context"); ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql"); if( ds == null ) out.println("no datasource"); conn = ds.getConnection(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String strSql = " select * from ght_users"; rs = stmt.executeQuery(strSql); while(rs.next()){ out.println(rs.getString(1)); } } catch(Exception ex){ ex.printStackTrace(); out.println(ex.toString()); } finally{ if( rs != null ) rs.close(); if( stmt != null ) stmt.close(); if( conn != null) conn.close(); if( ctx != null ) ctx.close(); } %>
<resource-ref> <description>jdbc/mysql</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>重启tomcat。
for tomcat-5.0.18 i use ROOT in tomcat as by web app i put the following entry in E:\jakarta-tomcat-5.0.18\conf\Catalina\localhost\ROOT.xml file for tomcat-4.1.18 i use ROOT in tomcat as my webapp i put the following entry in E:\qrules\tomcat\jakarta-tomcat-4.1.18\conf\server.xml <ResourceLink name="jdbc/quickstart" type="javax.sql.DataSource" global="jdbc/quickstart"/>Web界面配DBCP时,生成的是服务器的全局JNDI资源,查看%TOMCAT_HOME%\conf\server.xml可以得知tomcat修改了server.xml,在<server>下的<GlobalNamingResources>下添入了一些数据后的server.xml:
<?xml version='1.0' encoding='utf-8'?> <Server> <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/> <GlobalNamingResources> <Environment name="simpleValue" type="java.lang.Integer" value="30"/> <Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/> <Resource name="jdbc/mysql" type="javax.sql.DataSource"/> <ResourceParams name="UserDatabase"> <parameter> <name>factory</name> <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value> </parameter> <parameter> <name>pathname</name> <value>conf/tomcat-users.xml</value> </parameter> </ResourceParams> <ResourceParams name="jdbc/mysql"> <parameter> <name>maxWait</name> <value>5000</value> </parameter> <parameter> <name>maxActive</name> <value>4</value> </parameter> <parameter> <name>password</name> <value>gamedb</value> </parameter> <parameter> <name>url</name> <value>jdbc:mysql://localhost:3306/gamedb?autoReconnect=true&useUnicode=true&characterEncoding=GB2312</value> </parameter> <parameter> <name>driverClassName</name> <value>com.mysql.jdbc.Driver</value> </parameter> <parameter> <name>maxIdle</name> <value>2</value> </parameter> <parameter> <name>username</name> <value>gamedb</value> </parameter> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> </ResourceParams> </GlobalNamingResources> <Service name="Catalina"> <Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25"> </Connector> <Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443"> </Connector> <Engine defaultHost="localhost" name="Catalina"> <Host appBase="webapps" name="localhost"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/> </Host> <Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/> <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/> </Engine> </Service> </Server>
A:tomcat 5.x以后将web application的从server.xml里分离了出来,放在了 %TOMCAT_HOME%\conf\Catalina\localhost下,如你的应用为test, 那么在这个目录下就会有一个相应的test.xml与之对应,如:
<?xml version='1.0' encoding='utf-8'?> <Context displayName="Tomcat Test Application" docBase="C:/Program Files/Apache Software Foundation/Tomcat 5.5/server/webapps/test" path="/test" privileged="true" workDir="work\Catalina\localhost\test"> ... </Context>
A:在配制mysql的数据库url时我们加入了useUnicode=true&characterEncoding=GB2312参数,指定数据库编码方式为GB2312,其它还需要做的就是常用的一些方式,如指定 <%@ page contentType = "text/html; charset=gb2312" %>、使用filter等,这方面的资料很多,这里不再累述。
这里需要注意的是url中的“&“符号,如果手动在.xml内配制时,需将其转换为“&”