可是我用jsp测试过了数据源,数据源配置没问题呀,
我从网上也找到了好多关于这方面的贴子,可是没有一个给出了完整的解决办法,真是郁闷

解决方案 »

  1.   

    connection怎么写的jndi怎么培植的
      

  2.   

    ejb的主要代码如下:
       
      package book;
       //引入类包 public class BmpBookEJB implements EntityBean{
        
        //保存bookid字段值
       private String bookid;
      //保存bookname字段值   private String bookname;
       //保存bookprice字段值
      private double bookprice;
      private EntityContext ctx;
      private DataSource ds;
       
       private Connection con; public void setBookName(String bookname){
        this.bookname=bookname;
       }
       public void setBookPrice(double bookprice){
        this.bookprice=bookprice;
      }
       public String getBookName(){
         return this.bookname;
       }
      public double getBookPrice(){
         return this.bookprice;
       }
       
       //初始化数据库连接,初始化情境参数
       public void setEntityContext(EntityContext context){
         this.ctx=context;
         try{
      InitialContext initial=new InitialContext();
      ds=(DataSource)initial.lookup("java:comp/env/bookds");
        System.out.println("数据源连接成功!'");
      }catch(NamingException ne){
       System.out.println("数据源连接失败!");
              throw new EJBException(ne);
      } 
     }     
        
      //BMP需要Bean提供数据的插入
       public String ejbCreate(String bookid,String bookname,double bookprice) throws CreateException {       if(bookid==null)
        throw new CreateException("The bookid is required");
       try{
           con=ds.getConnection();
       }catch(Exception e)
       {
        System.out.println("connection连接失败");
       }
      try{
      PreparedStatement stmt=con.prepareStatement("insert into book values(?,?,?)");
       stmt.setString(1,bookid);
       stmt.setString(2,bookname);
       stmt.setDouble(3,bookprice);
       stmt.executeUpdate();
        stmt.close();
       con.close();
      }
      catch(SQLException e)
       {
    System.out.println(e);
       }
           this.bookid=bookid;
         this.bookname=bookname;
         this.bookprice=bookprice;
      //由Bean负责事务持久性,Bean负责返回主键值
        return bookid;
         }
          }
        
      控制台里打印出"数据源连接成功"字样,connection连接也是成功的,因为并没有捕获到"connection连接失败"的错误提示,但不明白却为何出现上述错误?