21天精通Java,这是一本英文书
源代码在线查看: locationbean.java
package data;
import java.rmi.*;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;
public class LocationBean implements EntityBean
{
private DataSource dataSource;
private String name;
private String description;
public String getName () {
return name;
}
public String getDescription () {
return description;
}
public void setDescription (String description) {
this.description = description;
}
// EJB methods start here
public void ejbPostCreate (String name, String description) {}
public String ejbCreate (String name, String description) throws CreateException {
try {
ejbFindByPrimaryKey(name);
throw new CreateException("Duplicate location name: "+name);
}
catch (FinderException ex) {}
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"INSERT INTO Location (name,description) VALUES (?,?)");
stmt.setString(1, name);
stmt.setString(2, description);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error creating Location "+name,e);
}
finally {
closeConnection(con, stmt, null);
}
this.name = name;
this.description = description;
return name;
}
public String ejbFindByPrimaryKey(String name) throws FinderException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT name FROM Location WHERE name = ?");
stmt.setString(1, name);
rs = stmt.executeQuery();
if (!rs.next()) {
throw new FinderException("Unknown location: "+name);
}
return name;
}
catch (SQLException e) {
error("Error in findByPrimaryKey for "+name,e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public Collection ejbFindAll() throws FinderException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT name FROM Location ORDER BY name");
rs = stmt.executeQuery();
Collection col = new ArrayList();
while (rs.next()) {
col.add(rs.getString(1));
}
return col;
}
catch (SQLException e) {
error("Error in findAll",e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public void ejbLoad(){
name = (String)ctx.getPrimaryKey();
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT name,description FROM Location WHERE name = ?");
stmt.setString(1, name);
rs = stmt.executeQuery();
if (!rs.next()) {
error("No data found in ejbLoad for "+name,null);
}
this.name = rs.getString(1);
this.description = rs.getString(2);
}
catch (SQLException e) {
error("Error in ejbLoad for "+name,e);
}
finally {
closeConnection(con, stmt, rs);
}
}
public void ejbStore(){
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"UPDATE Location SET description = ? WHERE name = ?");
stmt.setString(1, description);
stmt.setString(2, name);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error in ejbStore for "+name,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void ejbPassivate(){
name = null;
description = null;
}
public void ejbActivate(){
}
public void ejbRemove(){
name = (String)ctx.getPrimaryKey();
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM Location WHERE name = ?");
stmt.setString(1, name);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error removing location "+name,e);
}
finally {
closeConnection(con, stmt, null);
}
name = null;
description = null;
}
private EntityContext ctx;
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
InitialContext ic = null;
try {
ic = new InitialContext();
dataSource = (DataSource)ic.lookup("java:comp/env/jdbc/Agency");
}
catch (NamingException ex) {
error("Error connecting to java:comp/env/jdbc/Agency:",ex);
return;
}
}
public void unsetEntityContext() {
this.ctx = null;
dataSource = null;
}
private void closeConnection (Connection con, PreparedStatement stmt, ResultSet rslt) {
if (rslt != null) {
try {
rslt.close();
}
catch (SQLException e) {}
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException e) {}
}
if (con != null) {
try {
con.close();
}
catch (SQLException e) {}
}
}
private void error (String msg, Exception ex) {
String s = "LocationBean: "+msg + "\n" + ex;
System.out.println(s);
throw new EJBException(s,ex);
}
}
|
相关资源 |
|
-
21天精通Java,这是一本英文书
-
21天学会用JAVA开发网络游戏,这是一本英文书
-
<<21天精通JAVA>>一书的源码,边学边做,很快上手,适合JAVA初学者.
-
Linux设备驱动设计开发.是一本英文书.讲的挺不错。比国内的书强多了,强烈建议下载此书.配合另外本英文(linux kenel)书,书看完的时候,将是你成专家的时候.
-
这是一本不错的JAVA入门书藉.只要你恳踏踏实实的去学,一定会有你出人头地的一天.
-
这是一本叫精通java中间件的一本书的源码,大家一起学习学习
-
这是一本很权威的java编程英文书
-
这是一本有关java的教科书
|