--------写入Oracle的图片
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.sql.BLOB;
public class JdbcDemo {
public static void main(String[] args) {
JdbcDemo obj=new JdbcDemo();
obj.append_pic_to_Oracle();
}
private void append_pic_to_Oracle()
{
Connection conn=null;
BLOB blob=null;
int foodid=84;
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@10.18.45.17:1521:orcl";
conn=DriverManager.getConnection(url,"wangjun","wangjun");
conn.setAutoCommit(false); //把默认提交关闭,即开启了JDBC连接事务
//seq_foodinfo 是一个序列
String sql="insert into foodinfo values(seq_foodinfo.nextval,?,?,?,empty_blob(),?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, "拉面");
pstmt.setString(2,"原料:青菜、猪肉、海带、骨头口味");
pstmt.setDouble(3, 4);
pstmt.setString(4, "好吃");
//没有返回值就用update
pstmt.executeUpdate();
pstmt.close();
strsql="select image from foodinfo where foodid=? for update"; //锁定即将要更新的记录
pstmt=conn.prepareStatement(strsql);
pstmt.setInt(1, foodid);
ResultSet rs= pstmt.executeQuery();
if(rs.next())
{
//当前找到blob
blob= (BLOB)rs.getBlob(1);
}
File f=new File("d:/funny/images/500024.jpg");
FileInputStream fis=null;
if(f.exists())
{
fis=new FileInputStream(f);
}
strsql="update foodinfo set image=? where foodid=?";
pstmt=conn.prepareStatement(strsql);
OutputStream os=blob.getBinaryOutputStream();
//为图片开辟一条缓存空间
byte[] buffer=new byte[(int)f.length()];
fis.read(buffer);
os.write(buffer);
fis.close();
os.close();
pstmt.setBlob(1, blob);
pstmt.setInt(2, foodid);
pstmt.executeUpdate();
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try {
//连接不等于空或者没有关闭的时候
if(conn!=null || !conn.isClosed())
{
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
------------------------------------------------------------------------------
----------------在网页中输入图片
先建一个数据库的公用类 DBSession.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBSession {
private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
private static final String URL="jdbc:oracle:thin:@10.18.45.17:1521:orcl";
private static final String USERNAME="wangjun";
private static final String PWD="wangjun";
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,USERNAME,PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeConn(Connection conn)
{
try {
if(conn!=null || !conn.isClosed())
{
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
在写一个取出图片的servlet 类
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.webapp.comm.DBSession;
public class GetPicture extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String foodid =request.getParameter("foodid");
response.setContentType("image/jpg");
Connection conn=DBSession.getConnection();
String strsql="select image from foodinfo where foodid=?";
try {
PreparedStatement pstmt=conn.prepareStatement(strsql);
pstmt.setInt(1,Integer.parseInt(foodid));
ResultSet rs=pstmt.executeQuery();
if(rs.next())
{
Blob blob= rs.getBlob(1);
InputStream is= blob.getBinaryStream();
OutputStream os= response.getOutputStream();
byte[] buffer=new byte[1024*4];
int sss=0;
while((sss=is.read(buffer))!=-1)
{
os.write(buffer,0,sss);
}
is.close();
os.flush();
os.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
finally
{
DBSession.closeConn(conn);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
在写一个jsp代码 能去取出代码
<html>
<head>
<title></title>
</head>
<body>
<img src="getPic?foodid=${param.foodid}" >
</body>
</html>