简体   繁体   English

无法通过Java执行MySQL存储过程

[英]Can't execute a MySQL stored procedure from Java

I am connecting to a MySQL (5.08) database running on a linux machine from a web application running in tomcat. 我正在从在tomcat中运行的Web应用程序连接到在Linux机器上运行的MySQL(5.08)数据库。

I get the following exception when I try to execute a stored procedure: 当我尝试执行存储过程时,出现以下异常:

com.hp.hpl.chaos.web.exception.DBException: getNextValue for operatorinstance[Additional Information from SQL Exception][SQLErrorCode: 0 SQLState: S1000
    at com.hp.hpl.chaos.web.util.SQLUtil.getNextValue(SQLUtil.java:207)
        ..............

Caused by: 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:1619)
at com.mysql.jdbc.DatabaseMetaData.getProcedureColumns(DatabaseMetaData.java:4034)
at com.mysql.jdbc.CallableStatement.determineParameterTypes(CallableStatement.java:709)
at com.mysql.jdbc.CallableStatement.<init>(CallableStatement.java:513)
at com.mysql.jdbc.Connection.parseCallableStatement(Connection.java:4583)
at com.mysql.jdbc.Connection.prepareCall(Connection.java:4657)
at com.mysql.jdbc.Connection.prepareCall(Connection.java:4631)
at com.hp.hpl.chaos.web.util.SQLUtil.getNextValue(SQLUtil.java:196)
... 17 more

After installing mysql on the machine. 在机器上安装mysql之后。 I have given the follwing grant options to root 我已经将以下授予选项授予根目录

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'pass';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

In the java class.. 在java类中。

I am connecting to the db as follows: 我连接到数据库,如下所示:

String url = "jdbc:mysql://ipaddress:3306/test";
                con = DriverManager.getConnection(url,"root", "pass");

I have also tried the url with the noAccessToProcedureBodies=true option included. 我还尝试了包含noAccessToProcedureBodies=true选项的url。

Can someone tell me what is wrong here? 有人可以告诉我这里有什么问题吗? Is there anything I need to check? 我需要检查什么吗?

There are two ways to solve this: 有两种解决方法:

  1. set the connection's noAccessToProcedureBodies=true property 设置连接的noAccessToProcedureBodies=true属性

    For example as part of the connection string: 例如,作为连接字符串的一部分:

     jdbc:mysql://ipaddress:3306/test?noAccessToProcedureBodies=true 

    The JDBC driver will then create "INOUT" strings for the arguments without requiring meta data like the exception says. 然后,JDBC驱动程序将为参数创建“ INOUT”字符串,而不需要像异常说明那样的元数据。

  2. Grant SELECT privileges on mysql.proc to the database user 向数据库用户授予mysql.proc上的SELECT特权

    For example in the mysql prompt: 例如在mysql提示符下:

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

    Of course this would allow the application to read the entire mysql.proc table that contains information about all stored procedures in all databases (including source code). 当然,这将允许应用程序读取整个mysql.proc表,该表包含有关所有数据库中所有存储过程的信息(包括源代码)。

Following steps worked for me in mysql 以下步骤在mysql中为我工作

Step 1 : add follwong 步骤1:添加follwong
Connection con = DriverManager.getConnection("jdbc:mysql://ipaddress:3306/logparser?noAccessToProcedureBodies = true ", "root", ""); 连接con = DriverManager.getConnection(“ jdbc:mysql:// ipaddress:3306 / logparser?noAccessToProcedureBodies = true”,“ root”,“”);

Step 2 : GRANT ALL ON logparser . 步骤2:将所有内容logparser proc TO root@'%'; proc TO root @'%'; where logparser is DB name and proc is procedure name. 其中logparser是数据库名称,而proc是过程名称。

Running below queries should fix your issue. 在查询下面运行可以解决您的问题。

GRANT <SELECT, CREATE, UPDATE, DELETE, ALL> PRIVILEGES ON mysql.proc TO '<user>'@'%';
FLUSH PRIVILEGES;

You can change the definer in the mysql.proc table to your database user. 您可以将mysql.proc表中的定义器更改为数据库用户。

select * from mysql.proc; 从mysql.proc选择*;

choose the stored proc that has problem and change the definer to 'yourdbuser'@'localhost'. 选择有问题的存储过程,然后将定义器更改为“ yourdbuser” @“ localhost”。

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

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