簡體   English   中英

MySQL Java 存儲過程給出錯誤“參數索引 1 超出范圍”

[英]MySQL Java stored procedure gives error "Parameter index of 1 is out of range"

public static void main(String[] args) throws Exception {
        Connection connection = getMySqlConnection();
        CallableStatement proc = connection.prepareCall("{ call LCD_GetDispInfoAllTimeTable() }");
        proc.registerOutParameter(1, Types.INTEGER);
        proc.execute();
        int returnValue = proc.getInt(1);
        System.out.println(returnValue + "");
//      conn.close();
      }


      public static Connection getMySqlConnection() throws Exception {
        String driver = "com.mysql.jdbc.Driver";
        String url = "";
        String username = "";
        String password = "";

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
      }
    }

當我運行此代碼時,我在線程“main”中看到一個異常 java.sql.SQLException: Parameter index of 1 is out of range (1, 0) ,為什么?

此程序返回:

Niemstów 07 pętla   10  05:33:00    3673114 11558169    754378  1
NŻ Niemstów 05  16  05:35:00    3669905 11556510    754379  3
NŻ Niemstów 03  16  05:37:00    3666969 11555665    754380  3

我的程序;

CREATE DEFINER=`root`@`%` PROCEDURE `LCD_GetDispInfoAllTimeTable`()
BEGIN

    SELECT bs.name as bsName, tt.busstoptype as bsType, tt.time as ttTime, bs.longitude as lon, bs.latitude as lat, tt.timetable_id as ttID, 
    Bus_Stop_Status_GET( tt.timetable_id, bst.timetable_id, bst.busstate_id ) as bus_stop_status -- 0 zrobiony, 1 - aktualny, 2- pomiędzy, 3 następne
    FROM (SELECT * FROM  mpk_currentbusstate ORDER BY changestime desc LIMIT 1 )bst
    join mpk_timetable t ON( bst.timetable_id = t.timetable_id )
    join mpk_timetable tt ON ( t.linelogin_id  = tt.linelogin_id AND t.line_id = tt.line_id AND t.brigade = tt.brigade AND t.rate = tt.rate 
        and t.schedudle_id = tt.schedudle_id)
    LEFT JOIN mpk_busstop bs ON (bs.busstop_id = tt.busstop_id)
    LEFT JOIN mpk_busstate bt ON( bst.busstate_id = bt.busstate_id );

END

您需要在調用字符串中指定參數:

CallableStatement proc = connection.prepareCall("{ call LCD_GetDispInfoAllTimeTable(?) }");

注意到了? ,它說有一個參數要設置。 現在它知道有一個參數需要設置,就像 Java 或其他語言中的方法一樣。 如果你想使用多個參數,你可以寫多個? ,例如: ...LCD_GetDispInfoAllTimeTable(?, ?, ?)

就您的程序聲明而言,其代碼是:

 CREATE DEFINER=`root`@`%` PROCEDURE `LCD_GetDispInfoAllTimeTable`()
 BEGIN
 SELECT bs.name as bsName, tt.busstoptype as bsType, tt.time as ttTime,
 bs.longitude as lon, bs.latitude as lat, tt.timetable_id as ttID, 
 Bus_Stop

其中過程定義不包含任何參數定義,因此 setParameter 或 registerOutParameter 不能應用於此過程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM