下载地址: http://jakarta.apache.org/site/binindex.cgi
Tomcat配置
如果出现
Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK
...
Tomcat ClassPath配置如下:
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
下载地址: http://dev.mysql.com/downloads/mysql/4.0.html
MySQL配置
c:\mysql\bin>mysqld //启动mysql的服务进程 c:\mysql\bin\mysql -uroot //连接到mysql数据库 mysql>update user set password=PASSWORD('') where user='root'; mysql>flush privileges; 上面的操作是修改root用户的密码,' '是你设置的密码。 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 ' '; mysql>flush privileges; 上面是建立gamedb用户, 为gamedb用户的密码。 mysql>exit; //退出
下载MySQL JDBC
Connector/J 3.0.15-ga (MySQL.com提供的JDBC Driver)下载地址: http://dev.mysql.com/downloads/connector/j/3.0.html
安装JDBC
mysql-connector-java-3.0.15-ga.zip解压后将mysql-connector-java-3.0.15-ga-bin.jar 复制到%TOMCAT_HOME%\common\lib下
配置Tomcat
第一步: 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(); } %>
常见问题
- Q:出现javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
jdbc/mysql jdbc/mysql javax.sql.DataSource Container
- Q:出现org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '?' for onnect URL 'null',这是为什么?
重启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.xmlWeb界面配DBCP时,生成的是服务器的全局JNDI资源,查看%TOMCAT_HOME%\conf\server.xml可以得知tomcat修改了server.xml,在
factory org.apache.catalina.users.MemoryUserDatabaseFactory pathname conf/tomcat-users.xml maxWait 5000 maxActive 4 password gamedb url jdbc:mysql://localhost:3306/gamedb?autoReconnect=true&useUnicode=true&characterEncoding=GB2312 driverClassName com.mysql.jdbc.Driver maxIdle 2 username gamedb factory org.apache.commons.dbcp.BasicDataSourceFactory
- Q:如何配制DBCP只应用于指定的web application呢?
A:tomcat 5.x以后将web application的从server.xml里分离了出来,放在了 %TOMCAT_HOME%\conf\Catalina\localhost下,如你的应用为test, 那么在这个目录下就会有一个相应的test.xml与之对应,如:
...
- Q:我如何解决中乱码问题?
A:在配制mysql的数据库url时我们加入了useUnicode=true&characterEncoding=GB2312参数,指定数据库编码方式为GB2312,其它还需要做的就是常用的一些方式,如指定 <%@ page contentType = "text/html; charset=gb2312" %>、使用filter等,这方面的资料很多,这里不再累述。
这里需要注意的是url中的“&“符号,如果手动在.xml内配制时,需将其转换为“&”