File file = new File("E:\\outBulletinData.txt");
  try{            String sql = "insert into FrameResource(Content) values(?)";
            pstmt = c.prepareStatement(sql);
            //从文件中读取Content内容 
            InputStream is = new FileInputStream(file);//创建输入流,将外部文件输入到InputStream               byte[] textByte;
            textByte = new byte[is.available()];
            is.read(textByte);
            is.close();
            pstmt.setBinaryStream(1,(new ByteArrayInputStream(textByte)),textByte.length);//将文件流插入到数据库中
            pstmt.executeUpdate();
    } catch (Exception ee) {
            System.out.println(ee.getMessage());
            return -1;
     } finally {
            
        }以上是部分代码。数据库的联接都没有问题。文件的内容插不到sqlserver中。以上代码错在了哪儿,请各路好汉指教,不胜感激。

解决方案 »

  1.   

    pstmt.setBinaryStream(1,(new ByteArrayInputStream(textByte)),textByte.length);//将文件流插入到数据库中 
    这个就是给问好赋值啊.
      

  2.   

    没报错?你的connnect前面设置了自动commit为否?
      

  3.   

    PowerAMD3794,
    你好。你是说把插入的内容区出来看看吗?
    com.microsoft.jdbc.base.BaseInputStreamWrapper@1f12c4ezhe这是从Content字段取出的内容。而E:\\outBulletinData.txt文件中有几千个字符的啊。
      

  4.   

    没有报错啊。我用的NeatBean。
      

  5.   

    表面上看都没有错 那就说明是细节错误或者是逻辑错误,楼主把错误信息贴出来吧,还有就是,用debug调试一下,看看一些关键变量(比如file sql语句 is等等)的值是不是期望的值
      

  6.   

    你的那个C是怎么来的,怎么close的帖完整的代码
      

  7.   

    pstmt.setBinaryStream(1,(new ByteArrayInputStream(textByte)),textByte.length);
    换成下边这样看看。。
    pstmt.setBinaryStream(1,is,textByte.length);
      

  8.   

     public int impSqlFram(Bulletin bulletin_obj) {        String bulletintest = getDataBulletintext(bulletin_obj);
            int datasize = bulletintest.length();
            
            Timestamp time = bulletin_obj.getF_begintime();
            String currentTime = FormatDate.formatDate1(time);
            String year = FormatDate.formatDate2(time);
            String month = FormatDate.formatDate3(time);
             String day= FormatDate.formatDate4(time);
            String location = "/Data_YZGT/FileBase/LandMarket/"+currentTime+"/"+System.currentTimeMillis()+".html";//
            String FileName = year+month+day+".HTML";
            
            String ContentType = "text/html";
            String FileType = "HTML";
            String Title = "LandMarketNotic";
            String ClassID = "InfoBase";//        String Re = "LandMarketNotic";
            int SaveMode = 1;
            
            int FileSize = datasize;
            
            int ModifyPersonID = 0;
            int IsTemp = 0;
            ConnectionManager mgr = new ConnectionManager();
            Connection c = mgr.getSqlConnection();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            File file = new File("E:\\outBulletinData.txt");
            try {
                //Content,
                String sql = "insert into FrameResource(Location,ContentType,FileName,FileType,FileSize,Title,Content,SaveMode,ClassID,ModifyPersonID,Re,IsTemp) values(?,?,?,?,?,?,?,?,?,?,?,?)";
                pstmt = c.prepareStatement(sql);
                pstmt.setString(1, location);
                pstmt.setString(2, ContentType);
                pstmt.setString(3, FileName);
                pstmt.setString(4, FileType);
                pstmt.setInt(5, FileSize);
                pstmt.setString(6, Title);
                //从文件中读取Content内容 
                InputStream is = new FileInputStream(file);//创建输入流,将外部文件输入到InputStream              
                byte[] textByte;
                textByte = new byte[is.available()];
                is.read(textByte);
               is.close();
               pstmt.setBinaryStream(7,(new ByteArrayInputStream(textByte)),textByte.length);//将文件流插入到数据库中
               
                pstmt.setInt(8, SaveMode);
                pstmt.setString(9, ClassID);
                pstmt.setInt(10, ModifyPersonID);
                pstmt.setString(11, Re);
                pstmt.setInt(12, IsTemp);
                pstmt.executeUpdate();
             
                String sqlurl = "select max(id) as id  from frameresource ";
                pstmt = c.prepareStatement(sqlurl);
                rs = pstmt.executeQuery();
                int maxid1 = 0;
                if (rs.next()) {
                    maxid1 = rs.getInt("id");
                }
                return maxid1;
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
                return -1;
            } finally {
                
            }
        }
    以上是该方法的全部代码。
    以下是调试结果
      

  9.   

    sql         String               "insert into FrameResource....."
    is          FileInputStreame      #862
    textByte    byte[]               #865(length=2154)
    以上是几个变量的调试结果。