bean class:
package enterprise.ejb;import enterprise.common.*;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.sql.*;
import javax.naming.*;public class SchoolEJB implements EntityBean
{
 public String ProID;
 public String ProName; public Connection con;
 public String DBJndi="java:comp/env/jdbc/SchoolDB"; private EntityContext context; //取得数据库链接
 public void setEntityContext(EntityContext context)
 {
  this.context=context;
  try{
   connectToDB();
  }catch(Exception ex)
  {
    throw new EJBException(ex.getMessage());
  }
 }
 //关闭数据库链接
 public void unsetEntityContext()
 {
  context=null;
  try{
   con.close();
  }catch(SQLException ex)
  {
   throw new EJBException(ex.getMessage());
  }
 }
 //添加一条记录到数据库中
 public String ejbCreate(String ProID,String ProName)
    throws DuplicateKeyException,CreateException
 {
   try{
    insertSchoolRow(ProID,ProName);
   }catch(Exception ex)
   {
    throw new EJBException(ex.getMessage());
   }
  this.ProID=ProID;
  this.ProName=ProName;  return ProID;
 }
 //对应上面的ejbcreate()
 public void ejbPostCreate(String ProID,String ProName)
 {
 }
 //根据SchID查询
 public String ejbFindByPrimaryKey(String ProID)
     throws ObjectNotFoundException,FinderException
 {
  boolean result;
  try{
   result=selectByPrimaryKey(ProID);
  }catch(Exception ex)
  {
   throw new EJBException("ejbFindByPrimaryKey exception:"+ex.getMessage());
  }
  if(result)
  {
   return ProID;
  }
  else
  {
   throw new ObjectNotFoundException(ProID+"not found.");
  }
 }
 //取得所有表单
 public Collection ejbFindAll()
     throws FinderException
 {
  ArrayList result;
  try{
   result=selectAll();
  }catch(Exception ex)
  {
   throw new EJBException(ex.getMessage());
  }  if(result.isEmpty())
  {
  throw new ObjectNotFoundException("No rows found.");
  }
  else
  {
   return result;
  }
 }  public void ejbLoad()
 {
  try{
   selectRow();
  }catch(Exception ex)
  {
   throw new EJBException(ex.getMessage());
  }
 }
 public void ejbStore()
 {
  try
  {
   updateRow();
  }catch(Exception ex)
  {
   throw new EJBException(ex.getMessage());
  }
 } //
 public String getProID()
 {
  return ProID;
 }
 public String getProName()
 {
  return ProName;
 } public void ejbActivate()
 {
  ProID=(String)context.getPrimaryKey();
 }
 public void ejbPassivate()
 {
  ProID=null;
  ProName=null;
 }
 public void ejbRemove()
 {
   try{
    deleteRow();
   }catch(Exception ex)
   {
    throw new EJBException(ex.getMessage());
   }
 } //对应romote interface中的方法 public ProvinceItem getProData()
 {
  return new ProvinceItem(this.ProID,this.ProName);
 } public void setProData(String ProID,String ProName)
 {
  this.ProID=ProID;
  this.ProName=ProName;
 }
 /*
 -------------------------------
 下面是一些执行数据库操作的私有方法
 -------------------------------
 */
 //取得数据库链接
 private void connectToDB()
     throws NamingException,SQLException
 {
  InitialContext ictx=new InitialContext();
  DataSource ds=(DataSource)ictx.lookup(DBJndi);
  con=ds.getConnection();
 }
 //添加一条记录到数据库中
 private void insertSchoolRow(String ProID,String ProName)
     throws SQLException
 {
  String insertStatement="insert into Province values(?,?)";
  PreparedStatement ps=con.prepareStatement(insertStatement);     //发送SQL statement到数据库中  ps.setString(1,ProID);                                         //设置字段值
  ps.setString(2,ProName);  ps.executeUpdate();
  ps.close();
 }
 //根据SCHID从库中取得数据
 private boolean selectByPrimaryKey(String primaryKey)
     throws SQLException
 {
  String selectStatement="select ProID from Province where ProID=?";
  PreparedStatement ps=con.prepareStatement(selectStatement);
  try{
   ps.setString(1,primaryKey);
   ResultSet rs=ps.executeQuery();
   return rs.next();
  }finally{
   ps.close();
  }
 }
 //取得数据表单中的所有数据
 private ArrayList selectAll()
     throws SQLException
 {
  String sqlStatement="select ProID,ProName from Province";
  PreparedStatement ps=con.prepareStatement(sqlStatement);
  try
  {
   ResultSet rs=ps.executeQuery();
   ArrayList al=new ArrayList();
   while(rs.next())
   {
    String id=rs.getString(1);
    al.add(id);
   }
   return al;
  }finally
  {
   ps.close();
  }
 }
 //删除表记录
 private void deleteRow()
     throws SQLException
 {
  String sqlStatement="delete from Province where ProID=?";
  PreparedStatement ps=con.prepareStatement(sqlStatement);
  ps.setString(1,ProID);
  ps.executeUpdate();
  ps.close();
 }
 //载入目前数据表中的记录
 private void selectRow()
     throws SQLException
 {
  String sqlStatement="select ProID,ProName from Province where ProID=?";
  PreparedStatement ps=con.prepareStatement(sqlStatement);
  ps.setString(1,this.ProID);
  ResultSet rs=ps.executeQuery();  if(rs.next())
  { 
   this.ProID=rs.getString(1);
   this.ProName=rs.getString(2);
   ps.close();
  }else
  {
   ps.close();
   throw new SQLException("Row not found!");
  }
 }
 //更新目前数据库
 private void updateRow()
     throws SQLException
 {
  String sqlStatement="update Province set ProName=? where ProID=?";
  PreparedStatement ps=con.prepareStatement(sqlStatement);
  ps.setString(1,this.ProName);
  ps.setString(2,this.ProID);  int num=ps.executeUpdate();
  ps.close();  if(num==0)
    throw new SQLException("Update row failed!");
 }
}

解决方案 »

  1.   

    java bean:
    package enterprise.javabean;import enterprise.ejb.*;
    import enterprise.common.*;
    import java.util.*;
    import javax.naming.*;
    import javax.naming.InitialContext;
    import javax.rmi.PortableRemoteObject;
    import javax.ejb.*;
    import java.sql.*;public class SchoolForm
    {
     private String SchID=null;
     private String SchProvince=null;
     private String SchName=null;
     private String SchIntr=null;
     SchoolHome schoolhome=null;
     
     public SchoolForm() throws Exception
     {
      Context initial=new InitialContext();
      Object objref=initial.lookup("ejb/SchoolEntityBean");
      schoolhome=(SchoolHome)PortableRemoteObject.narrow(objref,SchoolHome.class);
     } public Vector getSchoolList() throws Exception
     {
      Vector pVector=new Vector();
      Collection c=schoolhome.findAll(); 
      Iterator i=c.iterator();
     
      while(i.hasNext())
      {
       School school=(School)i.next();
       pVector.add(school.getProData());
      }
      return pVector;
     }
    }我在jsp页面是这样显示数据库的内容的(下拉框):
    <%  
      Vector pVector=School.getSchoolList();
      Enumeration pEnu=pVector.elements();
      int num = 0;
      while(pEnu.hasMoreElements()){
        ProvinceItem p=(ProvinceItem)pEnu.nextElement(); 
    %> 
                               <option value='<%=num++%>'><%=p.getProID()%>--<%=p.getProName()%></option>
    <%}%>就是把ProID, ProName显示在下拉框中,但是显示的结果是这两个都是一样的,如:
    1--1
    2--2
    3--3
    ……谢谢!!!!
      

  2.   

    //取得数据表单中的所有数据
     private ArrayList selectAll()
         throws SQLException
     {
      String sqlStatement="select ProID,ProName from Province";
      PreparedStatement ps=con.prepareStatement(sqlStatement);
      try
      {
       ResultSet rs=ps.executeQuery();
       ArrayList al=new ArrayList();
       while(rs.next())
       {
        String id=rs.getString(1);//
    //增加另一个读取name的*******************************************************//    al.add(id);
       }
       return al;
      }finally
      {
       ps.close();
      }
     }
    这个函数有问题,放到列表中的只是ProID
      

  3.   

    你的selectAll方法中 好像没有把ProName放到要返回的ArrayList中,findAll方法所以没有得到。不是推荐大家使用CMP的么?怎么还用BMP呀//取得数据表单中的所有数据
     private ArrayList selectAll()
         throws SQLException
     {
      String sqlStatement="select ProID,ProName from Province";
      PreparedStatement ps=con.prepareStatement(sqlStatement);
      try
      {
       ResultSet rs=ps.executeQuery();
       ArrayList al=new ArrayList();
       while(rs.next())
       {
        String id=rs.getString(1);
        al.add(id);
       }
       return al;
      }finally
      {
       ps.close();
      }
     }太多了,看着头晕死了
      

  4.   

    CoolAbu(阿卜-Never Stop(★★★★)) 的观点
    不过看起来太费神了
    能不能写的简练些?
      

  5.   

    //取得数据表单中的所有数据
     private ArrayList selectAll()
         throws SQLException
     {
      String sqlStatement="select ProID,ProName from Province";
      PreparedStatement ps=con.prepareStatement(sqlStatement);
      try
      {
       ResultSet rs=ps.executeQuery();
    //可以考虑用HashMap OR Hashtable
        java.util.HashMap h = new java.util.HashMap();
        
       while(rs.next())
       {
        String id=rs.getString(1);//
        String name =rs.getString(2);//    h.put(id,name);  
     }
       return h;
      }finally
      {
       ps.close();
      }
     }