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( ...
随机推荐
- .Net环境下的缓存技术介绍
.Net环境下的缓存技术介绍 摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1 概念 1.1 ...
- Sublime Text 3插件安装
自动安装: 1.通过快捷键 ctrl+` 或者 View > Show Console 菜单打开控制台 2.粘贴对应版本的代码后回车安装 适用于 Sublime Text 3: import ...
- Sublime Text 配置记录
sublime userSetting sublime theme sublime plug sublime userSetting 对sublime的配置 { "color_scheme& ...
- CSS选择器--普通选择器
普通选择器: 1.标签选择器:使用标签选择器,所有的相同的标签都会被选中.(如:选择div所有的div都被选中.) 2.类选择器:如果一个元素设置了多个类选择器样式,那么这些类选择器都会被设置.但是如 ...
- html / css打印样式
最近做公司后台系统,需要打印贴箱标签,按照正常打印A4纸的标准,测试的效果不是自己想要的,文字排版布局都乱了,查了一些资料,需要设置的东西我总结了一下: 显示器(screen)和打印机(printer ...
- .net预览功能
1.把需要预览的文件格式转换为pdf 2.下载pdttoswf软件 3.实现预览swf功能 ok excel转pdf时会出现乱掉的问题.是excel文件的问题.调整excel文件的格式即可.
- 错误: java.lang.reflect.InvocationTargetException
错误: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(N ...
- a 标签中调用js的几种方法 文章摘自他人
我们常用的在a标签中有点击事件:1. a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题 ...
- Selenium VS QTP
Selenium系列 QTP 适用结构 B/S结构 C/S.B/S结构 适用人员 有一定代码基础(Java.C#.Python.Ruby) 对编程不是很熟悉的.厌烦了做手工功能测试的.想快速进 ...
- Create and Install Timer Job in MOSS 2007
Excute Timerjob public class TriggerLoadCacheTimerJob : SPJobDefinition { string ExceptionFlag = str ...