怎么用java将C盘的一个文件直接写入到sqlserver数据库中?

解决方案 »

  1.   

    我看还是将文件路径写入sqlserver,再按路径找文件吧
      

  2.   

    支持 sqlserver blob 么?反正你碰到 二进制格式的字段可以尝试  rs.getInputStream & rs.getOutputStream ,
    然后与得到 I/O 流发生关系就是了。
      

  3.   

    楼上的说得对,设成BLOB字段,然后写一段程序,专门更新Blob字段,
    网上有资料,找找看
      

  4.   

    连接什么的随便
              java.io.FileOutputStream out = new java.io.FileOutputStream("c:\a.txt");
              PreparedStatement ps = m_conn.prepareStatement(
                  "select 字段 from 表 ");
              ResultSet res1 = ps.executeQuery();
              if (res1 != null) {
                if (res1.next()) {
                  Blob bl = res1.getBlob("字段");
                  if (bl != null) {
                    java.io.InputStream is = bl.getBinaryStream();
                    int m = is.available();
                    byte b[] = new byte[m];
                    is.read(b, 0, m);
                    out.write(b);
                    out.close();
                    file = null;
                    is.close();
     
                  }
                }
              }
      

  5.   

    angel7532(卡卡西) 讲的是从数据库到文件
     搂主要的是反过来吧
     我也在做这块,还没弄通,不过可以大家讨论一下,用的binary    String sql2="select sr_filecontent from tb_ServRequest "+where+" for update";    try {
          Statement stmt = conn.createStatement();
          ResultSet rs = stmt.executeQuery(sql2);      if (rs.next()) {
            try {
              FileInputStream fis = new FileInputStream(file);
              rs.updateBinaryStream(1,fis,(int)file.length());
              System.out.println("File2DB:"+file.getPath());
            }
            catch (Exception ex1) {
              ex1.printStackTrace();
            }      }
          if (stmt != null)
            stmt.close();
        }
        catch (SQLException ex) {      System.out.println(ex.getMessage());
          ex.printStackTrace();
        }
        finally {
          DBManager.closeConnection(conn);
        }
      

  6.   

    是不是一定要用m$的jdbc包,不能用odbc阿
      

  7.   

    太久不写忘记了,要这样
    conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    才能用rs.updateXXXX      
     try {
          Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
          stmt.execute(sql);
          ResultSet rs = stmt.executeQuery(sql2);      if (rs.next()) {
            try {  
              FileInputStream fis = new FileInputStream(file);
              rs.updateBinaryStream(1,fis,(int)file.length());
              System.out.println("File2DB:"+file.getPath());
            }
            catch (Exception ex1) {
              ex1.printStackTrace();
            }      }
          if (stmt != null)
            stmt.close();
        }
        catch (SQLException ex) {      System.out.println(ex.getMessage());
          ex.printStackTrace();
        }
        finally {
          DBManager.closeConnection(conn);
        }
      

  8.   

    stmt.close();之前要rs.updateRow();
    怎么像拉稀,弄不干净阿