简体   繁体   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;
      }
    }

When I run this code I see a Exception in thread "main" java.sql.SQLException: Parameter index of 1 is out of range (1, 0) , why ?当我运行此代码时,我在线程“main”中看到一个异常 java.sql.SQLException: Parameter index of 1 is out of range (1, 0) ,为什么?

This procedure return :此程序返回:

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

My procedure ;我的程序;

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

You need to specify the parameter in your call String:您需要在调用字符串中指定参数:

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

Notice that ?注意到了? , it says that there is a parameter to be set. ,它说有一个参数要设置。 Now it knows that there is a parameter to be set, just like methods in Java or some other language.现在它知道有一个参数需要设置,就像 Java 或其他语言中的方法一样。 If you wanted to use multiple parameters you can write multiple ?如果你想使用多个参数,你可以写多个? , like: ...LCD_GetDispInfoAllTimeTable(?, ?, ?) . ,例如: ...LCD_GetDispInfoAllTimeTable(?, ?, ?)

as far as your procedecure declaration is concerned, its code is:就您的程序声明而言,其代码是:

 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

where the procedure definition does NOT contain any parameter definition, so setParameter or registerOutParameter, cannot be applied for this procedure其中过程定义不包含任何参数定义,因此 setParameter 或 registerOutParameter 不能应用于此过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM