· 载入
| public void ejbLoad() throws RemoteException { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream("client." + getId())); setFirstName((String)in.readObject()); setLastName((String)in.readObject()); setPhoneNumber((String)in.readObject()); in.close(); } catch (ClassNotFoundException e) { // Shouldn't happen -- We're dealing with Strings here. } catch (IOException e) { throw new RemoteException("An IO error occurred", e); } } |
· 删除 ejbRemove() 方法用来完成删除工作
| public void ejbRemove() throws RemoteException, RemoveException { File file = new File("client." + getId()); if (!file.exists()) throw new RemoveException(); file.delete(); } |
· XML 示例
| public void ejbStore() throws RemoteException { try { PrintWriter out = new PrintWriter(new FileWriter("client." + getId())); out.println("<?xml version=\"1.0\" ?>\n"); out.print("<client id=\""); out.print(getId()); out.print("\">\n\t<first>"); out.print(getFirstName()); out.print("</first>\n\t<last>"); out.print(getLastName()); out.print("</last>\n\t<phone>"); if (getPhoneNumber() != null) out.print(getPhoneNumber()); out.println("</phone>\n</client>"); out.close(); } catch (IOException e) { e.printStackTrace(); throw new RemoteException("An IO error occurred", e); } } |
| <?xml version="1.0" ?>
<client id="999999"> |
· Finder 方法
· 单个查找
| public ClientKey ejbFindByPrimaryKey(ClientKey key) throws RemoteException, FinderException { if (!new File("client." + key.id).exists()) throw new FinderException(); return key; } |
| public ClientKey ejbFindById(int id) throws RemoteException, FinderException { if (!new File("client." + id).exists()) throw new FinderException(); return new ClientKey(id); } |
· 多个查找
| public Enumeration ejbFindAll() throws RemoteException, FinderException { String[] files = new File(".").list(new FilenameFilter() { public boolean accept(File directory, String name) { return name.startsWith("client."); } }); Vector keys = new Vector(); |
· 多个查找 (JDBC 版本)
| public Enumeration ejbFindAll() throws RemoteException, FinderException { Vector keys = new Vector(); Connection connection = null; PreparedStatement statement = null; try { connection = getDataSource().getConnection(); statement = connection.prepareStatement("Select ID from CLIENT"); ResultSet results = statement.executeQuery(); while (results.next()) { int id = results.getInt(1); keys.addElement(new ClientKey(id)); } results.close(); } catch (SQLException e) { } finally { try {statement.close();} catch (SQLException e) { } try {connection.close();} catch (SQLException e) {} } return keys.elements(); } |
· JDBC 载入示例
| public void ejbLoad() throws RemoteException { Connection connection = null; PreparedStatement statement = null; try { connection = getDataSource().getConnection(); statement = connection .prepareStatement("Select FIRST, LAST, PHONE from CLIENT where ID=?"); statement.setInt(1, getId()); ResultSet results = statement.executeQuery(); if (results.next()) { setFirstName(results.getString(1)); setLastName(results.getString(2)); setPhoneNumber(results.getString(3)); } else { throw new RemoteException("The row cannot be found!"); } results.close(); } catch (SQLException e) { ... } |
· 查找的过程
· 本章讲述内容