JDBC链接执行MySQL存储过程报权限错误

JDBC链接执行MySQL存储过程报权限错误

mysqljdbcnoAccessToProcedureBodies存储过程异常 sql

在java中经过JDBC链接MySQL执行存储过程时报错:数据库

 

Log代码  收藏代码less

  1. 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.  ui

 

通过查阅资料得知,JDBC在调用存储过程时不光用户要有execute的权限,还须要对mysql.proc具备访问权限。不然它没法访问metadata。有两种解决方法:spa

一.给数据库链接设置一个noAccessToProcedureBodies属性,属性值为true,示例以下:.net

 

Xml代码  收藏代码blog

  1. jdbc:mysql://ipaddress:3306/test?noAccessToProcedureBodies=true  ip

 

 网上说设置noAccessToProcedureBodies=true会带来一些影响(未经考证):


1. 调用存储过程时,将没有类型检查,设为字符串类型,而且全部的参数设为int类型,可是在调用registerOutParameter时,不抛出异常。

2. 存储过程的查询结果没法使用getXXX(String parameterName)的形式获取,只能经过getXXX(int parameterIndex)的方式获取。

 

 

二.给数据库用户赋权,赋执行mysql.proc表的select权限,示例以下:

Sql代码  收藏代码

  1. GRANT SELECT ON mysql.proc TO 'user'@'localhost';  

 

 

参考资料:

  1. http://stackoverflow.com/questions/986628/mysql-java-cant-execute-stored-procedure

  2. http://space.itpub.net/18945822/viewspace-683363

  3. http://cau99.blog.51cto.com/1855224/348792

相关文章
相关标签/搜索