我正在创建一个Web应用程序,它需要连接到数据库并获取各种类型报告的类别。当我在java中通过storedproc执行我的SQL时,我遇到了一个奇怪的错误。 SELECT * FROM RPT_CTGR;
是我提交的sql,但是当我查看堆栈跟踪时,它会返回:
org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call SELECT * FROM RPT_CTGR;(?)}]; nested exception is java.sql.SQLException: ORA-06550: line 1, column 38:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
. ( * @ % & = - + < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec as between || indicator multiset member
submultiset
org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:969)
org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1003)
org.springframework.jdbc.object.StoredProcedure.execute(StoredProcedure.java:144)
org.ifmc.qies.reportaudit.impl.CataImpl.search(CataImpl.java:59)
org.ifmc.qies.reportaudit.web.CataAction.execute(CataAction.java:31)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
由于某种原因,将额外的“(?)”附加到我的语句的末尾会导致错误。有什么想法吗?
代码
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.ifmc.qies.reportaudit.dao.CataDao;
import org.ifmc.qies.reportaudit.model.Catagory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import oracle.jdbc.OracleTypes;
public class CataImpl extends JdbcDaoSupport implements CataDao {
protected static ApplicationContext ctx = null;
//BaseStoredProcedure extends StoredProcedure
public class CataProc extends BaseStoredProcedure{
public CataProc(DataSource ds, String name) {
super(ds,name);
System.out.println(name);
System.out.println(getSql());
declareParameter(new SqlOutParameter("catagories", OracleTypes.CURSOR,
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum)
throws SQLException {
Catagory t = new Catagory();
t.setCatId(rs.getString(1));
t.setCat_name(rs.getString(2));
System.out.println(t.getCat_name());
return t;
}
}));
}
}
@Override
public List search() {
String[] paths = {"V:path/to/applicationcontext.xml"};
Map params=new HashMap();
ctx = new FileSystemXmlApplicationContext(paths);
DataSource ds = (DriverManagerDataSource)ctx.getBean("dataSource");
CataProc proc = new CataProc(ds,"SELECT * FROM RPT_CTGR;");
Map results = proc.execute(params);
List catagory = (List)results.get("catagories");
//Test
System.out.println(catagory.get(1).toString());
return catagory;
}
}
除了print
之外,我得到了所有catagories.get(1).toString
语句,以正确返回我提交的SQL
答案 0 :(得分:0)
您应该在Oracle数据库中创建一个执行Store Procedure
语句的SELECT
。
您可以参考this question作为参考。
E.g。
CREATE OR REPLACE PROCEDURE getCatagories(categories in out sys_refcursor) is
...
SELECT * FROM RPT_CTGR;
...
之后,您需要按名称调用存储过程。
public List search() {
...
DataSource ds = (DriverManagerDataSource) ctx.getBean("dataSource");
CataProc proc = new CataProc(ds,"getCatagories");
Map results = proc.execute(params);
List catagory = (List)results.get("catagories");
...
}