我在Postgres DBMS中创建了一个存储过程,并调用了该过程的Java函数。
运行Java函数后,我收到错误:
org.postgresql.util.PSQLException: L'indice de la colonne est hors limite : 1, nombre de colonnes : 0.
at org.postgresql.core.v3.SimpleParameterList.registerOutParameter(SimpleParameterList.java:49)
at org.postgresql.jdbc2.AbstractJdbc2Statement.registerOutParameter(AbstractJdbc2Statement.java:1968)
at org.postgresql.jdbc3.AbstractJdbc3Statement.registerOutParameter(AbstractJdbc3Statement.java:1511)
at DAO.FournisseurDAO.getLastId(FournisseurDAO.java:192)
at CONTROLLER.FournisseurController.getLastIdInDataBase(FournisseurController.java:96)
at VIEW.FournisseurUi.spinnerProperties(FournisseurUi.java:183)
at VIEW.FournisseurUi.componentsProperties(FournisseurUi.java:139)
at VIEW.FournisseurUi.<init>(FournisseurUi.java:100)
at VIEW.FournisseurUi$2.run(FournisseurUi.java:542)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
以下是程序代码:
CREATE OR REPLACE FUNCTION getlastfournisseurid(OUT lastid integer)
RETURNS integer AS
$BODY$
begin
select last_value into lastid from fournisseur_fournisseurid_seq ;
end;
$BODY$
LANGUAGE plpgsql;
这是功能代码:
public int getLastId() {
String fournisseurLastId = "{ call getLastFournisseurId() }";
int lastid = -1 ;
// Get Connection
Connection connecte = utility.DatabaseConnection.getInstance();
try {
// Create a callableStatement
CallableStatement clblStmt = connecte.prepareCall(fournisseurLastId);
// Process query
clblStmt.registerOutParameter(1, java.sql.Types.INTEGER);
ResultSet result = clblStmt.executeQuery();
result.next();
lastid = clblStmt.getInt(1);
// close used resources
clblStmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
return -1 ;
} finally {
try {
if (connecte != null) {
connecte.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
return lastid;
}
}
答案 0 :(得分:2)
您应该与该函数的参数进行通信(事实上,它的返回值),但您将调用定义为:
String fournisseurLastId = "{ call getLastFournisseurId() }";
您应该将其定义为:
String fournisseurLastId = "{ ? = call getLastFournisseurId() }";
否则,JDBC无法将第一个占位符注册为out参数 - 没有占位符可供注册。
答案 1 :(得分:2)
Postgres函数只是:函数。无需使用专为真实存储过程设计的JDBC CallableStatement
。
您的功能也可以简化。不需要PL / pgSQL函数。一个简单的SQL函数可以:
CREATE OR REPLACE FUNCTION getlastfournisseurid()
RETURNS integer AS
$BODY$
select lastvalue from fournisseur_fournisseurid_seq;
$BODY$
LANGUAGE sql;
(这假设表fournisseur_fournisseurid_seq
中永远不会有一行,如果有,则需要在select语句中添加limit 1
然后这样称呼:
Statement stmt = connecte.createStatement();
ResultSet rs = stmt.executeQuery("select getlastfournisseurid()");
rs.next();
lastid = rs.getInt(1);