设为首页 收藏本站
查看: 606|回复: 0

[经验分享] TOMCAT中连接池的配置

[复制链接]
发表于 2017-2-1 09:40:12 | 显示全部楼层 |阅读模式
  创建TOMCAT连接池的目的是在TOMCAT启动的时候可以产生足够多的数据库连接,并提供给程序使用
通过连接池,可以提高程序的运行速度,同时也节省内存,提高服务器的效率,能够支持更多的用户连接,连接的建立,断开
都有连接池来管理,当程序需要建立连接,只需要从内存中取一个来用,不用新建。
如何是连接池的配置,只需将其放在CONF/SERVER.XML文件中的<host></host>中
  <Context path="/hack" docBase="hack" debug="5" reloadable="true" corssContext="true">
<Resource name="jdbc/ConnectionPool"
auth="Container"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="5"
maxWait="1000"
username="sa"
password="forhack"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_net"
/>
</Context>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
  然后我们新建一WEB项hack
  然后在其WEB.XML中加入以下
  <resource-ref>
<description>SQL server text app</description>
<res-ref-name>jdbc/ConnectionPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
  下面我们来建立数据库net_db
创建表tab_news
create table tab_news (
    id                    int                   identity,
    title                 varchar(50)           null,
    content               varchar(100)          null,
    author                varchar(50)           null,
    submittime            datetime              null default getdate(),
    constraint PK_TAB_NEWS primary key  (id)
)
  分别建立3个JAVA文件
  1  connsqlserver.java
  package com.www;
  import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
  import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
  public class connsqlserver {
  /**
*    @param    args
*/
private static Connection cn = null;
  private void getConnection() {//创建数据库连接方法
   if (cn != null) {//如果连接不为空,就不新建连接
    return;  //返回,不执行以下新建连接的代码
   }
   Context ctx;//初始化CONTEXT对象
   try {
    ctx = new InitialContext();
    DataSource ds = (DataSource) ctx
      .lookup("java:comp/env/jdbc/ConnectionPool");//获取数据源
    cn = ds.getConnection();//取得数据库连接
   } catch (NamingException e) {//捕捉异常
    e.printStackTrace();
   } catch (SQLException e) {//捕捉异常
    e.printStackTrace();
   }
  return;//返回
}
  public ResultSet executeQuery(String sql) {//创建数据库查询方法
   if (cn == null)
    getConnection();
   try {//执行SQL语句
    return cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
   } catch (SQLException e) {
    e.printStackTrace();
    return null;//在异常中返回空值
   } finally {
   }
}
  public int executeUpdate(String sql) {//创建数据表更新方法
   if (cn == null)
    getConnection();
   try {
    return cn.createStatement().executeUpdate(sql);//执行SQL语句
   } catch (SQLException e) {
    e.printStackTrace();
    return -1;//在异常中返回-1
   } finally {
   }
}
  public void close() {//创建关闭数据库方法
   try {
    cn.close();//关闭数据库连接
   } catch (SQLException e) {
    e.printStackTrace();
   }finally{
    cn = null;//最后将连接置为空
   }
}
}
  2  news.java
  package com.www;
/*一个关于承载相关信息的JAVABEAN
*
*
*
*/
public class news {
String title;//新闻标题
String author;//作者
String content;//内容
String id;//ID
String submittime;//提交时间
public news(){//通过构造方法初始化赋值
   title=null;
   author=null;
   content=null;
   id=null;
   submittime=null;
}
public String getAuthor() {//get方法
   return author;
}
public void setAuthor(String author) {
   this.author = author;
}
public String getContent() {
   return content;
}
public void setContent(String content) {
   this.content = content;
}
public String getId() {
   return id;
}
public void setId(String id) {
   this.id = id;
}
public String getSubmittime() {
   return submittime;
}
public void setSubmittime(String submittime) {
   this.submittime = submittime;
}
public String getTitle() {
   return title;
}
public void setTitle(String title) {
   this.title = title;
}
  }
  3 selectsql.java
  package com.www;
  import java.sql.*;
import java.util.*;
import java.text.*;
public class selectsql {
public static ResultSet rs;
public static connsqlserver connsqlserver=new connsqlserver();


public Collection selectNewsAll(){
   Collection ret=new ArrayList();//初始化COLLECTION集合
   try{
    connsqlserver connsqlserver=new connsqlserver();//新建数据库连接
    String sql="select * from tab_news";//SQL语句
    ResultSet rs=connsqlserver.executeQuery(sql);//执行SQL语句
    while(rs.next()){//循环结果集
     news news1=new news();//初始化NEWS类
     news1.setId(rs.getString("id"));//获取ID信息
     news1.setTitle(rs.getString("title"));//获取标题信息
     news1.setContent(rs.getString("content"));//获取内容
     news1.setAuthor(rs.getString("author"));//获取作者信息
     news1.setSubmittime(rs.getString("submittime"));//获取提交时间
     ret.add(news1);//将NEWS类添加到集合中
    }
   }catch(Exception e){
    e.printStackTrace();
   }
   connsqlserver.close();
   return ret;
  
}
public Collection selectNews(){
   Collection ret=new ArrayList();
   try{
    connsqlserver connsqlserver=new connsqlserver();
    String sql="select top 6 * from tb_news";
    ResultSet rs=connsqlserver.executeQuery(sql);
    while(rs.next()){
     String title=rs.getString(2);
     String author=rs.getString(3);
     String news=rs.getString(4);
     news news1=new news();
     news1.setTitle(title);
     news1.setContent(news);
     news1.setAuthor(author);
     ret.add(news1);
    }
   }catch(Exception e){
    e.printStackTrace();
   }
   connsqlserver.close();
   return ret;
  
}
}
  建立JSP文件
  <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8" import="com.www.selectsql,java.util.*,com.www.news,java.sql.*"%>
      <jsp:useBean id="sql" class="com.www.selectsql" scope="page"/>
<table width="100%"  border="0"    height="20">
           <%Collection temp2=sql.selectNews();
     Iterator it2=temp2.iterator();
     while(it2.hasNext()){
       news news=(news)it2.next();%>
     <tr>
             <td width="4%" class="zczi">&nbsp;</td>
           <td width="96%" class="zczi"><%=news.getTitle() %></td>
     </tr>
     <%}%>
         </table>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-335999-1-1.html 上篇帖子: tomcat运行时的utf-8问题 下篇帖子: tomcat数据库连接池的研究
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表