📄 agencybean.java
字号:
package agency;
import java.rmi.*;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.* ;
import javax.sql.*;
public class AgencyBean implements SessionBean
{
private DataSource dataSource;
private String name = "";
public String getAgencyName() {
return name;
}
public Collection getApplicants() {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT login FROM Applicant");
rs = stmt.executeQuery();
Collection col = new TreeSet();
while (rs.next()) {
col.add(rs.getString(1));
}
return col;
}
catch (SQLException e) {
error("Error getting Applicant list",e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public void createApplicant(String login, String name, String email) throws DuplicateException, CreateException{
Collection apps = getApplicants();
Iterator it = apps.iterator();
while (it.hasNext()) {
String s = (String)it.next();
if (s.equalsIgnoreCase(login))
throw (new DuplicateException(s+" applicant already defined"));
}
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"INSERT INTO Applicant (login,name,email) VALUES (?, ?, ?)");
stmt.setString(1, login);
stmt.setString(2, name);
stmt.setString(3, email);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error creating Applicant "+login,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void deleteApplicant (String login) throws NotFoundException{
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM ApplicantSkill WHERE applicant = ?");
stmt.setString(1, login);
stmt.executeUpdate();
stmt = con.prepareStatement(
"DELETE FROM Applicant WHERE login = ?");
stmt.setString(1, login);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error deleting Applicant "+login,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public Collection getCustomers() {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT login FROM Customer");
rs = stmt.executeQuery();
Collection col = new TreeSet();
while (rs.next()) {
col.add(rs.getString(1));
}
return col;
}
catch (SQLException e) {
error("Error getting Customer list",e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public void createCustomer(String login, String name, String email) throws DuplicateException, CreateException{
Collection cust = getCustomers();
Iterator it = cust.iterator();
while (it.hasNext()) {
String s = (String)it.next();
if (s.equalsIgnoreCase(login))
throw (new DuplicateException(s+" customer already defined"));
}
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"INSERT INTO Customer (login,name,email) VALUES (?, ?, ?)");
stmt.setString(1, login);
stmt.setString(2, name);
stmt.setString(3, email);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error creating Customer "+login,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void deleteCustomer (String login) throws NotFoundException {
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM JobSkill WHERE customer = ?");
stmt.setString(1, login);
stmt.executeUpdate();
stmt = con.prepareStatement(
"DELETE FROM Job WHERE customer = ?");
stmt.setString(1, login);
stmt.executeUpdate();
stmt = con.prepareStatement(
"DELETE FROM Customer WHERE login = ?");
stmt.setString(1, login);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error deleting Customer "+login,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public Collection getLocations() {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT name FROM Location");
rs = stmt.executeQuery();
Collection col = new TreeSet();
while (rs.next()) {
col.add(rs.getString(1));
}
return col;
}
catch (SQLException e) {
error("Error getting Location list",e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public String getLocationDescription(String name) throws NotFoundException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT description FROM Location WHERE name = ?");
stmt.setString(1, name);
rs = stmt.executeQuery();
if (!rs.next()) {
throw new NotFoundException("Unknown location: "+name);
}
return rs.getString(1);
}
catch (SQLException e) {
error("Error getting Location description for "+name,e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public void updateLocation(String name, String description) throws NotFoundException {
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 updating Location: "+name,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void addLocation(String name, String description) throws DuplicateException {
Collection locations = getLocations();
Iterator it = locations.iterator();
while (it.hasNext()) {
String s = (String)it.next();
if (s.equalsIgnoreCase(name))
throw (new DuplicateException(s+" location already defined"));
}
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"INSERT INTO Location (name,description) VALUES (?,?)");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -