版權聲明:尊重部落客原創文章,轉載請注明出處哦~http://blog.csdn.net/eson_15/article/details/51317001
上一節主要講了jdbc的基本操作,這一節主要總結一下jdbc如何處理大文本、如何處理圖檔以及進行批處理。
string path = demo1.class.getclassloader().getresource("1.txt").getpath(); //擷取指定檔案的path
file file = new file(path); //以這個path為參數建構一個file對象
reader reader = new filereader(file); //以這個file對象為參數建構reader流,這個流與這個檔案就關聯了
下面我們來看一下如何向資料庫中存入大文本以及如何從資料庫中取出大文本。先建立一個資料庫:
create database tes<span style="font-family:microsoft yahei;">t</span>;
use test;
create table testclob
(
id int primary key auto_increment,
resume text
);
下面看demo1.java:
public class demo1 {
@test//向資料庫中插入一個文本
public void add() throws filenotfoundexception {
connection conn = null;
preparedstatement st = null;
resultset rs = null;
try {
conn = jdbcutils.getconnection(); //使用上一節中的jdbcutils
string sql = "insert into testclob(resume) values(?)";
st = conn.preparestatement(sql); //預處理sql語句
string path = demo1.class.getclassloader().getresource("1.txt").getpath();//在工程src目錄下存放一個1.txt檔案
file file = new file(path);
st.setcharacterstream(1, new filereader(file), file.length());
int num = st.executeupdate(); //執行向資料庫中插入
if(num > 0) {
system.out.println("插入成功");
}
} catch (sqlexception e) {
e.printstacktrace();
} finally {
jdbcutils.release(conn, st, rs);
}
}
@test //從資料庫中讀取文本
public void read() throws ioexception {
conn = jdbcutils.getconnection();
string sql = "select resume from testclob where id=?";
st = conn.preparestatement(sql);
st.setint(1, 1);
rs = st.executequery(); //執行sql語句
if(rs.next()){
reader reader = rs.getcharacterstream("resume"); //擷取字段未resume的項,也就是我們剛剛存到資料庫的1.txt檔案
char buffer[] = new char[1024];
int len = 0;
filewriter out = new filewriter("d:\\1.txt"); //寫到d盤下
while((len = reader.read(buffer)) > 0){
out.write(buffer, 0, len);
}
out.close();
reader.close();
}
}
以上就是處理大文本的方法,比較簡單,下面我們來看看如何處理圖檔。
在mysql中,大文本是image類型,我們現在資料庫中建立新的表:
create table testblob
(
id int primary key auto_increment,
image longblob
);
然後我們直接看處理圖檔的java程式demo2.java:
public class demo2 {
@test //向資料庫中寫入圖檔
string sql = "insert into testblob(image) values(?)";
string path = demo2.class.getclassloader().getresource("honey.jpg").getpath(); //圖檔存在src目錄下
st.setbinarystream(1, new fileinputstream(path), new file(path).length());//這裡用的是setbinarystream,位元組流
//這種方法也行,fileinputstream方法重載了,既可以傳遞路徑,也可以傳遞具體檔案
//st.setbinarystream(1, new fileinputstream(new file(path)), new file(path).length());
int num = st.executeupdate();
/*這裡會有這個錯誤:
* packet for query is too large (4531349 > 1048576).
* you can change this value on the server by setting the max_allowed_packet' variable.
* 原因:mysql的一個系統參數:max_allowed_packet,其預設值為1048576(1m),即允許傳遞的最大packet為1m,如果照片超過1m無法導入
* 查詢:show variables like '%max_allowed_packet%';修改此變量的值:set global max_allowed_packet = 1024*1024*10;(10m)
* */
if(num > 0){
@test //從資料庫中讀取圖檔
string sql = "select image from testblob where id=?";
rs = st.executequery();
while(rs.next()){
inputstream in = rs.getbinarystream("image");//擷取用getbinarystream,也是位元組流
byte buffer[] = new byte[1024];
fileoutputstream out = new fileoutputstream("d:\\honey.jpg"); //寫到d盤下
while((len = in.read(buffer)) > 0){
in.close();
jdbc操作圖檔也比較簡單,跟操作大文本差不多。下面看看jdbc如何進行批處理。
首先我們還是先建立一個表:
create table testbatch
id int primary key,
name varchar(20)
然後我們看看jdbc進行批處理的具體代碼:
//jdbc進行批處理
public class demo3 {
@test
public void testbatch() {
statement st = null;
string sql1 = "insert into testbatch(id,name) values(1,'aaa')";
string sql2 = "insert into testbatch(id,name) values(2,'bbb')";
string sql3 = "insert into testbatch(id,name) values(3,'ccc')";
string sql4 = "delete from testbatch where id=1";
st = conn.createstatement();
st.addbatch(sql1);
st.addbatch(sql2);
st.addbatch(sql3);
st.addbatch(sql4); //将四條sql語句加入batch
st.executebatch(); //然後依次執行這四條sql語句
st.clearbatch();//執行完後清除batch
} catch (exception e) {
@test //大量插入
public void testbatch2() {
conn = j<span style="font-family:microsoft yahei;">dbc</span>utils.getconnection();
string sql = "insert into testbatch(id,name) values(?,?)";
for(int i = 0; i < 10000000; i++){
st.setint(1, i);
st.setstring(2, "aa"+i);
st.addbatch();
if(i % 1000 == 0){//每1000條向資料庫中添加一次
st.executebatch();
st.clearbatch();
st.executebatch();//防止還剩一些零頭的資料,這裡剛好是1000的倍數
st.clearbatch();
jdbc大文本、圖檔的操作以及批處理其實很簡單,掌握幾個關鍵的api就可以了,不再贅述,如有錯誤之處,歡迎留言指正~
<a target="_blank" href="http://blog.csdn.net/column/details/servletandjsp.html"></a>
_____________________________________________________________________________________________________________________________________________________
-----樂于分享,共同進步!