我们常常在网页中需要处理一些图片或者其他的信息,而这些信息在数据库中的存放,对程序员来说,怎么存放可以不去理会,但我们从如何JDBCAPI层出发怎么去处理它们才是最重要的
我们处理大文本文件需要处理的一个存,一个取。
存:需要用到(statement或者preparedStatement)st.setCharacterStream(1, reader,(int)file.length());//设置存放的参数
取:Clob clob = rs.getClob(1);//获取Clob存储的值
reader = clob.getCharacterStream();//将他转换为字符流
具体实例:public class ClobTest {
public static void main(String[] args) throws Exception {
//create();
read();
}
static void create() throws Exception{
Connection conn = null;
PreparedStatement st = null;
try {
conn=JdbcUtils.getConnection();
String sql = "insert into user_test(big_text) values(?)";
st=conn.prepareStatement(sql);
File file=new File("src/cn/vickyi/jdbc/JdbcUtils.java");
Reader reader = new BufferedReader(new FileReader(file));
st.setCharacterStream(1, reader,(int)file.length());
//st.setAsciiStream(1, reader,(int)file.length());
int len=st.executeUpdate();
reader.close();
System.out.println(len);
} finally {
JdbcUtils.free(null, st, conn);
}
}
static void read() throws Exception{
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
Date birthday=null;
try {
conn=JdbcUtils.getConnection();
String sql = "select big_text from user_test";
//conn = JdbcUtils.getConnection();
st = conn.prepareStatement(sql);
rs = st.executeQuery();
Writer writer=null;
Reader reader=null;
while (rs.next()) {
Clob clob = rs.getClob(1);
reader = clob.getCharacterStream();
File file=new File("JdbcUtils_bak.java");
writer = new BufferedWriter(new FileWriter(file));
char[] buf=new char[1024];
int len;
while((len=reader.read(buf))>0){
writer.write(buf,0,len);
}
}
writer.close();
reader.close();
} finally {
JdbcUtils.free(rs, st, conn);
}
}
}
二进制文本的处理:
理论差不多,同样的一个存,一个取,分别用到的函数是:InputStream in = new BufferedInputStream(new FileInputStream(file));//读取流
st.setBinaryStream(1, in,(int)file.length());//设置Statement的?值
取:有二种方法
a、通过转换来取得:Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream();
b、直接取得:in=rs.getBinaryStream(1);//这是直接就获取,不需要取得Blob
具体实例:
package cn.vickyi.jdbc;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
public class Blobtest {
/** @param args
* @throws Exception */
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//create();
read();
}
static void create() throws Exception{
Connection conn = null;
PreparedStatement st = null;
try {
conn=JdbcUtils.getConnection();
String sql = "insert into Blod_test(big_bit) values(?)";
st=conn.prepareStatement(sql);
File file=new File("src/cn/vickyi/jdbc/2.jpg");
InputStream in = new BufferedInputStream(new FileInputStream(file));
st.setBinaryStream(1, in,(int)file.length());
int len=st.executeUpdate();
System.out.println(len);
in.close();
} finally {
JdbcUtils.free(null, st, conn);
}
}
static void read() throws Exception{
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
Date birthday=null;
try {
conn=JdbcUtils.getConnection();
String sql = "select big_bit from Blod_test";
//conn = JdbcUtils.getConnection();
st = conn.prepareStatement(sql);
rs = st.executeQuery();
//一般都要把列名写出来
while (rs.next()) {
Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream();
in=rs.getBinaryStream(1);//这是直接就获取,不需要取得Blob
File file=new File("1.jpg");
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] buf=new byte[1024];
int len;
while((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
}
} finally {
JdbcUtils.free(rs, st, conn);
}
}
}
javabean:
javabean中的对应的属性值和JSP中设置的属性值的名称一定要对称
Javabean具有良好的跨平台,通用性,易于不懂Java的程序员来读程序,简写代码
用来替代繁琐的servlet程序中的表达示值。