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

📄 programmerbmpbean.java

📁 精通Jboss——Ejb和Web Services开发精解的随书源代码
💻 JAVA
字号:
/**
 * DailyBMPBean.java Created on 2003-12-13
 *
 */
package com.liuyang.bmp.programmer;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * @author liuyang
 * 
 * @ejb.bean description="ProgrammerBMPBean"
 *           display-name="ProgrammerBMPBean"
 *           jndi-name="ejb/ProgrammerBMP"
 *           name="ProgrammerBMP"
 *           type="BMP"
 *           view-type="remote"
 * 			 primkey-field = "name"
 * 
 * @ejb.resource-ref 
 * 		res-ref-name = "datasource"
 * 		res-type = "javax.sql.DataSource"
 * 		res-auth = "Container"			
 * 
 * 
 * 
 * @jboss.resource-ref 		
 * 		jndi-name = "java:/MySQLDS"
 * 		res-ref-name = "datasource"
 * 							
 * 
 */
public class ProgrammerBMPBean implements EntityBean {
	

	private EntityContext ctx;
	private String name;
	private int age = 0;
	private String language ;
	private String tool ;	
	/**
   	 * @ejb.interface-method 
   	 * 	view-type = "remote"
     * 
     * 
     * 
     * 
     * 
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @ejb.interface-method 
	 * 	view-type = "remote"
	 * 
	 * 
	 * 
	 *  
	 * 
	 */
	public String getLanguage() {
		return language;
	}
	/**
	 * @ejb.interface-method 
	 * 	view-type = "remote"
	 * 
	 * 
	 * 
	 * 
	 * 
	 */
	public String getName() {
		return name;
	}

	/**
	 * @ejb.interface-method 
	 * 	view-type = "remote"
	 * 
	 * 
	 * 
	 * 
	 * 
	 */
	public String getTool() {
		return tool;
	}

	/**
	 * @ejb.interface-method 
	 * 	view-type = "remote"
	 */
	public void setAge(int i) {
		age = i;
	}

	/**
	 * @ejb.interface-method 
	 * view-type = "remote"
	 */
	public void setLanguage(String string) {
		language = string;
	}

	/**
	 * @ejb.interface-method 
	 * view-type = "remote"
	 */
	public void setName(String string) {
		name = string;
	}

	/**
	 * @ejb.interface-method 
	 * view-type = "remote"
	 */
	public void setTool(String string) {
		tool = string;
	}
	/**
	 * @ejb.create-method
	 */  
	public String ejbCreate(String key, int age,String language,String tool) throws CreateException  {
	  Connection con = null;
	  PreparedStatement ps = null;
	  try {
		con = datasource.getConnection();
		ps = con.prepareStatement("INSERT INTO " + TABLE_NAME + " VALUES (?,?,?,?)");
		ps.setString(1, key);
		ps.setInt(2, age);
		ps.setString(3, language);		
		ps.setString(4, tool);	
		ps.execute();
	  }catch (Exception e) {
		 throw new EJBException (e);
	  }finally {
		try {
		  if(ps != null){ps.close();}
		  if(con != null){con.close();}
		}catch (Exception ignore) {
		}
	  }
	  return key;
	}
		
	public void ejbPostCreate(String key, int age,String language,String tool) {
	}
			 
	private static final String DATASOURCE_NAME = "java:comp/env/datasource";
	private static final String TABLE_NAME = "Programmer";
	private DataSource datasource = null;	
	
	public String ejbFindByPrimaryKey(String key) throws FinderException {
	  Connection con = null;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  boolean found = false;
	  try {
		con = datasource.getConnection();
		ps = con.prepareStatement("SELECT name FROM " + TABLE_NAME + " WHERE name=?");
		ps.setString(1, key);
		rs = ps.executeQuery();
		found = rs.next();
	  }catch (Exception e) {
		throw new EJBException(e);
	  }finally {
		try {
		  if (rs != null) {rs.close ();}
		  if (ps != null) {ps.close ();}
		  if (con != null) {con.close ();}
		}catch (Exception ignore) {
		}
	  }
	  if (!found) {
		throw new ObjectNotFoundException("No bean with name=" + key + " found.");
	  }
	  return key;
	}	

	public Collection ejbFindByLanguage(String lan) throws FinderException {

	  Connection con = null;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  boolean found = false;
	  Vector result = new Vector();
	  try {
		con = datasource.getConnection();
		ps = con.prepareStatement("SELECT name FROM " + TABLE_NAME + " WHERE language=?");
		ps.setString(1, lan);
		rs = ps.executeQuery();
		while (rs.next()) {
		  result.add(rs.getString("name"));
		}
	  }
	  catch (Exception e) {
		throw new EJBException(e);
	  }
	  finally {
		try {
		  if (rs != null) {
			 rs.close ();
		  }
		  if (ps != null) {
			 ps.close ();
		  }
		  if (con != null) {
			 con.close ();
		  }
		}
		catch (Exception ignore) {
		}
	  }

	  return result;
	 }	

	public Collection ejbFindByTool(String lan) throws FinderException {

	  Connection con = null;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  boolean found = false;
	  Vector result = new Vector();
	  try {
		con = datasource.getConnection();
		ps = con.prepareStatement("SELECT name FROM " + TABLE_NAME + " WHERE tool=?");
		ps.setString(1, lan);
		rs = ps.executeQuery();
		while (rs.next()) {
		  result.add(rs.getString("name"));
		}
	  }
	  catch (Exception e) {
		throw new EJBException(e);
	  }
	  finally {
		try {
		  if (rs != null) {
			 rs.close ();
		  }
		  if (ps != null) {
			 ps.close ();
		  }
		  if (con != null) {
			 con.close ();
		  }
		}
		catch (Exception ignore) {
		}
	  }

	  return result;
	 }	
	 
	public Collection ejbFindOldThan(int _age) throws FinderException {
		Connection con = null;
		PreparedStatement ps = null;
	  	ResultSet rs = null;
	  	Vector result = new Vector();
	  	try {
			con = datasource.getConnection();
			ps = con.prepareStatement("SELECT name FROM " + TABLE_NAME + " where age>?");
			ps.setInt(1, _age);
			rs = ps.executeQuery();
			while (rs.next()) {
		  		result.add(rs.getString("name"));
			}
	  	}catch (Exception e) {
		 	throw new EJBException(e);
	  	}finally {
	  		try {
		  		if (rs != null)rs.close();
		  		if (ps != null)ps.close();
		  		if (con != null)con.close();
		  	}catch (Exception ignore) {
			}
	  	}
	  	return result;
	}

	 
	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbLoad()
	 */
	public void ejbLoad() throws EJBException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			con = datasource.getConnection();
			ps = con.prepareStatement("SELECT name,age,language,tool FROM " + TABLE_NAME + " WHERE name=?");
			ps.setString(1, (String)ctx.getPrimaryKey());
			rs = ps.executeQuery();
			if (rs.next()) {
				this.name = rs.getString("name");
			  	this.age = rs.getInt("age");
			  	this.language = rs.getString("language");
			  	this.tool = rs.getString("tool");			  
		   	}
		} catch (Exception e) {
			e.printStackTrace();
			throw new EJBException(e);
		}finally {
			try {
				if (rs != null)rs.close();
				if (ps != null)ps.close();
				if(con != null)con.close();
			} catch (Exception ignore) {
		  	}
		}
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbRemove()
	 */
	public void ejbRemove()throws RemoveException, EJBException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = datasource.getConnection();
			ps = con.prepareStatement("DELETE FROM " + TABLE_NAME + " WHERE name=?");
			ps.setString(1, name);
			ps.execute();
		}catch (Exception e) {
			throw new EJBException(e);
		}finally {
			try {
				if (ps != null)ps.close();
				if (con != null)con.close();
			}catch (Exception ignore) {
			}
		}
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbStore()
	 */
	public void ejbStore() throws EJBException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = datasource.getConnection();
			ps = con.prepareStatement("UPDATE " + TABLE_NAME + " SET age=?,language=?,tool=? WHERE name=?");
			ps.setInt(1, this.age);
			ps.setString(2, this.language);	
			ps.setString(3, this.tool);		  	  
			ps.setString(4, this.name);
			ps.execute();
		}catch (Exception e) {
		  throw new EJBException(e);
		}finally {
			try {
				if (ps != null)ps.close();
				if (con != null)con.close();
		  	}catch (Exception ignore) {
		  	}
		}
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
	 */
	public void setEntityContext(EntityContext context)
		throws EJBException, RemoteException {
			try {
			  this.ctx = context;
			  datasource = (DataSource) new InitialContext().lookup(DATASOURCE_NAME);
			}
			catch (NamingException ne) {
			  throw new EJBException(ne);
			}
	}
	public void unsetEntityContext() throws EJBException, RemoteException {
		ctx = null;
	}



}

⌨️ 快捷键说明

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