📄 agencybean.java
字号:
stmt.setString(1, name);
stmt.setString(2, description);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error creating Location "+name,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void removeLocation(String code) throws NotFoundException {
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"UPDATE Job SET location = '' WHERE location = ?");
stmt.setString(1, code);
stmt.executeUpdate();
stmt = con.prepareStatement(
"UPDATE Applicant SET location = '' WHERE location = ?");
stmt.setString(1, code);
stmt.executeUpdate();
stmt = con.prepareStatement(
"DELETE FROM Location WHERE name = ?");
stmt.setString(1, code);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error removing Location "+code,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public Collection getSkills() {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT name FROM Skill");
rs = stmt.executeQuery();
Collection col = new TreeSet();
while (rs.next()) {
col.add(rs.getString(1));
}
return col;
}
catch (SQLException e) {
error("Error getting Skill list: ",e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public String getSkillDescription(String name) throws NotFoundException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT description FROM Skill WHERE name = ?");
stmt.setString(1, name);
rs = stmt.executeQuery();
if (!rs.next()) {
throw new NotFoundException("Unknown skill: "+name);
}
return rs.getString(1);
}
catch (SQLException e) {
error("Error getting skill description for "+name,e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public void updateSkill(String name, String description) throws NotFoundException {
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"UPDATE Skill SET description = ? WHERE name = ?");
stmt.setString(1, description);
stmt.setString(2, name);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error updating Skill: "+name,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void addSkill (String name, String description) throws DuplicateException {
Collection skills = getSkills();
Iterator it = skills.iterator();
while (it.hasNext()) {
String s = (String)it.next();
if (s.equalsIgnoreCase(name))
throw (new DuplicateException(s+" skill already defined"));
}
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"INSERT INTO Skill (name,description) VALUES (?,?)");
stmt.setString(1, name);
stmt.setString(2, description);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error creating skill "+name,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void removeSkill (String name) throws NotFoundException {
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM JobSkill WHERE skill = ?");
stmt.setString(1, name);
stmt.executeUpdate();
stmt = con.prepareStatement(
"DELETE FROM ApplicantSkill WHERE skill = ?");
stmt.setString(1, name);
stmt.executeUpdate();
stmt = con.prepareStatement(
"DELETE FROM Skill WHERE name = ?");
stmt.setString(1, name);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error deleting skill "+name,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public List select(String table) {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT * FROM "+table);
rs = stmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
List ans = new ArrayList();
String[] hdr = new String[numCols];
// get column header info
for (int i=1; i <= numCols; i++)
hdr[i-1] = rsmd.getColumnLabel(i);
ans.add(hdr);
while (rs.next()) {
String[] row = new String[numCols];
for (int i=1; i <= numCols; i++)
row[i-1] = rs.getString(i);
ans.add(row);
}
return ans;
}
catch (SQLException e) {
error("Error getting table rows",e);
}
finally {
closeConnection(con, stmt, rs);
}
return 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) {}
}
}
// EJB methods start here
private void error (String msg, Exception ex) {
String s = "AgencyBean: "+msg + "\n" + ex;
System.out.println(s);
throw new EJBException(s,ex);
}
public void ejbCreate () throws CreateException {
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;
}
try {
name = (String)ic.lookup("java:comp/env/AgencyName");
}
catch (NamingException ex) {
error("Error looking up java:comp/env/AgencyName:",ex);
}
}
public void ejbActivate(){
}
public void ejbPassivate(){
}
public void ejbRemove(){
dataSource = null;
}
private SessionContext ctx;
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -