jboss 开发BMP报出找不到表??

J2EE 码拜 9年前 (2015-05-11) 654次浏览 0个评论
 

java.sql.SQLException: Table not found: STUDENT in statement [select ID from STUDENT where ID=?]
14:07:39,642 INFO  [STDOUT] at org.hsqldb.jdbc.jdbcUtil.throwError(Unknown Source)
14:07:39,642 INFO  [STDOUT] at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
14:07:39,642 INFO  [STDOUT] at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
14:07:39,642 INFO  [STDOUT] at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.prepareStatement(BaseWrapperManagedConnection.java:374)
14:07:39,642 INFO  [STDOUT] at org.jboss.resource.adapter.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:217)
14:07:39,642 INFO  [STDOUT] at com.test.BmpbookBean.ejbFindByPrimaryKey(BmpbookBean.java:102)
14:07:39,642 INFO  [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:07:39,652 INFO  [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
14:07:39,652 INFO  [STDOUT] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
14:07:39,652 INFO  [STDOUT] at java.lang.reflect.Method.invoke(Method.java:324)
14:07:39,652 INFO  [STDOUT] at org.jboss.ejb.plugins.BMPPersistenceManager.callFinderMethod(BMPPersistenceManager.java:560)
14:07:39,652 INFO  [STDOUT] at org.jboss.ejb.plugins.BMPPersistenceManager.findEntity(BMPPersistenceManager.java:289)
14:07:39,652 INFO  [STDOUT] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.findEntity(CachedConnectionInterceptor.java:302)
14:07:39,652 INFO  [STDOUT] at org.jboss.ejb.EntityContainer.findSingleObject(EntityContainer.java:1043)
14:07:39,652 INFO  [STDOUT] at org.jboss.ejb.EntityContainer.find(EntityContainer.java:679)
14:07:39,652 INFO  [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:07:39,692 INFO  [STDOUT] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
14:07:39,692 INFO  [STDOUT] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
14:07:39,692 INFO  [STDOUT] at java.lang.reflect.Method.invoke(Method.java:324)
14:07:39,692 INFO  [STDOUT] at org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeH
………………
代码如下:
public String ejbFindByPrimaryKey(String key) throws FinderException {
  Connection con = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  try {
   // Class.forName(“oracle.jdbc.driver.OracleDriver”);
  //String url=”jdbc:oracle:thin:@localhost:1521:SAMPLE”; 

  
  con=getConnection();
  //con=DriverManager.getConnection(url,”system”,”manager”);
  System.out.println(“connection”);
  ps=con.prepareStatement(“select ID from STUDENT where ID=?”);
  ps.setString(1,key);
  rs=ps.executeQuery();
  System.out.println(“successful”);

  }catch (Exception e) {
System.out.println(e);
System.out.println(“null point”);
e.printStackTrace();
  }finally{
  try{
  if(rs!=null){rs.close();}
  if(ps!=null){rs.close();}
  if(con!=null){rs.close();}
  }catch(Exception ignore){
  ignore.printStackTrace();
  }
  }
  return key;

  public Connection getConnection()throws Exception{
  try{
  Context ctx=new InitialContext();
  javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup(“java:comp/env/OracleDS”);
  return ds.getConnection();
  }catch(Exception e)
  {
  System.out.println(“Coudn””t get datasource”);
  e.printStackTrace();
  throw e;
  }
}
配置文件:
ejb-jar.xml
……..
<resource-ref>
<res-ref-name>OracleDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
如果用class.forName()来访问就没问题,不知为何??

up  帮帮忙
up
不知道对不对,你和数据库的连接应该是由ejb容器来管理的吧。

是BEAN管理的。
lookup方法里,直接查找OracleDS就行了:)
数据源我能找到啊
就是执行
  ps=con.prepareStatement(“select ID from STUDENT where ID=?”);
时报错
20分
不会啊,如果能找到数据源,那怎么能执行select语句出错呢?除非你配置的数据源不是和你的数据库对应的。再说你用Class.forName不是好用吗?所以我觉得你应该检查下在服务器里的数据源配置。
这是数据源的配置 :oralce-ds.xml
<datasources>
  <local-tx-datasource>
    <jndi-name>OracleDS</jndi-name>
    <connection-url>jdbc:oracle:thin:@127.0.0.1:1521:SAMPLE</connection-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <user-name>system</user-name>
    <password>manager</password> 
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>     
  </local-tx-datasource>
</datasources>
不知道joss配置数据源除了配置这一文件还配置哪些??
CMP时我也就配置这个文件,能用

你改成用bean容器来管理你的数据库连接,然后再试试。
我刚做了一个jboss+mysql的bmp实现,测试成功。
to sjcode
  我用的是jboss+eclipse+oracle,你用的是什么?
代码:
 public String ejbFindByPrimaryKey(String key) throws FinderException {
  Connection con= null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  try {
         con=getConnection();
   ps=con.prepareStatement(“select* from DB where SID=?”);
   ps.setString(1,key);
   rs=ps.executeQuery();
   System.out.println(“successful”);

  }catch (Exception e) {
e.printStackTrace();
  }finally{
   try{
   if(rs!=null){rs.close();}
   if(ps!=null){rs.close();}
   if(con!=null){rs.close();}
   }catch(Exception ignore){
   ignore.printStackTrace();
   }
  }
  return key;
}
  public Connection getConnection()throws Exception{
   try{
   Context ctx=new InitialContext();
   javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup(“java:comp/env/OracleDS”);
   return ds.getConnection();
  }catch(Exception e)
  {
   System.out.println(“Coudn””t get datasource”);
   e.printStackTrace();
   throw e;
  }
}
ejb-jar.xml:
……..
<entity >
         <description><![CDATA[<!– begin-user-doc –> You can insert your documentation for “”<em><b>BmpbookBean</b></em>””.]]></description>

         <ejb-name>Bmpbook</ejb-name>

         <home>com.test.BmpbookHome</home>
         <remote>com.test.Bmpbook</remote>
         <local-home>com.test.BmpbookLocalHome</local-home>
         <local>com.test.BmpbookLocal</local>

         <ejb-class>com.test.BmpbookBMP</ejb-class>
         <persistence-type>Bean</persistence-type>
         <prim-key-class>java.lang.String</prim-key-class>
         <reentrant>False</reentrant>
 <resource-ref>
 <res-ref-name>OracleDS</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
 </resource-ref>
 
      </entity>
……..

oralce-ds.xml内容如上。
望能帮帮我,谢谢

to sjcode
  如果可以的话,把你测试通过的代码和配置文件发一份给我,好吗?
 非常感谢
up Thanks
数据库没这个表?
有的
up 怎么没人帮我啊?

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明jboss 开发BMP报出找不到表??
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!