📄 idgeneratorbean.java
字号:
package webauction.ejb;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class IDGeneratorBean implements SessionBean {
private static final boolean SUPPORTS_SEQUENCES = false;
private SessionContext ctx;
private DataSource dataSource;
private String jdbcStatement = null;
private String sequenceName;
public void setSessionContext(SessionContext c) {
ctx = c;
Context ic;
try {
ic = new InitialContext();
String poolName = (String) ic.lookup(EjbConstants.LOOKUP_POOL_NAME);
dataSource =
(DataSource) ic.lookup("java:/comp/env/" + poolName);
sequenceName = (String) ic.lookup(EjbConstants.LOOKUP_SEQ_NAME);
} catch (NamingException ne) {
ne.printStackTrace();
throw new EJBException("Unable to get datasource: " + ne);
}
}
private void setJdbcStatement() {
// Several databases do not support sequences. We simulate them with
// an update followed by a select.
// Oracle supports sequences but uses a slightly different set of
// SQL. We check for oracle and change the JDBC statement.
try {
DatabaseMetaData md = dataSource.getConnection().getMetaData();
String driverName = md.getDriverName();
if (SUPPORTS_SEQUENCES) {
if (driverName
.startsWith("Weblogic, Inc. Java-OCI JDBC Driver")
|| driverName.startsWith("Oracle JDBC driver")) {
// Oracle syntax
jdbcStatement =
"select " + sequenceName + ".nextval FROM dual";
} else {
// SQL syntax
jdbcStatement = "select nextval('" + sequenceName + "')";
}
}
} catch (SQLException sqe) {
throw new EJBException(sqe);
}
}
private int getNextValueFromSequence(Connection conn) throws SQLException {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(jdbcStatement);
if (rs.next()) {
return rs.getInt(1);
} else {
throw new EJBException(
"Query '" + jdbcStatement + "' did not return any results");
}
}
private int getNextValueFromTable(Connection conn) throws SQLException {
Statement st = conn.createStatement();
st.executeUpdate("update " + sequenceName + " set seqNo = seqNo + 1");
st = conn.createStatement();
ResultSet rs = st.executeQuery("select seqNo from " + sequenceName);
if (rs.next()) {
return rs.getInt(1);
} else {
throw new EJBException(
"Query '" + jdbcStatement + "' did not return any results");
}
}
public int getNextValue() {
try {
if(null == jdbcStatement) {
setJdbcStatement();
}
Connection conn = dataSource.getConnection();
if (SUPPORTS_SEQUENCES) {
return getNextValueFromSequence(conn);
} else {
return getNextValueFromTable(conn);
}
} catch (SQLException sqe) {
throw new EJBException(sqe);
}
}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbPassivate() {}
public void ejbActivate() {}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -