2016-09-23 166 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

相關問題