中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

jdbc 使用PreparedStatement來存儲和讀取大數(shù)據(jù)(Blob或Clob)

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
//假設(shè)這里建立的數(shù)據(jù)庫位use,而建立的表位luser

import  java.io.*;

import java.sql.*;

public class DBB{

private  static  String driver ="com.mysql.jdbc.driver";

private  static  String url ="jdbc:mysql://localhost:3306/use";
private static  String user="root";

private static String pass="admin";

public static  void main(String[]args){

Connection con= null;

PreparedStatement sta= null;

ResultSet res= null;

try{

//獲取數(shù)據(jù)源

//假設(shè)就是從d盤中讀取的一張照片;

File file= new File("d:" + File.separator + "photo.jpg");

int length= (int)file.length();

InputStream input = new FileInputStream(file);

//加載mysql驅(qū)動

Class.forName(driver);

//取得鏈接對象

con= DriverManager.getConnection(url,user,pass);

//取得操作對象

sta= con.prepareStatement("insert into luser values(?,?,?);");

//加載數(shù)據(jù)

sta.setInt(1,110);

sta.setString(2,"namefile");

sta.setBinaryStream(3,input ,length);

sta.executeUpdate();

sta.clearParameters();

input.close();//釋放資源;

}catch(SQLException e){

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}catch(ClassNotFoundException e){
e.printStackTrace();
}

finally{

if(con!=null){

try{

con.close();

}catch(SQLException e){   //(_)%^&**(_)(&*)(*#%$%^&*(()__)(((

e.printStackTrace();

}

}

}

Statement stat= null;

try{

//數(shù)據(jù)的取得

stat= con.createStatement();

stat.executeQuery("select* from luser;");

res.next();

String filename= res.getString(2);

Blob blob= res.getBlob(3);

//把獲得的數(shù)據(jù)寫到指定的文件中

FileOutputStream out= new FileOutputStream("d:" + File.separator+ "dong" +".bak");

out.write(blob.getBytes(1,(int)blob.length()));

out.flush();//把內(nèi)存中的東西刷出來

out.close();//釋放資源

}catch(SQLException e){

e.printStackTrace();

}

catch(IOException e){

e.printStackTrace();

}

finally{

if(stat!= null){

try{

stat.close();

}catch(SQLException e){

e.printStackTrace();

}



}
if(con!=null){

try{

con.close();

}catch(SQLException e){

e.printStackTrace();

}

}

}

}

} 

標簽: Mysql 數(shù)據(jù)庫

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇: C++實現(xiàn)KMP算法

下一篇:一個 MySQL 分庫分表php類