(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();
--------------------------------------------------------------------------------------------