?? blobtest.java
字號:
package cn.itcast.jdbc;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BlobTest {
/**
* @param args
* @throws SQLException
* @throws IOException
*/
public static void main(String[] args) throws SQLException, IOException {
// add();
read();
System.out.println("end");
}
static void add() throws SQLException, FileNotFoundException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "insert into blob_test(btest) values(?)";
conn = JdbcUtils.getConnection();
File file = new File("F:\\Picture\\PConline12gundamjt37.jpg");
InputStream is = new BufferedInputStream(new FileInputStream(file));
ps = conn.prepareStatement(sql);
System.out.println(file.length());
ps.setBinaryStream(1, is, (int) file.length());
int i = ps.executeUpdate();
System.out.println("i=" + i);
} finally {
JdbcUtils.realse(rs, ps, conn);
}
}
static void read() throws SQLException, IOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "select btest from blob_test";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
OutputStream os = new BufferedOutputStream(
new FileOutputStream("pic.jpg"));
InputStream is = rs.getBinaryStream("btest");
byte[] buf = new byte[1024];
for (int i = 0; (i = is.read(buf)) > 0;) {
os.write(buf, 0, i);
}
os.close();
is.close();
}
} finally {
JdbcUtils.realse(rs, ps, conn);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -