日期:2014-05-16  浏览次数:20893 次

基于WASCE+EJB2.1+Mysql数据源简单Demo


上篇文章我写了一个关于WASCE+EJB2.1的简单Demo,只是为了搭建如何使用EJB2.1的环境,但是,实际开发中一定会用到数据库,那么今天,我继续介绍一下如何在企业级项目中利用WASCE配置一个数据源,继而如果在SessionBean中调用的例子。首先,我们简单设计一个数据库wasce,创建一个users表,字段包括id,name,sex,age。具体创建语句如下:

--
-- Create schema wasce
--
CREATE DATABASE IF NOT EXISTS wasce;
USE wasce;
--
-- Definition of table `users`
--

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(45) NOT NULL,
  `sex` varchar(45) NOT NULL,
  `age` varchar(45) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `users`
--

/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` (`id`,`name`,`sex`,`age`) VALUES 
 (1,'zf','male','29'),
 (2,'zz','female','30');
?

然后启动WASCE服务,访问http://localhost:8888/console/portal/Welcome,(8888端口是我自己修改的,默认是8080)。默认用户名system密码manager。登陆管理界面,按照我在《关于eclipse+wasce+mysql的环境搭建》中的方式,搭建一个数据源 wasce。准备完成后,进行程序开发,我们需要在HelloEJB中添加三个类,一个数据库工具类DBUtils,提供对数据源的查找和创建数据库链接;一个数据库操作类DBAdaptor,对具体表进行CURD操做;一个数据实体类Users,存放对应表数据。具体代码如下:

DBUtils类

/**
 *  数据库工具类
 * @author Frank 
 * @version 1.0
 *
 */
public class DBUtils {
	
	private static DataSource ds = null;
	private static Context initCtx = null;
	private static final Logger logger = Logger.getLogger("DBUtils");
	protected Connection conn;
	protected ResultSet resultSet;
	protected PreparedStatement preparedStatement;
	
	static {
		new DBUtils();
	}
	public DBUtils() {
		try	{
			
			 Hashtable<String, String> environment = new Hashtable<String,String>();
			 //	retrieve the initial context in order to look up the datasource
			initCtx = new InitialContext(environment);
			ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/wasce");
			
		} 
		catch (NamingException nx) {
			logger.severe(nx.toString());
		}
		catch (Exception e) {
			logger.severe("Exception occured"+e);
		}
	}
	
	public static Connection getConnectionNoIsolation() throws SQLException {
		return ds.getConnection();
	}
	
	public static Connection getDefaultConnection() throws SQLException {
		Connection conn = ds.getConnection();
		return conn;
	}
	
	public static Connection getConnection() throws SQLException {
		Connection conn = ds.getConnection();
		logger.info("***open conn: " + conn);
		conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return conn;
	}
	
	public static Connection getConnection(int isolationLevel) throws SQLException {
		Connection conn = ds.getConnection();
		if(isolationLevel < 0)
			conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		else
			conn.setTransactionIsolation(isolationLevel);
		return conn;
	}
	
	public static void closeConnection(Connection connection) throws SQLException {
		closeConnection(null, connection, null);
	}
	
	public static void closeResultSet(ResultSet resultSet) throws SQLException {
		closeConnection(null, null, resultSet);
	}

	public static void closePreparedStatement(
			PreparedStatement preparedStatement) throws SQLException {
		closeConnection(preparedStatement, null, null);
	}

	public static void closeConnection(PreparedStatement preparedStatement,
			Connection conn, ResultSet resultSet) throws SQLException {
		logger.info("***close conn: " + conn);
		
		if (resultSet != null) {
			resultSet.close();
		}
		if (preparedStatement != null) {
			preparedStatement.close();
		}
		if (conn != null) {
			conn.close();
		}
	}
	
	public static void closeConnection(Statement stmt,
			Connection conn, ResultSet resultSet) throws SQLException {
		logger.info("***close conn: " + conn);
		
		if (resultSet != null) {
			resultSet.close();
		}
		if (stmt != null) {
			stmt.close();
		}
		if (conn != null) {
			conn.close();
		}
	}
	
	public