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