1.需要的文件:
mysql-5.0.27-win32.zip(安装文件),mysql-connector-java-5.0.4-bin.jar(连接驱动程序),apache-tomcat-6.0.10.exe(安装文件)
3.配置你的应用下的web.xml中的
之间加入:
4.大功告成,不用在原来的server.xml里面配置了,下面就可以编写测试程序了,这个网上就很多了,主要的就上面,当然要把连接驱动程序都放到tomcat6下的lib下面.测试代码如下:
2.配置tomcat下的conf下的context.xml文件,在 之间添加连接池如下:
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test"
username="root"
password="root"
maxActive="100"
maxIdle="30"
maxWait="10000" />
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test"
username="root"
password="root"
maxActive="100"
maxIdle="30"
maxWait="10000" />
<
resource-ref
>
< description > DB Connection </ description >
< res-ref-name > jdbc/mysqlx </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
</ resource-ref >
< description > DB Connection </ description >
< res-ref-name > jdbc/mysqlx </ res-ref-name >
< res-type > javax.sql.DataSource </ res-type >
< res-auth > Container </ res-auth >
</ resource-ref >
4.大功告成,不用在原来的server.xml里面配置了,下面就可以编写测试程序了,这个网上就很多了,主要的就上面,当然要把连接驱动程序都放到tomcat6下的lib下面.测试代码如下:
<!
doctype html
public
"
-//w3c//dtd html 4.0 transitional//en
"
" http://www.w3.org/TR/REC-html40/strict.dtd " >
<% @ page import = " java.sql.* " %>
<% @ page import = " javax.sql.* " %>
<% @ page import = " javax.naming.* " %>
<% @ page session = " false " %>
" Content-Type " content = " text/html; charset=gb2312 " >
<%
out.print( " 我的测试开始 " );
DataSource ds = null ;
try {
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
//提示:users必须是数据库已有的表,
//这里的数据库前文提及的Data Source URL配置里包含的数据库。
String strSql = " select * from users";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(1));
}
out.print("我的测试结束");
}
catch (Exception ex) {
out.print(“出现例外,信息是:”+ex.getMessage());
ex.printStackTrace();
}
%>
" http://www.w3.org/TR/REC-html40/strict.dtd " >
<% @ page import = " java.sql.* " %>
<% @ page import = " javax.sql.* " %>
<% @ page import = " javax.naming.* " %>
<% @ page session = " false " %>
" Content-Type " content = " text/html; charset=gb2312 " >
<%
out.print( " 我的测试开始 " );
DataSource ds = null ;
try {
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
//提示:users必须是数据库已有的表,
//这里的数据库前文提及的Data Source URL配置里包含的数据库。
String strSql = " select * from users";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(1));
}
out.print("我的测试结束");
}
catch (Exception ex) {
out.print(“出现例外,信息是:”+ex.getMessage());
ex.printStackTrace();
}
%>
提醒下大家:
mysql-connector-java-5.0.4-bin.jar OK
mysql-connector-java-5.0.5-bin.jar error
请大家在测试的时候一定要使用
mysql-connector-java-5.0.4-bin.jar
或
mysql-connector-java-3.1.12-bin.jar
都通过......