1
我在我的數據庫中有一個名爲BalanceOnDate的準備語句,當我嘗試從下面的方法調用它時,出現以下錯誤,我盡力弄清楚了但我沒有看到任何線索的解決方案,請協助我,如何在java中正確調用預處理語句
public void fillClosingMontBalance(Month closingMonth) throws Exception {
Connection con = connection;
CallableStatement csForPreMonth = (CallableStatement) connection.prepareCall("{call BalanceOnDate(?,?,?)}");
for (Iterator i = ledgerList.iterator(); i.hasNext();) {
Ledger ledger = (Ledger) i.next();
csForPreMonth.setString(1, "St_" + ledger.getStockType());
csForPreMonth.setString(2, ledger.getFullLedgerNo());
csForPreMonth.setString(3, closingMonth.getFullDate());
ResultSet curRs = csForPreMonth.executeQuery();
curRs.last();
MonthEndBalance endBalance = new MonthEndBalance(curRs.getString("itmTyp"), curRs.getString("fulItmCode"),
curRs.getDouble("op_Balance"), curRs.getDouble("balance"), curRs.getDouble("total_received"),
curRs.getDouble("total_issued"), curRs.getString("unit"), curRs.getString("batchNo"),
curRs.getDouble("rawPrice"), curRs.getDouble("avg_unit_price"), curRs.getDate("sysDateTime"),
curRs.getString("facCode"), curRs.getString("comCode"), curRs.getString("fullLedgerNo"),
curRs.getString("stockType"), curRs.getString("fullItemName"));
closingMonthRecords.add(endBalance);
}
}
錯誤;
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: FUNCTION BalanceOnDate does not exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2690)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2619)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1465)
at com.mysql.jdbc.DatabaseMetaData.getCallStmtParameterTypes(DatabaseMetaData.java:1602)
at com.mysql.jdbc.DatabaseMetaData.getProcedureOrFunctionColumns(DatabaseMetaData.java:4141)
at com.mysql.jdbc.DatabaseMetaData.getProcedureColumns(DatabaseMetaData.java:4059)
at com.mysql.jdbc.CallableStatement.determineParameterTypes(CallableStatement.java:830)
at com.mysql.jdbc.CallableStatement.<init>(CallableStatement.java:628)
at com.mysql.jdbc.JDBC4CallableStatement.<init>(JDBC4CallableStatement.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.CallableStatement.getInstance(CallableStatement.java:524)
at com.mysql.jdbc.ConnectionImpl.parseCallableStatement(ConnectionImpl.java:4081)
at com.mysql.jdbc.ConnectionImpl.prepareCall(ConnectionImpl.java:4165)
at com.mysql.jdbc.ConnectionImpl.prepareCall(ConnectionImpl.java:4139)
at Manufacturing.MonthEnd.FinishMonth.fillPreviousMonthBalanceRec(FinishMonth.java:69)
at COMMON.FrmMDI.jButton4ActionPerformed(FrmMDI.java:4117)
at COMMON.FrmMDI.access$14300(FrmMDI.java:72)
at COMMON.FrmMDI$96.actionPerformed(FrmMDI.java:1678)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
at java.awt.Component.processMouseEvent(Component.java:6203)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:5968)
at java.awt.Container.processEvent(Container.java:2105)
at java.awt.Component.dispatchEventImpl(Component.java:4564)
at java.awt.Container.dispatchEventImpl(Container.java:2163)
at java.awt.Component.dispatchEvent(Component.java:4390)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
at java.awt.Container.dispatchEventImpl(Container.java:2149)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4390)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$2.run(EventQueue.java:622)
at java.awt.EventQueue$2.run(EventQueue.java:620)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
以下是在數據庫中我的存儲過程,
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`user1`@`localhost` PROCEDURE `BalanceOnDate`(stname varchar(20),ledno varchar(100), fDate Varchar(12))
BEGIN
set @x = concat('
SELECT
Ledger.`StockType` AS Ledger_StockType,
Ledger.`FullLedgerNo` AS Ledger_FullLedgerNo,
Ledger.`BackLedgerNo` AS Ledger_BackLedgerNo,
Ledger.`ForwdLedgerNo` AS Ledger_ForwdLedgerNo,
Ledger.`OpeningBalance`AS Ledger_OpeningBalance,
Ledger.`ClosingBalance`AS Ledger_ClosingBalance,
Ledger.`Unit` AS Ledger_Unit,
Ledger.`User` AS Ledger_User,
Ledger.`ItemType` AS Ledger_ItemType,
Ledger.`Active` AS Ledger_Active,
Ledger.`FullITCode` AS Ledger_FullITCode,
St.`ID` AS St_BFG_ID,
St.`InQty` AS St_BFG_InQty,
St.`OutQty` AS St_BFG_OutQty,
round(St.InQ - St.OuQ, 2) as Balance,
St.`Remarks` AS St_BFG_Remarks,
St.`BatchNo` AS St_BFG_BatchNo,
St.`EventNo` AS St_BFG_EventNo,
St.`FrToLedgerNo` AS St_BFG_FrToLedgerNo,
St.`FrmEvent` AS St_BFG_FrmEvent,
St.`User` AS St_BFG_User,
St.`Dat` AS St_BFG_Dat,
St.`Unit` AS St_BFG_Unit,
St.`RawPrice` as St_row_price,
St.`Uprice` as UnitPrice,
St.`Balance` as St_balance,
item_grade.`FulItmName` AS item_grade_FulItmName,
Company.`CompanyName` AS Company_CompanyName,
Factory.`Factory` AS Factory_Factory,
St.`SupID` AS St_BFG_SupID
FROM (SELECT @inCsum:=0,@outCsum:=0) as xyz,
(SELECT @inCsum := @inCsum + InQty as InQ,@outCsum := @outCsum + OutQty as OuQ,d.*
FROM ',stname,' d WHERE FullLedgerNo= "',ledno,'" and Dat <= "', fDate ,'" ORDER BY Dat, SupID) as St
LEFT OUTER JOIN `Ledger` as Ledger ON St.`FullLedgerNo` = Ledger.`FullLedgerNo`
LEFT OUTER JOIN `item_grade` item_grade ON Ledger.`FullITCode` = item_grade.`FulItmCode`
LEFT OUTER JOIN `Factory` Factory ON St.`FacCode` = Factory.`FacCode`
LEFT OUTER JOIN `Company` Company ON Ledger.`ComCode` = Company.`ComCode`
WHERE Ledger.`FullLedgerNo` = "',ledno,'" and St.`Unit` = Ledger.`Unit`');
PREPARE stmt FROM @x;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
FUNCTION BalanceOnDate不存在的部分不清楚嗎?它告訴你名爲'BalanceOnDate'的數據庫中的存儲過程沒有被定義。要麼它不存在,要麼處於不同的模式中,並且名稱需要使用模式進行限定。 – 2012-02-02 02:49:09
不需要粗魯 – jere 2012-02-02 02:51:18