Parameter @x was not defined for stored procedure… with MS_SQL JDBC

后端 未结 1 1807
梦谈多话
梦谈多话 2021-01-19 13:59

I am trying to execute a stored procedure using SQL Server JDBC in a method:

//Connection connection, String sp_name, Mapparams input t         


        
1条回答
  •  抹茶落季
    2021-01-19 14:31

    Update 2017-10-07: The merge request to fix this issue has been accepted, so this should no longer be a problem with versions 6.3.4 and later.


    Yes, it is an unfortunate inconsistency that for mssql-jdbc the parameter names returned by DatabaseMetaData#getProcedureColumns do not match the names accepted by CallableStatement#setInt et. al.. If you consider it to be a bug then you should create an issue on GitHub and perhaps it will be fixed in a future release.

    In the meantime, however, you'll just have to work around it. So, instead of code like this ...

    ResultSet rs = connection.getMetaData().getProcedureColumns(null, "dbo", "MenuPlanner", null);
    while (rs.next()) {
        if (rs.getShort("COLUMN_TYPE") == DatabaseMetaData.procedureColumnIn) {
            String inParamName = rs.getString("COLUMN_NAME");
            System.out.println(inParamName);
        }
    }
    

    ... which produces ...

    @person
    @food
    

    ... you'll need to use code like this ...

    boolean isMssqlJdbc = connection.getClass().getName().equals(
            "com.microsoft.sqlserver.jdbc.SQLServerConnection");
    ResultSet rs = connection.getMetaData().getProcedureColumns(null, "dbo", "MenuPlanner", null);
    while (rs.next()) {
        if (rs.getShort("COLUMN_TYPE") == DatabaseMetaData.procedureColumnIn) {
            String inParamName = rs.getString("COLUMN_NAME");
            if (isMssqlJdbc && inParamName.startsWith("@")) {
                inParamName = inParamName.substring(1, inParamName.length());
            }
            System.out.println(inParamName);
        }
    }
    

    ... which produces ...

    person
    food
    

    0 讨论(0)
提交回复
热议问题