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中。以上代码错在了哪儿,请各路好汉指教,不胜感激。
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中。以上代码错在了哪儿,请各路好汉指教,不胜感激。
这个就是给问好赋值啊.
你好。你是说把插入的内容区出来看看吗?
com.microsoft.jdbc.base.BaseInputStreamWrapper@1f12c4ezhe这是从Content字段取出的内容。而E:\\outBulletinData.txt文件中有几千个字符的啊。
换成下边这样看看。。
pstmt.setBinaryStream(1,is,textByte.length);
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 {
}
}
以上是该方法的全部代码。
以下是调试结果
is FileInputStreame #862
textByte byte[] #865(length=2154)
以上是几个变量的调试结果。