准备环境
参见本人上一篇文章《在Windows 2000 Server上安装配置Apache+Tomcat》
下载以下软件
1.mysql-4.0.21
2.mymanager28 //MySQL图形管理工具,很好用。中文显示不太好。
//实现对数据库的操作和管理。
[Setup Mysql]
1.run mysql-4.0.21-win/setup.exe on c:\mysql
[Config Mysql]
1.run c:\mysql\bin\winmysqladmin.exe
2.注意my.ini文件中[mysqld]basedir内容的正确性
[mysqld]
basedir=c:/mysql
[Setup Mysql Manager]
1.run mymanager28/setup.exe
[Test Mysql]
1.Registe mysql 数据库
user:root
password:空(mysql安装时指定)
2.Connect mysql
成功标示:find table information
[Setup Mysql JDBC driver]
1.copy mysql-connector-java-3.0.15-ga-bin.jar复制到%TOMCAT_HOME%\common\lib
[Config Tomcat 数据库连接池 for Mysql]
1.create Mysql DB:study
2.create Mysql user:test password:test
3.http://127.0.0.1:8090,进入tomcat页,用tomcat的Tomcat Administration--->Resources--->Data Sources页面添加,参数如下:
Name: jdbc/mysql
Data Source URL: jdbc:mysql://localhost:3306/study?autoReconnect=true&useUnicode=true&characterEncoding=GB2312
JDBC Driver Class: com.mysql.jdbc.Driver
ser Name:test
Password:test
Max. Active Connections:4
Max. Idle Connections:2
Max. Wait for Connection:5000
Validation Query ://不添
4.Modify $Tomcat_HOME$/conf/server.xml, 拷贝步骤3对server.xml的修改部分到examples的context中。
[Test Mysql on Tomcat]
1.Mysql:在study中create table 表cat
Column | Type | Modifiers
-------+-----------------------+-----------
cat_id | character(32) | not null
name | character varying(16) | not null
sex | character(1) |
weight | real |
Indexes: cat_pkey primary key btree (cat_id)
插入一行纪录到CAT表中
2.Create file test.jsp,然后http访问这个jsp页,test.jsp代码如下
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " select * from cat";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println(rs.getString(2));
}
}
catch(Exception ex){
ex.printStackTrace();
}
%>
文件保存在目录$Tomcat_HOME$/webapps/examples下
3. Http://127.0.0.1:8090/examples/test.jsp即可看到结果:CAT NAME。
本文地址:http://com.8s8s.com/it/it15056.htm