日期:2014-05-20  浏览次数:20773 次

一个bmp的问题,一个cmp的问题
我是初学者,在做实体bean,用bmp,cmp做了同一个程序都出错。
一)bmp错误:  
client: ejb编译部署成功,调用下边方法的时候出错
bean: public studentPK ejbFindByPrimaryKey(studentPK pk) throws FinderException {
  try {
  conn = getConnection();
  PreparedStatement pstmt = conn.prepareStatement(
  "select id from student where id=?");
  pstmt.setInt(1, pk.id);
  System.out.println("查找学号:"+pk.id);
  ResultSet rs = pstmt.executeQuery();
  System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@"+rs.next()); if (rs.next()) { System.out.println("600000000000000000000000");
  int id = rs.getInt("id");
  return new studentPK(id);
  }
  else {
  throw new FinderException("该主键没有记录"); }
  }
  catch (Exception e) {
  throw new EJBException(e.toString());
  }
  /**@todo Complete this method*/
  finally {
  try {
  conn.close();
  }
  catch (Exception e) {
  e.printStackTrace();
  }
  }
  }红色标记前能执行,以后的即使rs.next()是真也不执行,直接抛出错误 该主键没有记录
 public void ejbLoad()该方法一样出错 {
  PreparedStatement pstmt=null;
  try{
  pstmt=conn.prepareStatement
  ("select * from student where id=?");
  pstmt.setInt(1,pk.id);
  System.out.println("pk.id="+pk.id);
  ResultSet rs=pstmt.executeQuery();
  System.out.println(rs.next());
  if(rs.next())同上即使返回真大括号里的也不执行抛出错误“找不到这个学生” {
  id=rs.getInt("id");
  name=rs.getString("name");
  sex=rs.getString("sex");
  System.out.println(id +name +sex);
  }else{
  throw new EJBException("找不到这个学生");
  }
  }catch(Exception e){
  throw new EJBException(e.toString());
  }
  finally{
  try{
  pstmt.close();
  }catch(Exception ex){
  }
  }
  }
二)cmp错误
。。。。。。。使用数据库sqlserver2005在jbuilder里引入数据源这一步就失败了= =!
 数据库驱动 :com.microsoft.sqlserver.jdbc.SQLServerDriver
  URL : jdbc:sqlserver://localhost:1433;DatabaseName=学生成绩管理系统
  USER : sa 
 PASSWORD : sa
  datasource jndi name: sqlserver 
。。。。jndi里sqlserver数据源建立成功,测试成功。另外数据库里属性有7个,我在bean里为了简单只定义了2个,是出错的原因吗。。。哭求~~~高手指导

------解决方案--------------------
1. resultset是游标而非查询结果集合 执行next下移指向下一个记录
你的查询结果如果只有一列,在执行第一个next时指向该列
执行第二个next指向空,返回false 
执行else代码 就出现了你的:直接抛出错误 该主键没有记录 

2. 
This could be a problem with the driver itself, or that the driver is not found on the classpath. 
没找到驱动