用struts从页面获取到了上传的FormFile对象。问题就是我就拿着这个FormFile对象怎么像数据库中存储?数据库的file字段是blob类型,oracle数据库。最好有代码以及注释。多谢大家
解决方案 »
- Spring Hibernate save无法插入到数据库中(上网搜过了,但是没有找到解决的办法)
- getHibernateTemplate 调用 MYSQL存储过程返回查询结果
- 帮忙看个hibernate的错No row with the given identifier exists: [com.ltd.blog.bean.ArticleBean#1]
- J2EE应用快速开发暨自动代码生成器实现详解
- 最后10分了,有谁用过Google Web Tookit,给讲一讲优点和缺点吧!
- 急 代码出错~~~~
- spring问题求助
- java中用JDOM操作xml文件的问题
- 怎样解决ORALCE中分页时,用count(*)来取得总记录数所耗时间过长得问题。
- 还是乱码问题!!!100分送上
- java.lang.NoClassDefFoundError 求救!!
- [API的设计]有用过facebook API(或类似的)做过开发的吗?
void setBlob(int parameterIndex,Blob x)throws SQLException
你应该将从formfile上将byte数组得到,你可以借助一下ByteArrayOutputStream.toByteArray()之类的方法简单测试一下
然后通过oracle的oracle.sql.BLOB.getEmptyBLOB().setBytes(bytes)构建一个的blob.
然后ps.setBlob(xxx, blob)就可以了。如果oracle版本较低,你需要先构建一个空blob插入数据库,在更新一次。
如果你使用spring的话,那就非常简单了。自己查一下。到处都是。
/**
* 保存Clob数据
*
* @param name
* 数据名称
* @param data
* 字串数据
*/
@SuppressWarnings("deprecation")
public static Integer saveClobDataUseSQL(String name, String data) {
Connection conn = getConnection();
Integer id = (int) (Math.random() * 100000);
StringBuilder sqlBuilder = new StringBuilder();
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
sqlBuilder.append("insert into TEST_CLOB(ID, NAME, CONTENT) ");
sqlBuilder.append("values ( " + id);
sqlBuilder.append(",'" + name + "'");
sqlBuilder.append(", empty_clob()) ");// 插入一个空对象empty_clob()
stmt.executeUpdate(sqlBuilder.toString());
// 锁定数据行进行更新,注意“for update”语句
String sqlUpd = "select CONTENT from TEST_CLOB where ID = " + id
+ " for update";
ResultSet rs = stmt.executeQuery(sqlUpd);
if (rs.next()) {
// 得到java.sql.Clob对象后强制转换为oracle.sql.CLOB
CLOB clob = (CLOB) rs.getClob("CONTENT");
Writer outStream = clob.setCharacterStream(0L);
// data是传入的字符串,定义:String data
char[] c = data.toCharArray();
outStream.write(c, 0, c.length);
outStream.flush();
outStream.close();
conn.commit();
stmt.close();
}
} catch (Exception e) {
try {
conn.rollback();
id = null;
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return id;
}
clob下面是clob的例子.需要导入oracle.jdbc.OracleResultSetsql = "select A from xxxtable where xxx=xxxfor update"
ResultSet rs_clob = st.executeQuery(sql)java.sql.Clob clob;
int i = 0;
while (rs_clob.next()) {
clob = ((oracle.jdbc.OracleResultSet) rs_clob)
.getClob("Bo");
oracle.sql.CLOB my_clob = (oracle.sql.CLOB) clob; Writer writer = my_clob.getCharacterOutputStream();
writer.write(str);
writer.flush();
writer.close(); }
rs_clob.close();
http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/clob10g/handlingclobsinoraclejdbc10g.html
oracle 官方文件 LOB sample
http://www.oracle.com/technology/sample_code/tech/java/sqlj_jdbc/files/advanced/LOBSample/LOBSample.java.html
package com.convert.mysql2oracle;import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;import oracle.jdbc.driver.OracleConnection;
import oracle.sql.CLOB;public class Mto { /**
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
Connection con;
PreparedStatement pstmt;
try {
Mysql mysql = new Mysql();
List<News> list = mysql.getlist("mysql");
Class.forName(driver);
con = DriverManager
.getConnection("jdbc:oracle:thin:name/[email protected]:1521:oradb");
pstmt = con
.prepareStatement("insert into table (id,title, content, description, inputtime, updatetime, catid,type) values(id.nextval,?,?,?,?,?,?,1)");
con.setAutoCommit(false);
int i = 1;
for (News news : list) {
int num=i++;
CLOB clob = new CLOB((OracleConnection) con);
clob = oracle.sql.CLOB.createTemporary((OracleConnection) con,
true, 1);
clob.putString(1, news.getContent());
pstmt.setClob(2, clob);
pstmt.setString(1, news.getTitle());
pstmt.setString(3, news.getDescription());
pstmt.setDate(4, (Date) news.getInputtime());
pstmt.setDate(5, (Date) news.getUpdatetime());
pstmt.setInt(6, news.getCatid());
pstmt.executeUpdate();
System.out.println(num+"oracle插入完畢");
}
con.commit();
System.out.println("--------oracle確認完畢--------");
pstmt.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }}