Java调用带有输入参数为Blob类型的存储过程

1.创建表和存储过程的SQL语句 
create table testblob (int_col integer, blob_col blob)
create PROCEDURE spp (IN INT_P INTEGER, INOUT BLOB_P BLOB)
P1:  BEGIN INSERT INTO testblob(INT_COL, BLOB_COL) values (INT_P, BLOB_P);
SELECT BLOB_COL INTO BLOB_P FROM testblob WHERE INT_COL = INT_P;
END P1 
2.Java代码调用该存储过程的实现

import java.sql.CallableStatement;
import java.sql.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
public class MySQLTest {
 public static void main(String[] args) throws Exception {
     callProc();
 }
 
 public static Connection getConnection() { 
     MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
        ds.setServerName("localhost");
        ds.setPortNumber(3306);
        ds.setDatabaseName("vtba");
        ds.setUser("root");
        ds.setPassword("12345678");
        System.out.println("URL:" + ds.getURL());
        try {
         Connection conn = ds.getConnection();
         System.out.println("Get connection: " + conn.toString());
         return conn;
         }catch (Exception e) {
          e.printStackTrace();
          return null;
        }
 }
 
 public static void callProc() {
     try{
         String sql = "{call spp(?,?)}";
            Connection conn = getConnection();
            CallableStatement ps = conn.prepareCall(sql);
         String txt = "ABC";
         byte[] b = txt.getBytes();
         System.out.print("The expected: ");
         printBytes(b);
      ps.setInt(1, 1);
      ps.setBytes(2, txt.getBytes());
      ps.registerOutParameter(2, java.sql.Types.BLOB);      
      ps.execute();     
      System.out.print("The real: ");
      byte[] o = ps.getBytes(2);
      printBytes(o);
     }catch(Exception e) {
      e.printStackTrace();
     }
    }
 
 private static void printBytes(byte[] b) {
     for (byte bb: b){
         System.out.print(bb);
         System.out.print(",");
     }
     System.out.println();
 }
}

转载于:https://my.oschina.net/kkkkkk/blog/277241

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值