数据库的连接池

Mysql的数据库的连接:

可以分为全局的连接和私有的连接;

全局的连接:

首先在eclipse里面找到context.xml这个文件,在里面配置


<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
  <Resource name="jdbc/T204" 
            auth="Container"
            type="javax.sql.DataSource"
            maxTotal="100" 
            maxIdle="30"
            maxWaitMillis="10000"
            username="root" password="sasa" driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/t204"/>
    
</Context>

这一段是在APache Tomcat官网 里面找到的

<Resource name="jdbc/T204" 
            auth="Container"
            type="javax.sql.DataSource"
            maxTotal="100" 
            maxIdle="30"
            maxWaitMillis="10000"
            username="root" password="sasa" driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/t204"/>

点击       

然后在界面输出Mysqld的连接地址,打印出来

<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.naming.InitialContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% 
  Context initContext = new InitialContext();
  Context envContext  = (Context)initContext.lookup("java:/comp/env");
  DataSource ds = (DataSource)envContext.lookup("jdbc/T204");
  Connection conn = ds.getConnection();
  out.print(conn);
%>
</body>
</html>
DataSource ds = (DataSource)envContext.lookup("jdbc/T204"); (Mysql里面的数据库名)

私有的数据库连接

在创建的项目中找到META-INF里面创建一个context.xml,内容跟全局连接context.xml里面一样,在web.xml里面配置,最后在界面输出就可以输出

<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>


 


以上就是Mysql的连接,其他的数据库的连接也一样,有什么不足的地方可以指出来!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值