tomcat-mysql数据库连接池

news/2024/5/19 0:01:41 标签: 数据库连接池, tomcat, jdbc, 数据库, mysql, import

 

1、 安装Tomcat
在安装了jdk后,参考Tomcat for window 的安装向导,基本直接安装即可,注意:安装时会提示输入管理用户名和密码,这是以后会用到的用户名和密码,切记。
2、 安装MySql
默认安装即可。
3、 使用Tomcat的Web管理应用配置数据源
启动Tomcat服务器,打开浏览器,输入
http://localhost:8080/admin/(其中localhost是名称服务器或称为主机),进入管理界面的登陆页面,这时候请输入原来安装时要求输入的用户名和密码,登陆到管理界面,
选择Resources-Data sources进入配置数据源界面,选择Data Source Actions ->选择Create New Data Source,进入配置详细信息界面,内容如下:
JNDI Name: jdbc/mysql
Data Source URL: jdbc:mysql://localhost:3306/cs
JDBC Driver Class: com.mysql.jdbc.Driver
User Name: root
Password: ********
Max. Active Connections: 4
Max. Idle Connections: 2
Max. Wait for Connection: 5000
Validation Query:
要求输入的JNDI Name等信息,其中除了JDBC DriverClass之外,其他的可以根据你的需要填写。比如Data Source URL的内容可能是:jdbc:mysql:// IP或是名称/DataBaseName,其中DataBaseName是你的数据库名称,IP是你的数据库的所在的服务器的IP或是名称。最后点击Save->Commit Change.这样你的数据源的基本资料配置一半了。
4、 修改web.xml和%TOMCAT_HOME%/conf/Catalina/localhost下对应你的引用的配置文件
打开%TOMCAT_HOME%/conf/web.xml,在</web-app>的前面添加以下内容:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
注意res-ref-name填写的内容要与在上文提到的JNDI Name名称一致。
进入%TOMCAT_HOME%/conf/Catalina/localhost下,找到你的web应用对应的.xml文件(下面有我的web的目录结构),如 myweb.xml,并在此文件的下添入代码:
<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>
到这里,配置工作就基本完成了。
5、 其他注意事项
别忘了JDBC驱动程序mysql-connector-java-3.0.9-stable-bin.jar一定要放置到Tomcat的对应目录,你的JDBC驱动可能版比笔者高,不过只要能与所使用的MySql对应就可以了,因为我发现版本太低的JDBC驱动不能支持4.0.*版本的MySQL数据库,建议放置在%TOMCAT_HOME%/common/lib和应用的WEB-INF/lib下。两者有什么不同呢?其实一看就明白了,common/lib是所有的应用都可以使用的库文件位置。
重启你的Tomcat服务。
6、 编写测试代码
在应用的目录下建立一个Test.jsp文件,代码如下:
<!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" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<%
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();
}
%>
</head>
<body>
</body>
</html>
运行结果:
我的测试开始12345678我的测试结束,因为我的rs.getString(1) 在数据库就是存放12345678


本文来自CSDN博客:http://blog.csdn.net/yfm10/archive/2009/04/11/4065600.aspx


http://www.niftyadmin.cn/n/1051366.html

相关文章

Linux下查看CPU信息、机器型号等硬件信息命令

查看CPU信息&#xff08;型号&#xff09; # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CPU E5410 2.33GHz # cat /proc/cpuinfo | grep physical | uniq -c 4 physical id : 0 4 physical id : 1 设置root密码sudo…

Leetcode 16 3Sum Closest

Leetcode 16 3Sum Closest 题目描述 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one sol…

Struts2连接MySQL数据库

按照如下步骤连接MySQL数据库 :第一步 : 创建struts.xml并向其中添加如下xml代码 :Xml代码 <?xml version"1.0" encoding"UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuratio…

Leetcode 20 Valid Parentheses

Leetcode 20 Valid Parentheses 题目描述 Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac…

windows7,windows8,ubuntu三系统64位安装

三系统的安装和安装双系统基本一样&#xff0c;本文介绍一下安装过程中出现的问题。后面附Ubuntu的安装方法。 首次安装&#xff0c;笔者本来准备用老毛桃直接安装win7&#xff0c;可是安装到一半出现问题&#xff0c;就是win7不能在安装过程中识别U盘&#xff08;这个地方是因…

FPO优化简介

。。。转载于:https://www.cnblogs.com/rlee063/p/8890364.html

Ubuntu 使用过程用到的命令

把文件shiyan.c的所有者改为wang。$ sudo chown wang shiyan.c例2&#xff1a;把目录/his及其下的所有文件和子目录的属主改成wang&#xff0c;属组改成users。$ sudo chown - R wang.users /his

jboss配置 mysql数据库连接池实例

jboss配置 mysql数据库连接池实例1 :配置: JDK 1.5 JBoss4.0.4 Mysql5.0 Myeclipse 4.12: 建立数据库:create database test;use test;DROP TABLE IF EXISTS test;CREATE TABLE test ( Test_id int(11) NOT NULL auto_increment, Test_name varchar(4…