首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Tomcat DBCP JDBC Connection池中获取连接对象

从Tomcat DBCP JDBC Connection池中获取连接对象
EN

Stack Overflow用户
提问于 2014-11-01 10:12:03
回答 1查看 452关注 0票数 0

我有一个从tomcat-dbcp获得的DataSource

代码语言:javascript
复制
   import java.sql.Connection

   public Connection initPooledConnection()
{
    try {
        conn=(Connection)ds.getConnection();
        if(conn==null)
        {
            System.out.println("Failed to initialize the connection");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return conn;
}

如何继续使用com.mysql.jdbc.Statementcom.mysql.jdbc.ResultSetcom.mysql.jdbc.PreparedStatementmysql发出请求

EN

回答 1

Stack Overflow用户

发布于 2014-11-01 10:32:04

以下是使用查询参数和结果的select查询示例

代码语言:javascript
复制
import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DataSource {
    public static Connection getConnection() {
        // Return a connection from the pool
    }
}

public class UserDAO {

    /**
     * Queries for User objects and returns them as a List
     */
    public List<User> getUsersForGroupID( int groupId ) {
        List<User> users = new ArrayList<User>();

        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // Check out a new connection from DataSource
            connection = DataSource.getConnection();

            // Define our query
            String query = "SELECT * FROM Users WHERE group_id = ?";

            // Create a PreparedStatement from the connection
            ps = connection.prepareStatement( query );

            // Add the parameter values (values for the ?s in the query)
            // The first one has an index of 1. They are not 0-based.
            ps.setInt( 1, groupId );

            // Execute the query and keep the returned ResultSet
            rs = ps.executeQuery();

            while (rs.next()) {
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setFullName(rs.getString("fullname"));
                users.add(user);
            }
        } catch (SQLException e) {
            // Log exception here
        } finally {
            try {
                if ( ps != null && !ps.isClosed() ) {
                    ps.close();
                }
            } catch (Exception e) {
                // Log exception thrown by ps.close()
            }
            try {
                if ( connection != null && !connection.isClosed() ) {
                    connection.close();
                }
            } catch (Exception e) {
                // Log exception thrown by connection.close();
            }
        }

        return users;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26685648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档