posts - 189,comments - 115,trackbacks - 0
Tomcat&mysql

SQL

        mysqlshow   数据库结构查询
        mysqlaccess 用户权限查询
        mysqladmin  超级用户管理工具
        mysqldump       dump工具
        isamchk         检查,优化,修补数据工具
        mysqlimport 从txt文件输入数据库
        mysql_install_db        系统数据库安装脚本
        mysqld          守护进程               
        safe_mysqld     启动脚本
       
        1.未在db表中列出的任意数据库,user表中的权限都为有效.
        可只在user表中设置某些superuser的权限,而在db表中不涉及.
        2.host只用来维护有效的server,缺省为空表,即为局域网内
        的所有机器.
        3.可用mysql -u root mysql命令修改权限.
        4.权限匹配时遵循的原则是:
                a. 表顺序: host -> db -> user;
                b. host和db域可有通配符%和_,表示所有和单个字符;
                c. 同一表中,也是 host->db->user域的顺序,无通配符优
                于有通配符;
                d. user域为空(''),表示匹配所有其他的用户;
                e. host可为机器名,localhost,IP,或通配符(如192.10.10.%),
                        但不允许用数字和点号开头(如123.321.1.edu.cn);
                f. 各表中的权限最后相"或";
        5.修改完之后,执行mysqladmin -u root reload更新. 
        
       
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配置
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:


<?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&amp;useUnicode=true&amp;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>

 

Q:如何配制DBCP只应用于指定的web application呢?

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>

 

Q:我如何解决中乱码问题?

A:在配制mysql的数据库url时我们加入了useUnicode=true&characterEncoding=GB2312参数,指定数据库编码方式为GB2312,其它还需要做的就是常用的一些方式,如指定 <%@ page contentType = "text/html; charset=gb2312" %>、使用filter等,这方面的资料很多,这里不再累述。

这里需要注意的是url中的“&“符号,如果手动在.xml内配制时,需将其转换为“&”

posted on 2006-03-25 20:13 MEYE 阅读(549) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: