尝试获取数据时的SQLException

时间:2016-09-23 20:31:09

标签: java database spring oracle

我正在申请春季申请。我面临着与数据库连接以获取记录的问题。下面是例外:

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类中调用并执行不同服务器中存在的不同数据库表。

1 个答案:

答案 0 :(得分:1)

例外ORA-00942: table or view does not exist通常意味着该表不存在,例如您在表名中犯了错误,或者您没有授权从该表中进行选择。