//DAO层
public List getForList(String sql, Object... args) {
Connection connection = null;
System.out.println(args);
try {
connection = JdbcUtils.getConnection();
return queryRunner.query(connection, sql, new BeanListHandler(clazz), args);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}
//接口实现
public class BookListDaoImpl extends DAO<Book> implements BookListDao{
public List<Book> getByUserIdWithOrdered(String id) {
// String sql =
// "select distinct TBkInfo.*,courseInfo.name coursename from userInfo,courseInfo,TBkInfo " +
// "where userInfo.id = ? and (userInfo.major = courseInfo.major or courseInfo.major is null) " +
// "and courseInfo.TBkId = TBkinfo.Id "
// + "and TBkinfo.Id in ( select orderlist.TBkId from orderlist where orderlist.userid = ? )";
String sql = "select distinct TBkInfo.*,courseInfo.name coursename from userInfo,courseInfo,TBkInfo,orderlist ol " +
"where userInfo.id = ? and ol.userid = ? and userInfo.major = courseInfo.major " +
"and courseInfo.TBkId = TBkinfo.Id and TBkinfo.Id = ol.TBkId";
return getForList(sql,id, id);
}
}
异常:
java.sql.SQLException: ORA-00942: 表或视图不存在
Query: select distinct TBkInfo.*,courseInfo.name coursename from userInfo,courseInfo,TBkInfo,orderlist ol where userInfo.id = ? and ol.userid = ? and userInfo.major = courseInfo.major and courseInfo.TBkId = TBkinfo.Id and TBkinfo.Id = ol.TBkId Parameters: [2013215087, 2013215087]
at org.apache.commons.dbutils.QueryRunner.rethrow(QueryRunner.java:542)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:399)
at com.TBO.DAO.DAO.getForList(DAO.java:77)
at com.TBO.DAOImpl.BookListDaoImpl.getByUserIdWithOrdered(BookListDaoImpl.java:20)
at com.TBO.test.test.test(test.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
说明:select语句中的表都存在,sql语句在PLsql中能够得到正确结果。现将其写到java中,并使用通配符,出下一下情况:
情况一:
String sql =
"select distinct TBkInfo.*,courseInfo.name coursename from userInfo,courseInfo,TBkInfo " +
"where userInfo.id = ? and (userInfo.major = courseInfo.major or courseInfo.major is null) " +
"and courseInfo.TBkId = TBkinfo.Id "
+ "and TBkinfo.Id in ( select orderlist.TBkId from orderlist where orderlist.userid = ? )";
return getForList(sql,id, id);
结果:报上面提到的异常
情况二:将sql语句写实
String sql =
"select distinct TBkInfo.*,courseInfo.name coursename from userInfo,courseInfo,TBkInfo " +
"where userInfo.id = 2013215087 and (userInfo.major = courseInfo.major or courseInfo.major is null) " +
"and courseInfo.TBkId = TBkinfo.Id "
+ "and TBkinfo.Id in ( select orderlist.TBkId from orderlist where orderlist.userid = 2013215087)";
return getForList(sql);
结果:可得到正确结果
情况三:
public void test() {
DAO < Order > dao = new DAO < Order>();
dao.getForList("select * from orderlist ");
}
说明:Order是个bean,成员变量与数据库字段对应
结果:
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 10, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> mvcapp, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> oracle.jdbc.driver.OracleDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1br3s3l9n1dqtu2imjlrpx|427800ef, idleConnectionTestPeriod -> 0, initialPoolSize -> 20, jdbcUrl -> jdbc:oracle:thin:@127.0.0.1:1521:TBO, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 60, maxStatements -> 40, maxStatementsPerConnection -> 10, minPoolSize -> 20, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
java.lang.NullPointerException
at sun.reflect.misc.ReflectUtil.checkPackageAccess(ReflectUtil.java:164)
at sun.reflect.misc.ReflectUtil.isPackageAccessible(ReflectUtil.java:195)
at java.beans.Introspector.getBeanInfo(Introspector.java:154)
at org.apache.commons.dbutils.BeanProcessor.propertyDescriptors(BeanProcessor.java:358)
at org.apache.commons.dbutils.BeanProcessor.toBeanList(BeanProcessor.java:168)
at org.apache.commons.dbutils.BasicRowProcessor.toBeanList(BasicRowProcessor.java:134)
情况四:
public List<Book> getByUserIdWithOrdered(String id) {
String sql =
"select distinct TBkInfo.*,courseInfo.name coursename from userInfo,courseInfo,TBkInfo " +
"where userInfo.id = ? and (userInfo.major = courseInfo.major or courseInfo.major is null) " +
"and courseInfo.TBkId = TBkinfo.Id ";
return getForList(sql,id);
}
结果:得到正确结果
再次说明,以上的sql语句在PLsql中都能得到正确结果,getForList在多个模块中用到,也存在通配符,但只在当前情况下抛异常。数据库用户具有DAB权限