mysql存储过程返回查询结果集

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `testproc`;

CREATE TABLE `testproc` (

`id` int(4) NOT NULL,

`name` varchar(100) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1.2  插入数据

INSERT INTO `testproc` VALUES ('1', '第一条记录');

INSERT INTO `testproc` VALUES ('2', 'efgh');

INSERT INTO `testproc` VALUES ('3', 'ijklmn');

INSERT INTO `testproc` VALUES ('4', 'zxvb');

INSERT INTO `testproc` VALUES ('5', 'uiop');

INSERT INTO `testproc` VALUES ('6', '222');

INSERT INTO `testproc` VALUES ('7', '8888');

INSERT INTO `testproc` VALUES ('9', '第9条记录');

INSERT INTO `testproc` VALUES ('10', '第10条记录');

INSERT INTO `testproc` VALUES ('11', '第11条记录');

INSERT INTO `testproc` VALUES ('12', '第12条记录');

1.3  创建存储过程

CREATE PROCEDURE test_proc_multi_select()

BEGIN

select * from testproc;

select * from testproc where id=1;

END;

1.4 删除存储过程
DROP PROCEDURE test_proc_multi_select;

  二、JAVA相关代码

package action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.CallableStatement;

/**
 * 存储过程
 * @author Pei
 *
 */
public class CunChu {

	public static final String DBDRIVER = "com.mysql.jdbc.Driver";

	public static final String DBURL = "jdbc:mysql://127.0.0.1:3306/test1";

	public static final String DBUSER = "root";

	public static final String DBPASS = "123456";

	public static void main(String[] args) {
		Connection con = null;
		CallableStatement cs;
		try {
			Class.forName(DBDRIVER);
			con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
			String sql = "{call test_proc_multi_select()}";
			cs = (CallableStatement) con.prepareCall(sql);
			boolean hadResults = cs.execute();
			int i = 0;
			while (hadResults) {
				System.out.println("result No:----" + (++i));
				ResultSet rs = cs.getResultSet();
				while (rs != null && rs.next()) {
					int id1 = rs.getInt(1);
					String name1 = rs.getString(2);
					System.out.println(id1 + ":" + name1);
				}
				hadResults = cs.getMoreResults(); // 检查是否存在更多结果集
			}
		} catch (ClassNotFoundException e) {
		  e.printStackTrace();
		} catch (SQLException e) {
		  e.printStackTrace();
		} finally {
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}

	}
}

 pom.xml文件

<dependencies>
		  <dependency>
		    <groupId>javax.websocket</groupId>
		    <artifactId>javax.websocket-api</artifactId>
		    <version>1.1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId> 
		   <artifactId>javax.servlet-api</artifactId> 
		   <version>3.0.1</version> 
		   <scope>provided</scope>
		</dependency>
		
		<!--  数据库-->
		<dependency>
		  <groupId>mysql</groupId>
		  <artifactId>mysql-connector-java</artifactId>
		  <version>5.1.38</version>
		</dependency>
		<dependency>
		  <groupId>c3p0</groupId>
		  <artifactId>c3p0</artifactId>
		  <version>0.9.1.2</version>
		</dependency>
  </dependencies>

  

 

posted @ 2021-03-04 11:15  红尘沙漏  阅读(4588)  评论(0编辑  收藏  举报