手机站
网通分站
电信主站
密 码:
用户名:
热门关键字:  虚拟主机  cn域名  asd  ddd  域名注册
当前位置 : 主页>程序设计>Java技术>列表

TOMCAT/MySQL JNDI使用配置说明

来源:互联网 作者:west263.com 时间:2008-02-23 点击:
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

下载安装Tomcat & MySQL

Tomcat目前版本为 5.0.28
下载地址: 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.jar

MySQL目前版本为 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('<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;   //退出 

下载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

假设:数据库:gamedb 用户名:gamedb 密码:gamedb

第一步: 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
A: 在yourWebApp\WEB-INF\web.XML中增加
    <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>

  • Q:出现org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '?' for onnect URL 'null',这是为什么?
A: 在%TOMCAT_HOME%\conf\Catalina\localhost下找到你的web应用对应的.xml文件,如test.xml,并在此文件的下添入代码:
<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:

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

共2页: 上一页 1 [2] 下一页
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名
注册