11级_Java_曹建波 9.07 JDBC编程将文本图片存入数据库并取出

JDBC将文本存入数据库并取出

package com.hbsi.demo;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.Reader;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import com.hbsi.dbUtil.DBManager;

public class Demo3 {

publicvoid insert() {

Connectioncon=null;

PreparedStatementst = null;

try{

con= DBManager.getConnection();

Stringsql = "insert into textclob(memo) values(?)";

st=con.prepareStatement(sql);

Filef=new File("src/com/hbsi/demo/Demo2.java");

BufferedReaderbr=new BufferedReader(new FileReader(f));

st.setCharacterStream(1,br,f.length());

inti = st.executeUpdate();

if(i > 0) {

System.out.println("插入成功");

}

}catch (Exception e) {

//TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBManager.closeDB(con,st, null);

}

}

publicvoid find() {

Connectioncon=null;

PreparedStatementst=null;

ResultSetrs=null;

try{

con=DBManager.getConnection();

Stringsql = "select memo from textclob where id = ?";

st=con.prepareStatement(sql);

st.setInt(1,1);

rs=st.executeQuery();

while(rs.next()){

Readerr=rs.getCharacterStream("memo");

BufferedReaderbr=new BufferedReader(r);

Stringline=null;

while((line=br.readLine())!=null){

System.out.println(line);

}

}

}catch(Exceptione){

e.printStackTrace();

}finally{

DBManager.closeDB(con,st, null);

}

}

/**

* @param args

*/

publicstatic void main(String[] args) {

Demo3d=new Demo3();

//d.insert();

d.find();

}

}

JDBC将图片存入数据库并取出

package com.hbsi.demo;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import com.hbsi.dbUtil.DBManager;

public class Demo4 {

publicvoid insert() {

Connectioncon=null;

PreparedStatementst = null;

try{

con= DBManager.getConnection();

Stringsql = "insert into textblob(image) values(?)";

st=con.prepareStatement(sql);

Filef=new File("src/com/hbsi/demo/gl.gif");

FileInputStreamfis=new FileInputStream(f);

st.setBinaryStream(1,fis,f.length());

inti = st.executeUpdate();

if(i > 0) {

System.out.println("插入成功");

}

}catch (Exception e) {

//TODO Auto-generated catch block

e.printStackTrace();

}finally {

DBManager.closeDB(con,st, null);

}

}

publicvoid find() {

Connectioncon=null;

PreparedStatementst=null;

ResultSetrs=null;

try{

con=DBManager.getConnection();

Stringsql = "select image from textblob where id = ?";

st=con.prepareStatement(sql);

st.setInt(1,1);

rs=st.executeQuery();

while(rs.next()){

InputStreamis=rs.getBinaryStream("image");

FileOutputStreamfos=new FileOutputStream("F:\\1.gif");

byte[]buf=new byte[1024];

intcount=0;

while((count=is.read(buf,0, 1024))!=-1){

fos.write(buf,0,count);

}

}

}catch(Exceptione){

e.printStackTrace();

}finally{

DBManager.closeDB(con,st, null);

}

}

/**

* @param args

*/

publicstatic void main(String[] args) {

Demo4d=new Demo4();

//d.insert();

d.find();

}

}

你可能感兴趣的:(11级_Java_曹建波 9.07 JDBC编程将文本图片存入数据库并取出)