【问题标题】:Obtaining a Connection object from Tomcat DBCP JDBC COnnection Pooling从 Tomcat DBCP JDBC 连接池中获取 Connection 对象
【发布时间】:2014-11-01 02:12:02
【问题描述】:

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

   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.Statement,com.mysql.jdbc.ResultSet,com.mysql.jdbc.PreparedStatementmysql 发出请求?

【问题讨论】:

    标签: java mysql tomcat jdbc apache-commons-dbcp


    【解决方案1】:

    这是一个使用查询参数并使用结果的选择查询示例

    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;
        }
    
    }
    

    【讨论】:

    • 我应该使用 java.sql.* 中的类而不是 com.mysql.jdbc.* 吗?
    • 我不断收到此转换错误 java.lang.ClassCastException: com.mysql.jdbc.JDBC4PreparedStatement 无法转换为 com.mysql.jdbc.PreparedStatement,我可以使用 JDBC4PreparedStatement 而不是 java.sql.PreparedStatement或 com.mysql.jdbc.PreparedStatement
    • 抱歉耽搁了。我已经更新了答案以包括导入并修复了我的一些非常愚蠢的错误。 :)
    • 当我使用com.mysql.jdbc.Connectionjava.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to com.mysql.jdbc.Connection时出现此错误
    猜你喜欢
    • 2020-08-22
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2013-01-02
    • 2013-06-27
    • 2019-03-03
    相关资源
    最近更新 更多