2016-09-23 115 views
-1

我正在处理spring应用程序。我现在面临的问题与数据库连接,以获得records.Below是例外:尝试获取数据时发生SQLException

Error org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SELECT dstoreNumber, details, VALUE FROM dstore.table1 where value='400s' ]; nested exception is java.sql.SQLException: ORA-00942: table or view does not exist 

我连接到分别位于两个不同的数据库服务器,两个不同的模式。

我在一个类中创建了两个JdbcTemplate对象。以下是我的代码。

spring-beans.properties

##### dstore Datasource ##### 
dstore.dataSource.url=jdbc:oracle:thin:@dstore1.dev.xyz.com:5150:dstore 
dstore.dataSource.username=dstore 
dstore.dataSource.password=password 

##### dscon Datasource ##### 
dscon.dataSource.url=jdbc:oracle:thin:@dscon.dev.xyz.com:5150:dscon 
dscon.dataSource.username=dscon 
dscon.dataSource.password=password 

的src /主/资源/ META-INF /数据源-config.xml中

<bean name="dstoreDataSource" class="oracle.jdbc.pool.OracleConnectionPoolDataSource"> 
     <property name="URL" value="${dstore.dataSource.url}" /> 
     <property name="user" value="${dstore.dataSource.username}" /> 
     <property name="password" value="${dstore.dataSource.password}" /> 
    </bean> 
<bean name="dsconDataSource" class="oracle.jdbc.pool.OracleConnectionPoolDataSource"> 
     <property name="URL" value="${dscon.dataSource.url}" /> 
     <property name="user" value="${dscon.dataSource.username}" /> 
     <property name="password" value="${dscon.dataSource.password}" /> 
    </bean> 

的src /主/资源/ META-INF /弹簧beans.xml中

<import resource="classpath:META-INF/dstore/dstore-config.xml" /> 
<import resource="classpath:META-INF/dscon/dscon-config.xml" /> 
<bean name="jdbcTemplateServicedstore" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <property name="dataSource" ref="dstoreDataSource" /> 
</bean> 
<bean name="jdbcTemplateServicedscon" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <property name="dataSource" ref="dsconDataSource" /> 
</bean> 

的src /主/资源/ META-INF/dstore/dstore-config.xml中

<?xml version="1.0"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <tx:annotation-driven proxy-target-class="false" /> 

    <bean id="dstoreServicingTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="dstoreServicing" /> 
     <qualifier value="servicing" /> 
    </bean> 

    <bean id="dstoreServicing" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dstoreDataSource" /> 
     <property name="persistenceUnitName" value="dstoreServicing" /> 
     <property name="persistenceXmlLocation" 
      value="classpath:META-INF/dstore/jpa-persistence.xml" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true" /> 
       <property name="databasePlatform" value="org.hibernate.dialect.Oracle9iDialect" /> 
      </bean> 
     </property> 
    </bean> 
</beans> 

的src /主/资源/ META-INF/DSCON/DSCON-config.xml中

<?xml version="1.0"?> 

<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <aop:aspectj-autoproxy proxy-target-class="false" /> 

    <context:annotation-config /> 

    <tx:annotation-driven transaction-manager="dsconTransactionManager" proxy-target-class="false" /> 

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 

    <bean id="dsconTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="dataSource" ref="dscondatasource" /> 
     <property name="entityManagerFactory" ref="dscon"/> 
    </bean> 

    <bean id="dscon" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dscondatasource" /> 
     <property name="persistenceUnitName" value="dscon" /> 
     <property name="persistenceXmlLocation" value="classpath:META-INF/myloans/jpa-persistence.xml" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true" /> 
       <property name="databasePlatform" value="org.hibernate.dialect.Oracle9iDialect" /> 
      </bean> 
     </property> 
     <property name="loadTimeWeaver"> 
      <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> 
     </property> 
    </bean> 
</beans> 

Java类:在下面的类myDAO.getMethod2()执行成功,但是当我打电话myDAO .getMethod1(),抛出异常“表或视图不存在”。

@component 
class LoadData{ 
    @Inject 
    private MyDAO myDAO; 

//Gets values from database 
    @PostConstruct 
    public void loadListCodes() 
    { 
    final List<MyDTO> listValues = myDAO.getMethod2(); //successfully connected to database 
     try { 
     final List<MyDTO> listValue = myDAO.getMethod1(); //unable to connect to Database, exception is thrown 
     LOG.info("Method1 : " + listValue); 
     } catch (Exception e) { 
      LOG.info("exception " + e); 
      System.out.println("exception is :: " + e); 
    } 
    //logic goes here 
    } 
    } 

Java类:

@Repository 
public class MyDAOJDBCImpl implements MyDAO 
{ 

@Inject 
@Qualifier("jdbcTemplateServicedstore") 
private JdbcTemplate jdbcTemplate1; 

@Inject 
@Qualifier("jdbcTemplateServicedscon") 
private JdbcTemplate jdbcTemplate2; 

private static final String SQL1 = "SELECT dstoreNumber, details, VALUE FROM dstore.table1 " 
+ "WHERE value = '400S' "; 

private static final String SQL2 = "SELECT dsconNumber, details, VALUE FROM dscon.table1 " 
+ "WHERE value = '700S' "; 


public List<MyDTO> getMethod1() 
{ 
//unable to connect to database, throwing exception 
List<MyDTO> listValues = null; 
LOG.info("Retrieving data.."); 
try 
{ 
listValues = jdbcTemplate1.query(SQL1, 

new RowMapper<ListValueDTO>() 
{ 
public MyDTO mapRow(final ResultSet rs, final int rowNum) throws SQLException 
{ 
    final MyDTO listValueDTO = new MyDTO(); 
    listValueDTO.setdstoreNumber(rs.getString("dstoreNumber)); 
    listValueDTO.setDetails(rs.getString("details")); 
    listValueDTO.setValue(rs.getString("value")); 
    return listValueDTO; 
} 
}); 
} 
catch (Exception e) 
{ 
LOG.error("Error :" + e); 
} 

return listValues; 
} 

public List<MyDTO> getMethod2() 
{ 
List<MyDTO> listValues = null; 
LOG.info("Retrieving data.."); 
try 
{ 
    listValues = jdbcTemplate2.query(SQL2, 

    new RowMapper<ListValueDTO>() 
    { 
    public MyDTO mapRow(final ResultSet rs, final int rowNum) throws SQLException 
    { 
     final MyDTO listValueDTO = new MyDTO(); 
     //code goes here 
     return listValueDTO; 
    } 
    }); 
} 
catch (Exception e) 
{ 
    LOG.error("Error :" + e); 
} 

return listValues; 
} 
} 

PS:我连接到分别位于两个不同的数据库服务器和不同的模式两个不同的表。我想当我调用getMethod1()时,它无法找到架构或无法连接到其他数据库服务器。我该如何解决这个问题?任何人都需要面对这个问题。我需要在一个java类中调用并执行不同服务器中存在的不同数据库表。

+0

您是否使用TOAD和您的APP使用相同的Oracle用户? – reos

+0

是的,你的意思是连接数据库的凭据?@reos – Ran

+0

是的,如果你可以在TOAD上执行语句,那么你可以在你的应用程序中执行语句。可能您正在使用不同的用户,或者您正在执行不同的声明。 – reos

回答

1

异常ORA-00942: table or view does not exist通常表示该表不存在,例如您在表名中犯了错误,或者您没有从此表中选择的权限。

+0

我有一张桌子,我有补助金..我可以在TOAD中执行查询并获得结果..我猜测上面提到的代码中有些映射是错误的。 – Ran

相关问题