| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 871 人关注过本帖
标题:[求助]JDBC与BLOB结合的应用(很急)
只看楼主 加入收藏
gwlgqx
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-6-2
收藏
 问题点数:0 回复次数:3 
[求助]JDBC与BLOB结合的应用(很急)

[实验目的]

1. 了解BLOB与CLOB

2. 掌握BLOB的在数据库的存取

[实验内容]

1. 设计一个用于测试的数据表,其中包括image类型的字段

2. 编写一个程序实现把图片的往数据库中存储。

3. 编写一个程序实现把图片的从数据库中取出来,并显示在页面中。

4. 尝试修改Blob对象

搜索更多相关主题的帖子: BLOB left JDBC align 数据库 
2007-10-24 17:57
blackboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:34
专家分:0
注 册:2007-10-21
收藏
得分:0 

package com.city.oa.business.impl;
import java.sql.*;
import java.util.*;
import java.io.*;
import com.city.oa.factory.*;
import com.city.oa.value.*;

public class EmployeeImpl
{
public void add(String empNo,String password,String name) throws Exception
{
String sql="insert into Employee (EMPNO,PASSWORD,NAME) values (?,?,?)";
Connection cn=null;
try
{
cn=ConnectionFactory.getConnection();
PreparedStatement ps=cn.prepareStatement(sql);
ps.setString(1, empNo);
ps.setString(2, password);
ps.setString(3, name);
ps.executeUpdate();
ps.close();
}
catch(Exception e)
{
throw new Exception("增加员工错误:"+e.getMessage());
}
finally
{
if(cn!=null)
{
cn.close();
}
}

}
public List getAllList() throws Exception
{
List empList=new ArrayList();
String sql="select * from EMPLOYEE";
Connection cn=null;
try
{
cn=ConnectionFactory.getConnection();
PreparedStatement ps=cn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
EmployeeValue ev=new EmployeeValue();
ev.setEmpNo(rs.getString("EMPNO"));
ev.setPassword(rs.getString("PASSWORD"));
ev.setName(rs.getString("NAME"));
ev.setFileType(rs.getString("FILETYPE"));
empList.add(ev);
}
rs.close();
ps.close();
}
catch(Exception e)
{
throw new Exception("取得员工列表错误:"+e.getMessage());
}
finally
{
if(cn!=null)
{
cn.close();
}
}

return empList;
}

public EmployeeValue getEmployee(String empNo) throws Exception
{
EmployeeValue ev=null;
String sql="select * from Employee where EMPNO=?";
Connection cn=null;
try
{
cn=ConnectionFactory.getConnection();
PreparedStatement ps=cn.prepareStatement(sql);
ps.setString(1, empNo);

ResultSet rs=ps.executeQuery();
if(rs.next())
{
ev=new EmployeeValue();
ev.setEmpNo(rs.getString("EMPNO"));
ev.setPassword(rs.getString("PASSWORD"));
ev.setName(rs.getString("NAME"));
ev.setFileType(rs.getString("FILETYPE"));

}
rs.close();
ps.close();
}
catch(Exception e)
{
throw new Exception("取得指定员工信息方法错误:"+e.getMessage());
}

finally
{
if(cn!=null)
{
cn.close();
}
}

return ev;
}
public void savePhoto(String empNo,InputStream in,int fileSize,String fileType) throws Exception
{
String sql="update EMPLOYEE set Photo=?,FileType=? where empNo=?";
Connection cn=null;
try
{
cn=ConnectionFactory.getConnection();
PreparedStatement ps=cn.prepareStatement(sql);
ps.setBinaryStream(1, in, fileSize);
ps.setString(2, fileType);
ps.setString(3, empNo);
ps.executeUpdate();
ps.close();
}
catch(Exception e)
{
throw new Exception("保存照片方法错误:"+e.getMessage());
}
finally
{
if(cn!=null)
{
cn.close();
}
}

}

}

2007-10-25 17:02
blackboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:34
专家分:0
注 册:2007-10-21
收藏
得分:0 

package com.city.oa.servlet;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.city.oa.factory.ConnectionFactory;

public class ShowPhoto extends HttpServlet
{

/**
* Constructor of the object.
*/
public ShowPhoto() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {



String sql="select * from Employee where EMPNO=?";
String empNo=request.getParameter("empno");
Connection cn=null;
try
{
cn=ConnectionFactory.getConnection();
PreparedStatement ps=cn.prepareStatement(sql);
ps.setString(1, empNo);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
InputStream in=rs.getBinaryStream("photo");
String fileType=rs.getString("FileType");

response.setContentType(fileType);
OutputStream out=response.getOutputStream();

int len=0;
byte[] data=new byte[100];
while( (len=in.read(data))!=-1 )
{
out.write(data,0,len);
}

in.close();
out.flush();
out.close();

}
rs.close();
ps.close();

}
catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}
finally
{
if(cn!=null)
{
try{ cn.close();}catch(Exception e){}
}
}



}

2007-10-25 17:03
blackboy
Rank: 1
等 级:新手上路
威 望:1
帖 子:34
专家分:0
注 册:2007-10-21
收藏
得分:0 

package com.city.oa.value;

import java.io.Serializable;

public class EmployeeValue implements Serializable
{
private String empNo=null;
private String password=null;
private String name=null;
private String fileType=null;

public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}



}

2007-10-25 17:04
快速回复:[求助]JDBC与BLOB结合的应用(很急)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035018 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved