1、数据库中的表:
表名: FILE_ORIGENAL字段名称                    类型               用途
id                          int                序号
CREAT_DATE_TIME             datetime           记录文件存入数据库的时间
CONTENT                     binary?image      文件内容
注:1、文件内容不超过10k,是类似于文本的文件,但是后缀不是.txt,而是自定义了一个后缀。
    2、使用的是Microsoft SQL Server 2000数据库,数据库连接什么的都已经实现了。
    我的问题是:
1、用什么字段存储这个文件?Oracle中用的是blob类型,MS sql server中用什么类型?image?text?binary?
2、如何实现将文件存入数据库这个过程?
最好有个实例!谢谢!

解决方案 »

  1.   

    在java中没这样存储过, 只有在c#中这样搞过.                  实在对不起,具体怎么实现忘了.    几年前的事了..  呵呵
      

  2.   

    用image就可以,就当成二进制数据存,//这是我存取图片的代码:
    public static void printBinary(Connection conn)throws Exception{
      Statement stmt=conn.createStatement();
      ResultSet rs=stmt.executeQuery("select * from binaryTest");
      while(rs.next()){
       System.out.println(rs.getInt("id"));
       InputStream in=rs.getBinaryStream("binary");
       URL url=SQLTest.class.getResource("aa.txt");
       String path=url.getFile();
       File file=new File(path.substring(0,path.lastIndexOf("/"))+"/copy.JPG");
       OutputStream out=new FileOutputStream(file);
       byte buff[]=new byte[1024];
       for(int i=0;(i=in.read(buff))>0;){
        out.write(buff,0,i);
       }
       out.close();
       in.close();
      }
      rs.close();
      stmt.close();
     }
     public static void insertBinary(Connection conn)throws Exception{
      String sql="insert into binaryTest(binary) values(?)";
      PreparedStatement ps=conn.prepareStatement(sql);
      InputStream in=SQLTest.class.getResourceAsStream("动态代理的工作原理图.JPG");
      ps.setBinaryStream(1, in,in.available());
      ps.executeUpdate();
            in.close();
      ps.close();
     } 
      

  3.   

    关键是你的文件类型固定吗,是二进制,还是ascii文件呢
      

  4.   

    列类型:还是BlOB(通用)import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;public class C{
    private String username;
    private String password;
    private String url;
    private Connection conn;
    private PreparedStatement pst;
    private FileInputStream read;
    public C(){
    username="root";
    password="123456";
    url="jdbc:mysql://localhost;database=mydb";//mydb为库名
    String sql="insert into FILE_ORIGENAL(create_date_time,content) values(?,?)";
    try {
    //加载驱动
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    //获取连接对象
    conn=DriverManager.getConnection(url, username, password);
    pst=conn.prepareStatement(sql);
    pst.setString(1, "2010-11-3");
    File f=new File("c:/sanguo.txt");//文件格式没有要求,只要读出时还是相同格式就行了
    read=new FileInputStream(f);
    pst.setBinaryStream(2, read, f.length());//将第二个设置为该文件流
    pst.executeUpdate();

    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }finally{
    if(pst!=null)
    try {
    pst.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    if(read!=null)
    try {
    read.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    if(conn!=null)
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    public static void main(String[] args) {
    new C();
    }
    }
      

  5.   

    列类型:还是BlOB(通用)import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;public class C{
    private String username;
    private String password;
    private String url;
    private Connection conn;
    private PreparedStatement pst;
    private FileInputStream read;
    public C(){
    username="root";
    password="123456";
    url="jdbc:mysql://localhost;database=mydb";//mydb为库名
    String sql="insert into FILE_ORIGENAL(create_date_time,content) values(?,?)";
    try {
    //加载驱动
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    //获取连接对象
    conn=DriverManager.getConnection(url, username, password);
    pst=conn.prepareStatement(sql);
    pst.setString(1, "2010-11-3");
    File f=new File("c:/sanguo.txt");//文件格式没有要求,只要读出时还是相同格式就行了
    read=new FileInputStream(f);
    pst.setBinaryStream(2, read, f.length());//将第二个设置为该文件流
    pst.executeUpdate();

    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }finally{
    if(pst!=null)
    try {
    pst.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    if(read!=null)
    try {
    read.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    if(conn!=null)
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    public static void main(String[] args) {
    new C();
    }
    }
      

  6.   


    您好,我看了您给我写的 代码。有一个问题,就是,MS SQL server中没有BLOB字段,所以,pst.setBinaryStream(2, read, f.length());//将第二个设置为该文件流,这一句有如下异常“Exception in thread "main" java.lang.AbstractMethodError: com.microsoft.jdbc.base.BasePreparedStatement.setBinaryStrea(ILjava/io/InputStream;J)V
            at sqlservertest.txt.SaveTXT2.<init>(SaveTXT2.java:34)
            at sqlservertest.txt.SaveTXT2.main(SaveTXT2.java:65)
    Java Result: 1
      

  7.   


     try
       {
         InputStream is=new FileInputStream(new File("e:/aa.gif"));     PreparedStatement pstmt=conn.prepareStatement("insert stu Values(10,'lily',20,?)");
         pstmt.setBinaryStream(1,is,is.available());
         pstmt.executeUpdate();     pstmt.close();
         is.close();
       }
       catch(Exception ep){
         ep.printStackTrace();
       }这是我在网上找的,他用的是Image类型的,不过看样子操作起来一样,试了下可以,你也试下吧