• 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>
    

      

     

  • 相关阅读:
    Postman+Newman生成接口测试报告
    Spring Boot系列 – 2. Spring Boot提供的特性
    Spring Boot系列 – 5. 不同的环境使用不同的配置
    Spring Boot系列 – 4. 读取配置文件(application.yml)中的属性值
    MySQL高可用之MHA安装
    恢复误删除表黑科技之relay log大法(续)
    恢复误删除表黑科技之relay log大法
    关于mysqldump备份非事务表的注意事项
    利用binlog server及Xtrabackup备份集来恢复误删表(drop)
    基于Xtrabackup备份集来恢复某个误删除的表(drop)
  • 原文地址:https://www.cnblogs.com/xianz666/p/14479158.html
Copyright © 2020-2023  润新知