Tomcat5.5 配置数据库连接池【MySQL+Tomcat-0.2】

来源:互联网 发布:java导出doc文件 编辑:程序博客网 时间:2024/05/16 02:18

 Tomcat5.5 配置数据库连接池【MySQL+Tomcat-0.1】

 

 在Tomcat5.5 配置数据库连接池【MySQL+Tomcat-0.1】 中test中将获得链接(connection)裸露在jsp页面中,既不雅观,也不“文明”!

 

下面简单包装下!

1.DBUtils.java通过DataSources来获得连接。

  1. package com.fox.test_2;
  2. import java.sql.Connection;
  3. import javax.naming.NamingException;
  4. import javax.naming.Context;
  5. import javax.naming.InitialContext;
  6. import javax.sql.DataSource;
  7. import java.sql.SQLException;
  8. public class DButil {
  9.     public static Connection getConnection() {
  10.         Connection conn = null;
  11.         DataSource ds = null;
  12.         
  13.         try {
  14.             
  15.             Context intiContext = new InitialContext();
  16.             Context context = (Context)intiContext.lookup("java:comp/env");
  17.             Object obj = (Object) context.lookup("×××");//和前面的要统一
  18.             ds = (javax.sql.DataSource) obj;
  19.             
  20.         } catch (NamingException e) {
  21.             e.printStackTrace();
  22.         }
  23.         if (ds == null) {
  24.             System.out.println("No DataSource!!!!!!!!!!!!!!!!!!");
  25.             return null;
  26.         }
  27.         try {
  28.             conn = ds.getConnection();
  29.         } catch (SQLException e) {
  30.             e.printStackTrace();
  31.         }
  32.         return conn;
  33.     }
  34. }

2.DBConnection.java 用来定义一些常用的方法

  1. package com.fox.test_2;
  2. import java.sql.Connection;
  3. import java.sql.Date;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. public class DBConnect {
  9.     private Connection conn = null;
  10.     private Statement stmt = null;
  11.     private PreparedStatement prepstmt = null;
  12.     void init() {
  13.         
  14.         conn = DButil.getConnection();
  15.     }
  16.     /**
  17.      * 构造数据库的连接和访问类
  18.      */
  19.     public DBConnect() throws Exception {
  20.         init();
  21.         stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
  22.                 ResultSet.CONCUR_READ_ONLY);
  23.     }
  24.     /**
  25.      * PreparedStatement
  26.      * 
  27.      * @return sql 预设SQL语句
  28.      */
  29.     public void prepareStatement(String sql) throws SQLException {
  30.         prepstmt = conn.prepareStatement(sql);
  31.     }
  32.     /**
  33.      * 设置对应值
  34.      * 
  35.      * @param index
  36.      *            参数索引
  37.      * @param value
  38.      *            对应值
  39.      */
  40.     public void setString(int index, String value) throws SQLException {
  41.         prepstmt.setString(index, value);
  42.     }
  43.     public void setInt(int index, int value) throws SQLException {
  44.         prepstmt.setInt(index, value);
  45.     }
  46.     public void setBoolean(int index, boolean value) throws SQLException {
  47.         prepstmt.setBoolean(index, value);
  48.     }
  49.     public void setDate(int index, Date value) throws SQLException {
  50.         prepstmt.setDate(index, value);
  51.     }
  52.     public void setLong(int index, long value) throws SQLException {
  53.         prepstmt.setLong(index, value);
  54.     }
  55.     public void setFloat(int index, float value) throws SQLException {
  56.         prepstmt.setFloat(index, value);
  57.     }
  58.     public void setBytes(int index, byte[] value) throws SQLException {
  59.         prepstmt.setBytes(index, value);
  60.     }
  61.     /**
  62.      * 执行SQL语句返回字段集
  63.      * 
  64.      * @param sql
  65.      *            SQL语句
  66.      * @return ResultSet 字段集
  67.      */
  68.     public ResultSet executeQuery(String sql) throws SQLException {
  69.         if (stmt != null) {
  70.             return stmt.executeQuery(sql);
  71.         } else
  72.             return null;
  73.     }
  74.     public ResultSet executeQuery() throws SQLException {
  75.         if (prepstmt != null) {
  76.             return prepstmt.executeQuery();
  77.         } else
  78.             return null;
  79.     }
  80.     /**
  81.      * 执行SQL语句
  82.      * 
  83.      * @param sql
  84.      *            SQL语句
  85.      */
  86.     public void executeUpdate(String sql) throws SQLException {
  87.         if (stmt != null)
  88.             stmt.executeUpdate(sql);
  89.     }
  90.     public void executeUpdate() throws SQLException {
  91.         if (prepstmt != null)
  92.             prepstmt.executeUpdate();
  93.     }
  94.     /**
  95.      * 关闭连接
  96.      */
  97.     public void close() throws Exception {
  98.         if (stmt != null) {
  99.             stmt.close();
  100.             stmt = null;
  101.         }
  102.         if (prepstmt != null) {
  103.             prepstmt.close();
  104.             prepstmt = null;
  105.         }
  106.         if (conn != null) {
  107.             conn.close();
  108.         }
  109.     }
  110.     
  111.     }

3.测试

  1. <%@ page contentType="text/html; charset=gb2312" %>
  2. <%@ page errorPage="jsp1_error.jsp" %>
  3. <%@ page import="java.sql.*"%>
  4. <%@ page import="javax.sql.*"%>
  5. <%@ page import="javax.naming.*"%>
  6. <%@ page import="com.fox.test_2.*"%>
  7. <%@ page session="false" %>
  8. <html>
  9. <head>
  10.     <title>
  11.         test the datasource!
  12.     </title>
  13. </head>
  14. <body>
  15.     <% 
  16.         DBConnect con = null;
  17.         ResultSet rs = null;
  18.         try{
  19.             con = new DBConnect();
  20.             rs = con.executeQuery("select * from test");
  21.             while(rs.next()){
  22.                 out.println("****name is :" + rs.getString("name"));
  23.             }
  24.         }catch(Exception e){
  25.         e.printStackTrace();
  26.         }
  27.         try{
  28.             rs.close();
  29.             con.close();
  30.         }catch(Exception e){
  31.             e.printStackTrace();
  32.         }
  33.     %>
  34. </body>
  35. </html>

 

原创粉丝点击