熱線電話:13121318867

登錄
首頁精彩閱讀JDBC之大數據內容的傳輸_數據分析師
JDBC之大數據內容的傳輸_數據分析師
2014-12-14
收藏

JDBC之大數據內容的傳輸_數據分析師

JDBC之 大數據內容的傳輸

什么是大數據內容?

      在數據庫中,有一條一條的記錄,記錄中很多字段都是幾個字符就夠的,假如現在要把一部小說存入數據庫,這本小說當然不是幾個字符組成,而是由幾萬字組成,這本小說的數據我們就可以說是大數據,生活中當然有各種各樣的大數據:電影,音樂,圖片等等。。。

大字符數據內容操作

      大字符內容:通常是指很長的字符類型的文件,例如小說,故事等等,內容有字符組成。

      下面說明一下MySQL與Oracle中的大數據類型

數據種類 數據大小 MySQL Oracle
字符 char,varchar varchar2
text/longtext clob
字節 bit,blob,longblob blob

1.把大字符數據存進數據庫(把一個文本的數據存進MySQL中的text類型字段)

@Test public void writeInDB() throws Exception { //獲取連接 connection = sqlUtil.getconnection(); //獲取對象 PreparedStatement preparedStatement = connection.prepareStatement("insert into book values(?)"); //準備一個Reader用于讀取本地文件 Reader reader = new FileReader(new File("e:/test.txt")); //設置大數據參數 preparedStatement.setClob(1, reader); //執行SQL語句  preparedStatement.executeUpdate(); //關閉資源  reader.close();
    sqlUtil.close(preparedStatement, connection);
} 

2.從數據庫把大字符文件讀入到本地

@Test public void readFromDB() throws Exception
{ //獲取連接 connection = sqlUtil.getconnection(); //創建對象 PreparedStatement preparedStatement = connection.prepareStatement("SELECT content FROM book"); //設置參數//preparedStatement.setObject(1, "book");//獲得結果 ResultSet res = preparedStatement.executeQuery(); //以String的形式獲得大字符內容 while(res.next())
    {
String content = res.getString("content");
System.out.println(content);
    } //關閉資源  sqlUtil.close(preparedStatement, connection);
}

      獲得結果后還有第二種方法:

@Test public void readFromDB() throws Exception
{ //獲取連接 connection = sqlUtil.getconnection(); //創建對象 PreparedStatement preparedStatement = connection.prepareStatement("SELECT content FROM book"); //設置參數//preparedStatement.setObject(1, "book");//獲得結果 ResultSet res = preparedStatement.executeQuery();  FileWriter fileWriter = new FileWriter(new File("d:/11021.txt")); //利用Clob對象 if(res.next())
{
Clob clob = res.getClob("content");
Reader reader = clob.getCharacterStream(); //然后把Reader寫入到本地文件中 char[] cr = new char[1024]; int len = 0; while((len = reader.read(cr))!=-1)
{
fileWriter.write(cr, 0, len);
}
reader.close();
} //關閉資源 fileWriter.close();
sqlUtil.close(preparedStatement, connection);
}

      以上就是對大字符文件的讀入與寫出~下面我們示范來對大字節文件的操作~

4.把大字節文件寫入數據庫

 @Test public void writeInDB() throws Exception { //獲取連接 connection = sqlUtil.getconnection(); //獲取對象 PreparedStatement preparedStatement = connection.prepareStatement("insert into book values(?,?)"); //準備一個InputStream用于讀取本地文件 InputStream in = new FileInputStream(new File("f:/computer.jpg")); //設置大數據參數 preparedStatement.setObject(1, 1);
preparedStatement.setBlob(2, in); //也可以使用這個//preparedStatement.setBinaryStream(2, in);//執行SQL語句 preparedStatement.executeUpdate(); //關閉資源 in.close();
sqlUtil.close(preparedStatement, connection);
    }

5.從數據庫把大字節文件讀取到本地

@Test public void readFromDB() throws Exception
{ //獲取連接 connection = sqlUtil.getconnection(); //創建對象 PreparedStatement preparedStatement = connection.prepareStatement("SELECT content FROM book where id=?"); //設置參數 preparedStatement.setInt(1, 1); //獲得結果 ResultSet res = preparedStatement.executeQuery();

FileOutputStream out = new FileOutputStream(new File("d:/999.jpg")); //利用Blob對象 if(res.next())
{ //Blob blob = res.getBlob("content");//InputStream in =  blob.getBinaryStream();//這樣也行  InputStream in = res.getBinaryStream("content"); //然后把Reader寫入到本地文件中 byte[] buf = new byte[1024]; int len = 0; while((len = in.read(buf))!=-1)
{ out.write(buf, 0, len);
} in.close(); out.close();
} //關閉資源  sqlUtil.close(preparedStatement, connection);
}

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢