JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted,
执行存储过程时,出现如下错误:
java.sql.SQLException: User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted, configure connection with "noAccessToProcedureBodies=true" to have driver generate parameters that represent INOUT strings irregardless of actual parameter types.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.DatabaseMetaData.getCallStmtParameterTypes(DatabaseMetaData.java:1616)
at com.mysql.jdbc.DatabaseMetaData.getProcedureColumns(DatabaseMetaData.java:4009)
at com.mysql.jdbc.CallableStatement.determineParameterTypes(CallableStatement.java:702)
at com.mysql.jdbc.CallableStatement.<init>(CallableStatement.java:513)
at com.mysql.jdbc.Connection.parseCallableStatement(Connection.java:4536)
at com.mysql.jdbc.Connection.prepareCall(Connection.java:4610)
at com.mysql.jdbc.Connection.prepareCall(Connection.java:4584)
经过查阅资料得知,JDBC在调用存储过程时不光用户要有execute的权限,还需要对mysql.proc具有访问权限。否则它无法访问metadata。有两种解决方法:
一.给数据库连接设置一个noAccessToProcedureBodies属性,属性值为true,示例如下:
- jdbc:mysql://ipaddress:3306/test?noAccessToProcedureBodies=true
网上说设置noAccessToProcedureBodies=true会带来一些影响(未经考证):
1. 调用存储过程时,将没有类型检查,设为字符串类型,并且所有的参数设为int类型,但是在调用registerOutParameter时,不抛出异常。
2. 存储过程的查询结果无法使用getXXX(String parameterName)的形式获取,只能通过getXXX(int parameterIndex)的方式获取。
二.给数据库用户赋权,赋执行mysql.proc表的select权限,示例如下:
- GRANT SELECT ON mysql.proc TO ‘user‘@‘localhost‘;
原文地址:http://www.cnblogs.com/AloneSword/p/3591702.html