jdbc执行预处理,批处理,LOB字段处理,调用存储过程
(1)jdbc执行预处理
PreparedStatment预备语句
eg:String sql="insert into user(id,name,birthday,money) values(5,'administrator',?,'1000000')";
PreparedStatement stmt=conn.prepareStatment(sql);
stmt.setTimestamp(1,new Timestamp(System.currentTimeMillis()));
stmt.execute();
(2)调用存储过程
对于标量函数 其调用字符串为{fn functionname(?,?)}/{fn functionname()}
对于存储过程 其调用字符串为{call proc(?,?)}/{call proc}/{call ?=proc(?,?)}
CallableStatement
无参数,无返回值的存储过程
String sql="{call test1()}";
CallableStatment stmt=conn.prepareCall(sql);
stmt.executeUpdate();
有参数,有返回值的存储过程
String sql="{call ?=test1(?)}"; //方式1
CallableStatment stmt=conn.prepareCall(sql);
stmt.setString(2,"test");
stmt.registerOutParameter(1,Type.Interger);
stmt.execute();
int result=stmt.getInt(1);
String sql="{call test1(?,?)}"; //方式2
CallableStatment stmt=conn.prepareCall(sql);
stmt.setString(1,"test");
stmt.registerOutParameter(2,Type.Interger);
stmt.execute();
int result=stmt.getInt(2);
(3)jdbc数据批处理
String sql="insert into user(id,name,birthday,money) values(?,?,?,?)";
PreparedStatement stmt=conn.prepareStatement(sql);
for (int i=1;i<1000;i++){
stmt.setInt(1,i);
stmt.setString(2,"test");
stmt.setDate(3,new Date(System.currentTimeMillis()));
stmt.setFloat(4,1000);
stmt.addBatch(sql);
}
int[] result=stmt.executeBatch();
(4)LOB字段(BLOB,CLOB)处理
处理常用类与方法
java.sql.ResultSet
Blob getBlob(int columnIndex);
Blob getBlob(string columnLabel);
Clob getClob(int columnIndex);
Clob getClob(string columnLabel);
-----------------------------------------------------------------
java.sql.Blob
long length();
byte[] getBytes(long startPosition,long length);
InputStream getBinaryStream();
InputStream getBinaryStream(long startPosition,long length);
OutputStream setBinaryStream(long startPosition);
java.sql.Clob
long length();
byte[] getSubString(long startPosition,long length);
Reader getCharacterStream();
Reader getCharacterStream(long startPosition,long length);
Writer setCharacterStream(long startPosition);
-----------------------------------------------------------------
java.sql.Connection
Blob createBlob();
Clob createClob();
----------------------------------------------------------------
eg:
PreparedStament stmt=conn.prepareStatement("select pic from book where bokkid='1'");
ResultSet rt=stmt.executeQuery();
if(rt.next()){
Blob blob=rt.getBlob(1);
Image img=ImageIO.read(blob.getInputStream());
}
--------------------------------------------------------------------------------------------
Blob blob=conn.createBlob();
int offset=0;
OutputStream out=blob.setBinaryStream(offset);
ImageIO.write(img,"PNG",out);
PreparedStament stmt=conn.prepareStatement("insert into book values(?,?)");
stmt.setInt(1,1);
stmt.setBlob(2,blob);
stmt.executeUpdate();
--------------------------------------------------------------------------------------------
PreparedStament stmt=conn.prepareStatement("insert into book values(?,?)");
stmt.setInt(1,1);
File file=new File("E:\\text.txt");
FileReader reader=new FileReader(file);
BufferReader buffer=new BufferReader(reader);
stmt.setCharacterStream(2,buffer,3);
stmt.execute();
--------------------------------------------------------------------------------------------
PreparedStament stmt=conn.prepareStatement("select pic from book where id=1");
ResuleSet rs=stmt.exexcuteQuery();
if(rs.next()){
String strTmp="";
Reader read=rs.getCharacterStream(1);
BufferReader br=new BufferReader(read);
File file=new File("E:\\text.txt");
FileWriter fw=new FileWriter(f);
BufferWriter bw=new BufferWriter(fw);
while((strTmp=br.readLine())!=null){
bw.writer(strTmp+"\n");
bw.flush();
}
bw.close();
fw.close();
br.close();
}
jdbc执行预处理,批处理,LOB字段处理,调用存储过程的更多相关文章
- ASP调用存储过程访问SQL Server
ASP调用存储过程访问SQL Server 2011-02-15 10:22:57 标签:asp 数据库 sQL 存储过程 Server ASP和存储过程(Stored Procedures)的文章 ...
- JDBC第二篇--【PreparedStatment、批处理、处理二进制、自动主键、调用存储过程、函数】
这是我JDBC的第一篇 http://blog.csdn.net/hon_3y/article/details/53535798 1.PreparedStatement对象 PreparedState ...
- JDBC【PreparedStatment、批处理、处理二进制、自动主键、调用存储过程、函数】
1.PreparedStatement对象 PreparedStatement对象继承Statement对象,它比Statement对象更强大,使用起来更简单 Statement对象编译SQL语句时, ...
- 【转载】JDBC操作LOB字段
转自:http://www.cnblogs.com/tengtao93/p/4984689.html 1.LOB(Large Objects)大对象,是用来存储大量的二进制和文本数据的一种数据类型(一 ...
- JDBC 学习笔记(二)—— 大数据+存储过程+批处理+事务管理
本文目录: 1.使用JDBC处理大数据 2.使用JDBC处理大文本 3.使用JDBC处理二进制数据 4.Oracle中大数据处理 5 ...
- Java数据库连接——JDBC调用存储过程,事务管理和高级应用
一.JDBC常用的API深入详解及存储过程的调用 相关链接:Jdbc调用存储过程 1.存储过程(Stored Procedure)的介绍 我们常用的操作数据库语言SQL语句在执行的时候需要先编译,然后 ...
- Java数据库连接--JDBC调用存储过程,事务管理和高级应用
相关链接:Jdbc调用存储过程 一.JDBC常用的API深入详解及存储过程的调用 1.存储过程的介绍 我们常用的操作数据库语言SQL语句在执行的时候要先进行编译,然后执行,而存储过程是在大型数据库系统 ...
- 【Java EE 学习 29 下】【JDBC编程中操作Oracle数据库】【调用存储过程的方法】
疑问:怎样判断存储过程执行之后返回值是否为空. 一.连接oracle数据库 1.需要的jar包:在安装的oracle中就有,所以不需要到官网下载,我的oracle11g下:D:\app\kdyzm\p ...
- varchar2_to_blob,应用向数据库更新LOB字段时的超时问题
将字符串转换为BLOB类型数据,写入服务器. 1,首先利用to_clob函数把varchar2字段转成 clob字段. 2 利用c2b上面函数将clob转成blob. 即: c2b(to_clob( ...
随机推荐
- Visual Studio(VS2012) Project&(Solution) 虚拟文件夹 & 物理文件夹
今天发生个怪事:在 Solution Explorer 中,x project 内建立文件夹(folder)时,同时在磁盘目录下也创建了同名的文件夹. 1, 原本:应该只是创建一个“虚拟文件夹”用来“ ...
- Mac系统下配置JDK环境变量
第一次用Mac做开发,在网上也搜索了一些环境变量配置的文章,在此总结一下以方便日后使用. 1.打开终端Terminal: 2.进入当前用户主目录,cd ~: 3.临时授权,sudo su: 4.输入密 ...
- 学习PHP 逛的几个网站。
PHP 第一社区 http://www.php1.cn/ 51cto php开发 http://developer.51cto.com/col/1441/ phphub https://phphub. ...
- hibernate一级缓存的源码初窥
hibernate的一级缓存的存在使得hibernate可以在操作实体化对象的时候减少对于数据库的访问.hibernate的一级缓存实际上就是指的session缓存,它的生命周期和session相同. ...
- 数据库查询优化-SQL优化
1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where num is null可以在num上设置默 ...
- SQL Server 2012 联机丛书离线安装
昨日根据微软官网的方式安装SQL Server 2012 联机丛书报错,无法安装: 联机丛书下载位置及安装方式: 按照给出的方式安装,无法完成,错误如下:
- VS2010+Qt+OpenCv(显示图像)
Qt在界面显示窗口中起着越来越重要的作用,从而了解了下如何在Qt中显示一副图像. 该小程序主要注意一下几点: 1.工程属性中设置OpenCV的环境(包含目录和库目录,以及附加依赖项),设置Qt的环境( ...
- 【crawler】log4j:WARN No appenders could be found for logger (dao.hsqlmanager).
This Short introduction to log4j guide is a little bit old but still valid. That guide will give you ...
- Python常用内置函数总结
一.数学相关 1.绝对值:abs(-1)2.最大最小值:max([1,2,3]).min([1,2,3])3.序列长度:len('abc').len([1,2,3]).len((1,2,3))4.取模 ...
- jQuery滚动数字
<ul class="dateList"> <li class="one"> <p class="titleName&q ...