⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basequerycommand.java

📁 100多M的J2EE培训内容
💻 JAVA
字号:
package examples.datacommands;

import javax.sql.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.*;
import sun.jdbc.rowset.CachedRowSet;

/**
 * The Super class for any data command beans Read from the
 * database.
 */
abstract class BaseReadCommand {

	protected PreparedStatement pstmt;
	protected CachedRowSet rowSet = null;
	private Connection con;

	protected BaseReadCommand ( String jndiName, String statement ) throws DataCommandException
	{
        InitialContext ctx = null;
        try
        {
            ctx = new InitialContext();
            DataSource ds = (javax.sql.DataSource) ctx.lookup(jndiName);
            con = ds.getConnection();
            pstmt = con.prepareStatement(statement);
        }
        catch (NamingException e)
        {
            throw new DataCommandException(e.getMessage());
        }
        catch (SQLException e)
        {
            throw new DataCommandException(e.getMessage());
        }
	}

	public void execute() throws DataCommandException
	{
        try
        {
            rowSet = new CachedRowSet();
            rowSet.populate(pstmt.executeQuery());
            rowSet.beforeFirst();
            this.release();
        } catch (SQLException e)
        {
            throw new DataCommandException(e.getMessage());
        }
    }
	public boolean next() throws DataCommandException
	{
        try
        {
            return rowSet.next();
        } catch (SQLException e)
        {
            throw new DataCommandException(e.getMessage());
        }
	}
	private void release() throws SQLException
	{
        if (pstmt != null) pstmt.close();
        if (con != null) con.close();
 	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -