`
xiezhuogang
  • 浏览: 28842 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

TOMCAT中连接池的配置

阅读更多

创建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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics