百分求解~!!十万火急

weizhihui 2004-03-30 10:14:25
背景:
我是即将毕业的学生,现在有一个老板,他说让我解决一个问题就要我,时间很短,我查了几天的资料。没有查到, 先求助于各位, 现行谢过了

问题:
怎样把一个图片用java类读取到数据库?
...全文
20 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
weizhihui 2004-04-06
  • 打赏
  • 举报
回复
谢谢华哥
boy 2004-03-30
  • 打赏
  • 举报
回复
我可以给你一个反过来的,要不要?
luojx 2004-03-30
  • 打赏
  • 举报
回复
先连接数据库,
用io包将文件读成文件流,
将文件流存进数据库(我以前用的是SQL SERVER)就行了,以前就这么做的
boy 2004-03-30
  • 打赏
  • 举报
回复
odbc.ini的内容
jdbc:odbc:testOle
test
test
boy 2004-03-30
  • 打赏
  • 举报
回复
import java.io.*;
import java.sql.*;

public class GetTif
{
String Url="jdbc:odbc:Test",JdbcUser="system",JdbcPwd="manager";

public static BufferedReader getBufferedReader(String fName)
{
try{
FileReader fR=new FileReader(fName);
if(fR==null)return null;
return new BufferedReader(fR);
}catch(Exception ex)
{ex.printStackTrace();return null;}
}

//存储过度文件
private void saveFile(String DocID,InputStream inole)
{
try{
byte[] OleByte = getOle(inole);
FileOutputStream f = new FileOutputStream("d:\\documents\\GetOLE\\"+DocID+".Tif");
f.write(OleByte);
} catch(Exception e) {
e.printStackTrace();
}
}

//读二进制数据
private static byte[] getOle(InputStream s)
{
byte[] buffer = new byte[0];
byte[] chunk = new byte[4096];
int count;
try{
while((count = s.read(chunk))>=0)
{
byte[] t = new byte[buffer.length+count];
System.arraycopy(buffer,0,t,0,buffer.length);
System.arraycopy(chunk,0,t,buffer.length,count);
buffer=t;
}
}catch(Exception e){}
return buffer;
}

//初始化配置信息
public GetTif(BufferedReader brin)
{
if(brin==null)return;
try{
this.Url=brin.readLine().trim();
this.JdbcUser = brin.readLine().trim();
this.JdbcPwd = brin.readLine().trim();
brin.close();
}catch(IOException ex)
{System.err.println("FileIOError:"+ex);}
}

//读数据库
private void readDB()
{
//注册数据库驱动程序
try{
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
}
catch(Exception e)
{System.err.println("注册数据库驱动程序错误:" + e);}
//从数据库读OLE或附件
try{
Connection con = DriverManager.getConnection(Url,JdbcUser,JdbcPwd);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM image_files");
java.math.BigDecimal DocNo; InputStream inTif;
while(rs.next()){
try{
DocNo = rs.getBigDecimal("NO",0);
inTif = rs.getBinaryStream("TIF");
saveFile(DocNo.toString(),inTif);
System.out.println("存储过度Tif文件:"+DocNo.toString());
}
catch(Exception e)
{System.err.println("存储过度文件错误:" + e);}
}
st.close();
con.close();
}
catch(Exception e)
{System.err.println("数据库查询错误:" + e);}
}

public static void main(String args[])
{
GetTif gt = new GetTif(getBufferedReader("d:\\documents\\GetOLE\\class\\odbc.ini"));
gt.readDB();
}
}
weizhihui 2004-03-30
  • 打赏
  • 举报
回复
tx18(华哥)

我可以给你一个反过来的,要不要?

好啊!请你把他写下来好了 谢谢
kewin 2004-03-30
  • 打赏
  • 举报
回复
没用过
fantasyCoder 2004-03-30
  • 打赏
  • 举报
回复
1楼的方法可行!!!

恭喜你找到工作!!!

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧