Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从存储过程中获取mysql游标值_Java_Mysql_Jdbc_Java Stored Procedures - Fatal编程技术网

Java 如何从存储过程中获取mysql游标值

Java 如何从存储过程中获取mysql游标值,java,mysql,jdbc,java-stored-procedures,Java,Mysql,Jdbc,Java Stored Procedures,如何在java程序中获取mysql游标值 这是我的mysql存储过程 delimiter // CREATE PROCEDURE cursor_student() BEGIN DECLARE row_count INT DEFAULT 0; DECLARE exit_flag INT DEFAULT 0; DECLARE sid varchar(30); DECLARE sname varchar(50); DECLARE rst CURSOR FOR SELECT sid, snam

如何在java程序中获取mysql游标值

这是我的mysql存储过程

delimiter //
CREATE PROCEDURE cursor_student()
BEGIN
DECLARE row_count INT DEFAULT 0;
DECLARE exit_flag INT DEFAULT 0;
DECLARE sid varchar(30);
DECLARE sname varchar(50);
DECLARE rst CURSOR FOR 
    SELECT sid, sname FROM student WHERE class = '11th';

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET exit_flag=1;

OPEN rst;
fetch_loop: LOOP

    FETCH rst INTO sid, sname;  
    IF exit_flag THEN 
        LEAVE fetch_loop;
    END IF;
    SET row_count = row_count +1;
END LOOP;
CLOSE rst;  
SELECT 'number of rows fetched =', row_count;
END;
import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class StoredProcedure {

public static void main(String[] args) {
    Connection dbConnection = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "test";
    String driver = "com.mysql.jdbc.Driver";
    ResultSet rs = null;
    CallableStatement callableStatement = null;
    String getDBUSERCursorSql = "{call cursor_student}";
    try {
        Class.forName(driver);
        dbConnection = DriverManager.getConnection(url + db, "root", "");
        try {

            callableStatement = dbConnection.prepareCall(getDBUSERCursorSql);


            callableStatement.executeUpdate();

            rs = callableStatement.getResultSet();

            while (rs.next()) {


                System.out.println("sid "+rs.getString(1)  +"  name  "+rs.getString(2));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
这是我的simlpe java程序读取上面的存储过程

delimiter //
CREATE PROCEDURE cursor_student()
BEGIN
DECLARE row_count INT DEFAULT 0;
DECLARE exit_flag INT DEFAULT 0;
DECLARE sid varchar(30);
DECLARE sname varchar(50);
DECLARE rst CURSOR FOR 
    SELECT sid, sname FROM student WHERE class = '11th';

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET exit_flag=1;

OPEN rst;
fetch_loop: LOOP

    FETCH rst INTO sid, sname;  
    IF exit_flag THEN 
        LEAVE fetch_loop;
    END IF;
    SET row_count = row_count +1;
END LOOP;
CLOSE rst;  
SELECT 'number of rows fetched =', row_count;
END;
import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class StoredProcedure {

public static void main(String[] args) {
    Connection dbConnection = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "test";
    String driver = "com.mysql.jdbc.Driver";
    ResultSet rs = null;
    CallableStatement callableStatement = null;
    String getDBUSERCursorSql = "{call cursor_student}";
    try {
        Class.forName(driver);
        dbConnection = DriverManager.getConnection(url + db, "root", "");
        try {

            callableStatement = dbConnection.prepareCall(getDBUSERCursorSql);


            callableStatement.executeUpdate();

            rs = callableStatement.getResultSet();

            while (rs.next()) {


                System.out.println("sid "+rs.getString(1)  +"  name  "+rs.getString(2));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

上述java程序的输出为

  sid number of rows fetched =  name  6
但我想显示sid和sname的值

+------+-------+
| sid  | sname |
+------+-------+
| 1    | asdf  |
| 2    | dff   |
| 3    | gggg  |
| 4    | tttt  |
| 5    | mmmm  |
| 6    | .uyy  |
+------+-------+

我认为您的SP有问题

您能否根据此SP示例进行更改?我还没有执行,但我想是的

DROP PROCEDURE IF EXISTS  mysql_cursor_example $$
CREATE PROCEDURE mysql_cursor_example ( IN in_name VARCHAR(255) )
BEGIN
  -- First we declare all the variables we will need
  DECLARE l_name    VARCHAR(255);
  -- flag which will be set to true, when cursor reaches end of table
  DECLARE exit_loop BOOLEAN;         

  -- Declare the sql for the cursor
  DECLARE example_cursor CURSOR FOR
    SELECT name status_update
    FROM employees
    WHERE name = name_in;

  -- Let mysql set exit_loop to true, if there are no more rows to iterate
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;

  -- open the cursor
  OPEN example_cursor;

  -- marks the beginning of the loop
  example_loop: LOOP

    -- read the name from next row into the variable l_name
    FETCH  example_cursor INTO   l_name;

    -- check if the exit_loop flag has been set by mysql, 
    -- if it has been set we close the cursor and exit 
    -- the loop
    IF exit_loop THEN
        CLOSE example_cursor;
        LEAVE example_loop;
    END IF;

  END LOOP example_loop;
END $$

DELIMITER ;

重新标记为[mysql]。我认为问题出在
mysql
side@user000001所以java代码是正确的。如果没有测试,我不能这么说。但是根据您的输出,结果集似乎充满了
SELECT'number of rows fetched=',row_count。这使我相信问题出在存储过程中。不过我可能错了。我的SQL知识有限。