都是一些很久前的笔记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:         

发表评论

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.