您的位置:首页 > 数据库

对特种类型数据在数据库中的读取和存储

2009-09-15 15:06 453 查看
今天需要完成一个功能将一个excel文件存入sql数据库 查阅无数资料 没有直接的方法

不过却有不少针对图片的操作 是将图片转换成二进制数据 然后存储进数据库

所谓数据的类型只是针对使用者而言的,数据在内存中只有一种格式 就是二进制格式 存储应该也差不多 我将一个文件转换成二进制存储进数据库 等需要的时候 再用二进制读出来 恢复原来的格式 不就可以了么

以下是实现:

package FileUtil;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class InsertFile {
private String filename ;
private File file;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public static void main(String s[]){
File   file   =   new   File("c://test//扬帆教学管理系统 v1.0 + 源码.rar")   ;
new InsertFile().writeFile(file);
String   sql   =   "select excel FROM excel";
String filename = "c://test1//扬帆教学管理系统 v1.0 + 源码.rar";
new InsertFile().readFile(sql,filename);
}
public void writeFile(File file){
Connection conn =null;
try{
Class.forName(MvcProperties.dbDriverName);
conn=DriverManager.getConnection(MvcProperties.url,MvcProperties.user,MvcProperties.password);
String   sql   =   "Insert into excel (excel) values (?)";
PreparedStatement   pstmt   =   conn.prepareStatement(sql)   ;
FileInputStream   fis   =   new   FileInputStream(file);
pstmt.setBinaryStream(1,   fis,   (int)file.length());
pstmt.executeUpdate();
pstmt.close();
fis.close();
if(conn!=null && !conn.isClosed() ){
conn.close();
System.out.println("写入"+file.getName()+"成功~!");

}
}catch(ClassNotFoundException e){
System.out.println("文件未找到!");
e.printStackTrace();
} catch(SQLException se){
System.out.println("SQL语句异常");
se.printStackTrace();
}catch(FileNotFoundException fne){
System.out.println("文件未找到");
fne.printStackTrace();
}catch(IOException ioe){
System.out.println("io异常");
ioe.printStackTrace();
}finally{
try{
if(conn!=null && !conn.isClosed() ){
conn.close();

}
}catch(Exception e){
e.printStackTrace();
}
}

}
public void readFile(String sql,String filename){
Connection conn =null;
try{
Class.forName(MvcProperties.dbDriverName);
conn=DriverManager.getConnection(MvcProperties.url,MvcProperties.user,MvcProperties.password);
Statement stmt = conn.createStatement();
ResultSet rs;
String strSql  = "select excel FROM excel";
InputStream   in   =   null;
rs=stmt.executeQuery(strSql);
while(rs.next()){
//Windows 系统下存储路径用2个反斜杠
DataOutputStream sos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
//读出流用getBinaryStream()方法。
in = rs.getBinaryStream("excel");
//用缓存数组逐渐输出流
int len = 0; byte[] b = new byte[1024];
while ((len = in.read(b)) != -1) {
sos.write(b,0,len);
}
sos.close();
in.close();
}
rs.close();
if(conn!=null && !conn.isClosed()){
conn.close();
System.out.println("读出到"+filename+"成功~!");
}
}catch(ClassNotFoundException e){
System.out.println("文件未找到!");
e.printStackTrace();
} catch(SQLException se){
System.out.println("SQL语句异常");
se.printStackTrace();
}catch(FileNotFoundException fne){
System.out.println("文件未找到");
fne.printStackTrace();
}catch(IOException ioe){
System.out.println("io异常");
ioe.printStackTrace();
}finally{
try{
if(conn!=null && !conn.isClosed() ){
conn.close();

}
}catch(Exception e){
e.printStackTrace();
}
}

}
}


代码明显有很多问题 不过 暂时只能以完成功能为本 。

欢迎拍砖 两个问题需要解决
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐