Tomcat5.5 配置数据库连接池【MySQL+Tomcat-0.1】续
在Tomcat5.5 配置数据库连接池【MySQL+Tomcat-0.1】 中test中将获得链接(connection)裸露在jsp页面中,既不雅观,也不“文明”!
下面简单包装下!
1.DBUtils.java通过DataSources来获得连接。
- package com.fox.test_2;
- import java.sql.Connection;
- import javax.naming.NamingException;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.sql.DataSource;
- import java.sql.SQLException;
- public class DButil {
- public static Connection getConnection() {
- Connection conn = null;
- DataSource ds = null;
- try {
- Context intiContext = new InitialContext();
- Context context = (Context)intiContext.lookup("java:comp/env");
- Object obj = (Object) context.lookup("×××");//和前面的要统一
- ds = (javax.sql.DataSource) obj;
- } catch (NamingException e) {
- e.printStackTrace();
- }
- if (ds == null) {
- System.out.println("No DataSource!!!!!!!!!!!!!!!!!!");
- return null;
- }
- try {
- conn = ds.getConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- }
2.DBConnection.java 用来定义一些常用的方法
- package com.fox.test_2;
- import java.sql.Connection;
- import java.sql.Date;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class DBConnect {
- private Connection conn = null;
- private Statement stmt = null;
- private PreparedStatement prepstmt = null;
- void init() {
- conn = DButil.getConnection();
- }
- /**
- * 构造数据库的连接和访问类
- */
- public DBConnect() throws Exception {
- init();
- stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- }
- /**
- * PreparedStatement
- *
- * @return sql 预设SQL语句
- */
- public void prepareStatement(String sql) throws SQLException {
- prepstmt = conn.prepareStatement(sql);
- }
- /**
- * 设置对应值
- *
- * @param index
- * 参数索引
- * @param value
- * 对应值
- */
- public void setString(int index, String value) throws SQLException {
- prepstmt.setString(index, value);
- }
- public void setInt(int index, int value) throws SQLException {
- prepstmt.setInt(index, value);
- }
- public void setBoolean(int index, boolean value) throws SQLException {
- prepstmt.setBoolean(index, value);
- }
- public void setDate(int index, Date value) throws SQLException {
- prepstmt.setDate(index, value);
- }
- public void setLong(int index, long value) throws SQLException {
- prepstmt.setLong(index, value);
- }
- public void setFloat(int index, float value) throws SQLException {
- prepstmt.setFloat(index, value);
- }
- public void setBytes(int index, byte[] value) throws SQLException {
- prepstmt.setBytes(index, value);
- }
- /**
- * 执行SQL语句返回字段集
- *
- * @param sql
- * SQL语句
- * @return ResultSet 字段集
- */
- public ResultSet executeQuery(String sql) throws SQLException {
- if (stmt != null) {
- return stmt.executeQuery(sql);
- } else
- return null;
- }
- public ResultSet executeQuery() throws SQLException {
- if (prepstmt != null) {
- return prepstmt.executeQuery();
- } else
- return null;
- }
- /**
- * 执行SQL语句
- *
- * @param sql
- * SQL语句
- */
- public void executeUpdate(String sql) throws SQLException {
- if (stmt != null)
- stmt.executeUpdate(sql);
- }
- public void executeUpdate() throws SQLException {
- if (prepstmt != null)
- prepstmt.executeUpdate();
- }
- /**
- * 关闭连接
- */
- public void close() throws Exception {
- if (stmt != null) {
- stmt.close();
- stmt = null;
- }
- if (prepstmt != null) {
- prepstmt.close();
- prepstmt = null;
- }
- if (conn != null) {
- conn.close();
- }
- }
- }
3.测试
- <%@ page contentType="text/html; charset=gb2312" %>
- <%@ page errorPage="jsp1_error.jsp" %>
- <%@ page import="java.sql.*"%>
- <%@ page import="javax.sql.*"%>
- <%@ page import="javax.naming.*"%>
- <%@ page import="com.fox.test_2.*"%>
- <%@ page session="false" %>
- <html>
- <head>
- <title>
- test the datasource!
- </title>
- </head>
- <body>
- <%
- DBConnect con = null;
- ResultSet rs = null;
- try{
- con = new DBConnect();
- rs = con.executeQuery("select * from test");
- while(rs.next()){
- out.println("****name is :" + rs.getString("name"));
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- try{
- rs.close();
- con.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- %>
- </body>
- </html>