都是一些很久前的笔记txt,废话不多说,直接上码。

[java]
public class TestClob {
private static void addClob() {
Connection conn;
try {
conn = JDBCUtil.getInstance().getConnection();
PreparedStatement ps = conn.prepareStatement( insert into mytest(id, context, feature) values(?,?,?) );
ps.setInt(1, 5);

InputStream input = new FileInputStream(new File( D:\\hello.txt ));
ps.setAsciiStream(2, input, new File( D:\\hello.txt ).length());

InputStream inputJPG = new FileInputStream(new File( D:\\samples\\v5.JPG ));
ps.setBinaryStream(3, inputJPG, new File( D:\\samples\\v5.JPG ).length());

ps.executeUpdate();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private static void getBook(int id)throws Exception{

Connection conn = JDBCUtil.getInstance().getConnection();
String sql = select * from mytest where id = ? ;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();

if(rs.next()){
Clob context = rs.getClob( context );
String str = context.getSubString(1, (int)context.length());
System.out.println(str);

Blob pic = rs.getBlob( feature );
FileOutputStream out = new FileOutputStream(new File( D:\\1.jpg ));
out.write(pic.getBytes(1, (int)pic.length()));
out.close();
}

if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
}
}

public static void main(String [] args) throws Exception{
//addClob();

getBook(5);
}

}
[/java]

JDBC插入Blob and Clob数据
Tagged on:         

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据