Topic: Tomcat 4.0 +mysql 4.0转换到Tomcat 5.5.x+mysql4.0经验谈(例子) |
Print this page |
1.Tomcat 4.0 +mysql 4.0转换到Tomcat 5.5.x+mysql4.0经验谈(例子) | Copy to clipboard |
Posted by: shunzi Posted on: 2005-12-13 15:37 Cannot create JDBC driver of class '' for connect URL 'null' 确实用了很长的时间,在Tomcat 4.0 +mysql 4.0 是可以轻松的实现DataSource,不出错。但是转到Tomcat 5.5.x+mysql4.0 总是是报错。 准备解决问题,于是首先想到的是到mysql的官方网站寻求帮助,的确发现有很多的帮助。参照:http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html 具体步骤: 1. MySQL configuration 在mysql中,添加一个用户(dbuser),密码(1234),并赋予一定的权限,然后创建数据库( BookDB ),创建表(books).最后启动 winmysqladmin.exe。 2. server.xml configuration 在Tomcat 4.0 下 server.xml configuration 与 Tomcat 5.5.x 下不太一样!注意由原来的很长,5.5.x 缩减到几行。 <Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true" crossContext="true"> <!--**************************************** 刚开始时,我的发布文件夹在,${Tomacat-home}\webapps\helloapp,但是总是出错,后来我修改为 c:\helloapp,后又改server.xml中的Context: <Context path="/BookDB" docBase="c:\BookDB" debug="0" 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/BookDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="dbuser" password="1234" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/BookDB?autoReconnect=true"/> **************************************--> <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/> </Context> 3. web.xml configuration 原样复制过来,放在 c:\helloapp\WEB-INF下( 注意为 BookDB ) <web-app > <description>MySQL Test App</description> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/BookDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app> 4.jdbc configuration 在系统的path 中增加jdbc.jar的路径。刚开始时,总是不成功,就想是不是驱动程序版本太底,于是我下载了好几个版本的jdbc,如:mysql-connector-java-3.1.12.zip,mysql-connector-java-3.0.17-ga.zip,mysql-connector-java-3.2.0-alpha.zip,释放出来的发现 jdbc 的名字很长,开始没在意,直接使用,但是有很长的驱动程序名称的驱动老是不成功,我都试用过。最后我把使用jdbc 的名字改为 mysqldriver.jar (觉得舒服一些!),一试成功,同理,把不同版本的驱动都试用了一遍,发现都可以,只要名称为 mysqldriver. 5. DBTest.jsp 下面保存上面的更改,重新启动Tomcat(我用的是apache-tomcat-5.5.14.exe),在helloapp中添加DBTest.jsp,测试:http://localhost:8080/helloapp/DBTest.jsp OK ! Good luck ! successfully? <%@ page import="java.io.*"%> <%@ page import="java.util.*"%> <%@ page import="java.sql.*"%> <%@ page import="javax.sql.*"%> <%@ page import="javax.naming.*"%> <%@ page import="com.mysql.jdbc.Connection"%> <%@ page contentType="text/html; charset=GB2312" %> <html> <head> <title>DBTest.jsp</title> </head> <body> <% try { java.sql.Connection con; Statement stmt; ResultSet rs; Context ctx = new InitialContext(); DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/BookDB"); con = ds.getConnection(); stmt = con.createStatement(); //注意,你的比数据表的的结构 stmt.executeUpdate("INSERT INTO books (id,name,title,price) VALUES ('999','Tom','Tomcat Bible',44.5)"); rs = stmt.executeQuery("SELECT id,name,title,price from books"); out.println("<table border=1 width=400>"); while (rs.next()) { String col1 = rs.getString(1); String col2 = rs.getString(2); String col3 = rs.getString(3); float col4 = rs.getFloat(4); col1=new String(col1.getBytes("ISO-8859-1"),"GB2312"); col2=new String(col2.getBytes("ISO-8859-1"),"GB2312"); col3=new String(col3.getBytes("ISO-8859-1"),"GB2312"); out.println("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td></tr>"); } out.println("</table>"); stmt.executeUpdate("DELETE FROM books WHERE id='999'"); rs.close(); stmt.close(); con.close(); } catch (Exception e) {out.println(e.getMessage());} %> Welcome to JNDI DataSource </body> </html> 最后,祝你成功,我在机器上试用了不同的版本的数据库驱动程序,都可以。希望,能给各位有一点帮助,初次发帖,请多指教。 Email:cuibinglshun@hotmail.com |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |