免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: 我是好人
打印 上一主题 下一主题

关于Tomcat的MySQL连接池的问题,求救! [复制链接]

论坛徽章:
0
11 [报告]
发表于 2003-12-29 16:13 |只看该作者

关于Tomcat的MySQL连接池的问题,求救!

你这个bean是被什么调用的?如果是jsp的话,那在jsp中等你处理完rs后,调用这个bean的close方法不就可以了么

论坛徽章:
0
12 [报告]
发表于 2003-12-29 19:20 |只看该作者

关于Tomcat的MySQL连接池的问题,求救!

你对数据库操作不是要catch 诸如 NamingException SQLException等等的Exceptions吗? 如果我说得没错, 那就在你的catch候加上一个finally块, 在finnally块里关闭连接, 这是你的业务逻辑肯定已经结束了, 如果这个bean里的另外一个方法还需要连接, 那让它再去获取连接, 这是标准操作, 获取的连接池里的空闲连接。

论坛徽章:
0
13 [报告]
发表于 2003-12-29 19:22 |只看该作者

关于Tomcat的MySQL连接池的问题,求救!

不是再jsp里关闭, 再bean里就要关闭。

论坛徽章:
0
14 [报告]
发表于 2003-12-30 08:46 |只看该作者

关于Tomcat的MySQL连接池的问题,求救!

楼上的,他的bean里那个方法是返回rs的,如果来个finally,那rs就没用了。

论坛徽章:
0
15 [报告]
发表于 2003-12-30 11:24 |只看该作者

关于Tomcat的MySQL连接池的问题,求救!

返回rs的做法不对, 那样又把很多java代码弄到页面里来了,最好放回一个数组, 或者另外一个bean

论坛徽章:
0
16 [报告]
发表于 2004-01-04 07:57 |只看该作者

关于Tomcat的MySQL连接池的问题,求救!

to elgs:请具体讲讲好吗?

论坛徽章:
0
17 [报告]
发表于 2004-01-05 00:26 |只看该作者

关于Tomcat的MySQL连接池的问题,求救!


  1. package something;

  2. import java.sql.*;
  3. import javax.naming.NamingException;


  4. public class RsBean
  5. {
  6.         private ConnMaker connMaker;
  7.         private Connection connection;
  8.         private Statement statement;
  9.         private ResultSet rs;

  10.         private int rows;
  11.         private int cols;
  12.         private String[][] data;

  13.         public int getRows()
  14.         {
  15.                 return this.rows;
  16.         }
  17.         public void setRows(int rows)
  18.         {
  19.                 this.rows = rows;
  20.         }

  21.         public int getCols()
  22.         {
  23.                 return this.cols;
  24.         }
  25.         public void setCols(int cols)
  26.         {
  27.                 this.cols = cols;
  28.         }

  29.         public RsBean()
  30.         {
  31.                 connMaker = new ConnMaker();
  32.         }

  33.         public double getSum(int col) throws NumberFormatException
  34.         {
  35.                 if(data != null)
  36.                 {
  37.                         double sum = 0D;
  38.                         for(int i = 0; i < getRows(); ++i)
  39.                                 sum += Double.parseDouble(data[col][i]);
  40.                         return sum;
  41.                 }
  42.                 else
  43.                         return 0D;
  44.         }
  45.       public void setData(String sql) throws NamingException, SQLException
  46.       {
  47.             try
  48.             {
  49.                   connection = connMaker.getConnection();
  50.                   statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
  51.                   rs = statement.executeQuery(sql);
  52.                   rs.last();
  53.                   setRows(rs.getRow());
  54.                   rs.beforeFirst();
  55.                   ResultSetMetaData rsMetaData = rs.getMetaData();
  56.                   setCols(rsMetaData.getColumnCount());
  57.                   data = new String[cols][rows];
  58.                   while(rs.next())
  59.                   {
  60.                         int row = rs.getRow() - 1;
  61.                         for(int col = 0; col < cols; ++col)
  62.                               data[col][row] = rs.getString(col + 1);
  63.                   }
  64.             }
  65.             finally
  66.             {
  67.                   if(rs != null)
  68.                   {
  69.                         rs.close();
  70.                   }
  71.                   if(statement != null)
  72.                   {
  73.                         statement.close();
  74.                   }
  75.                   if(connection != null)
  76.                   {
  77.                         connection.close();
  78.                   }
  79.             }

  80.       }
  81.       public String[][] getData()
  82.       {
  83.             return data;
  84.       }
  85. }

复制代码

论坛徽章:
0
18 [报告]
发表于 2004-01-05 00:27 |只看该作者

关于Tomcat的MySQL连接池的问题,求救!


  1. package something;

  2. import java.sql.*;
  3. import javax.sql.*;
  4. import javax.naming.*;

  5. public class ConnMaker
  6. {
  7.         Context context = null;
  8.         public ConnMaker()
  9.         {
  10.         }
  11.         public Connection getConnection() throws NamingException,SQLException
  12.         {       
  13.                 try
  14.                 {
  15.                         context = new InitialContext();
  16.                         DataSource dataSource = (DataSource)context.lookup("java:comp/env/jdbc/hd31DB");
  17.                         return dataSource.getConnection();
  18.                 }
  19.                 finally
  20.                 {
  21.                         if(context == null)
  22.                                 context.close();
  23.                 }
  24.         }
  25. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP