ssingh
ssingh

Reputation:

error while using groovy.sql Out parameter

I am trying to execute some stored procedures in groovy way. I am able to do it quite easily by using straight JDBC but this does not seem in the spirit of Grails.

I am trying to call the stored procedure as:

sql.query( "{call web_GetCityStateByZip(?,?,?,?,?)}",[params.postalcode, 
sql.out(java.sql.Types.VARCHAR), sql.out(java.sql.Types.VARCHAR), 
sql.out(java.sql.Types.INTEGER), sql.out(java.sql.Types.VARCHAR)]) { rs ->
   params.city = rs.getString(2)
   params.state = rs.getString(3)
}

I tried various ways like sql.call. I was trying to get output variable value after this.

Everytime error:

Message: Cannot register out parameter. 
Caused by: java.sql.SQLException: Cannot register out parameter. 
Class: SessionExpirationFilter 

but this does not seem to work.

Can anyone point me in the right direction?

Upvotes: 1

Views: 4486

Answers (3)

Tomas Peterka
Tomas Peterka

Reputation:

groovy way could be this code:

def getHours(java.sql.Date date, User user) throws CallProceduresServiceException {

    log.info "Calling stored procedure for getting hours statistics."
    def procedure
    def hour
    try {
        def sql = Sql.newInstance(dataSource.url, user.username, user.password, dataSource.driverClassName)
        log.debug "Date(first param): '${date}'"

        procedure = "call ${dbPrefixName}.GK_WD_GET_SCHEDULED_TIME_SUM(?, ?, ?, ?)"
        log.debug "procedure: ${procedure}"

        sql.call("{${procedure}}", [date, Sql.out(Sql.VARCHAR.getType()), Sql.out(Sql.VARCHAR.getType()), Sql.out(Sql.VARCHAR.getType())]) {
            hourInDay, hourInWeek, hourInMonth -> 
            log.debug "Hours in day: '${hourInDay}'"
            log.debug "Hours in week: '${hourInWeek}'"
            log.debug "Hours in month: '${hourInMonth}'"
            hour = new Hour(hourInDay, hourInWeek, hourInMonth)
        }
        log.info "Procedure was executed."
    } 
    catch (SQLException e) {
        throw new CallProceduresServiceException("Executing sql procedure failed!"
                + "\nProcedure: ${procedure}", e)
    }       
    return hour 
}

In my app it works great.

Tomas Peterka

Upvotes: 0

ssingh
ssingh

Reputation:

Thanks Internet Friend, If i write code like-

    Sql sql = new Sql(dataSource)
Connection conn
ResultSet rs
try {
        conn = sql.createConnection()
        CallableStatement callable = conn.prepareCall(
        "{call web_GetCityStateByZip(?,?,?,?,?)}")
         callable.setString("@p_Zip",params.postalcode)
         callable.registerOutParameter("@p_City",java.sql.Types.VARCHAR)
         callable.registerOutParameter("@p_State",java.sql.Types.VARCHAR)
         callable.registerOutParameter("@p_RetCode",java.sql.Types.INTEGER)
         callable.registerOutParameter("@p_Msg",java.sql.Types.VARCHAR)
         callable.execute()
        params.city = callable.getString(2)
        params.state = callable.getString(3)
    }

It working well in JDBC way. But i wanted to try it like the previous code using sql.query/sql.call.

Any comments??

Thanks Sadhna

Upvotes: 0

Internet Friend
Internet Friend

Reputation: 1072

This is still unanswered, so I did a bit of digging although I don't fully understand the problem. The following turned up from the Groovy source, perhaps it's of some help:

This line seems to be the origin of the exception:

http://groovy.codehaus.org/xref/groovy/sql/Sql.html#1173

This would seem to indicate that you have a Statement object implementing PreparedStatement, when you need the subinterface CallableStatement, which has the registerOutParameter() method which should be ultimately invoked.

Upvotes: 1

Related Questions