在數(shù)據(jù)庫(kù)中,有一條一條的記錄,記錄中很多字段都是幾個(gè)字符就夠的,假如現(xiàn)在要把一部小說(shuō)存入數(shù)據(jù)庫(kù),這本小說(shuō)當(dāng)然不是幾個(gè)字符組成,而是由幾萬(wàn)字組成,這本小說(shuō)的數(shù)據(jù)我們就可以說(shuō)是大數(shù)據(jù),生活中當(dāng)然有各種各樣的大數(shù)據(jù):電影,音樂(lè),圖片等等。。。
大字符數(shù)據(jù)內(nèi)容操作大字符內(nèi)容:通常是指很長(zhǎng)的字符類型的文件,例如小說(shuō),故事等等,內(nèi)容有字符組成。
下面說(shuō)明一下MySQL與Oracle中的大數(shù)據(jù)類型
| 數(shù)據(jù)種類 | 數(shù)據(jù)大小 | MySQL | Oracle |
| 字符 | 小 | char,varchar | varchar2 |
| 大 | text/longtext | clob | |
| 字節(jié) | 大 | bit,blob,longblob | blob |
@Test public void writeInDB() throws Exception { //獲取連接 connection = sqlUtil.getconnection(); //獲取對(duì)象 PReparedStatement preparedStatement = connection.prepareStatement("insert into book values(?)"); //準(zhǔn)備一個(gè)Reader用于讀取本地文件 Reader reader = new FileReader(new File("e:/test.txt")); //設(shè)置大數(shù)據(jù)參數(shù) preparedStatement.setClob(1, reader); //執(zhí)行SQL語(yǔ)句 preparedStatement.executeUpdate(); //關(guān)閉資源 reader.close(); sqlUtil.close(preparedStatement, connection); }2.從數(shù)據(jù)庫(kù)把大字符文件讀入到本地 @Test public void readFromDB() throws Exception { //獲取連接 connection = sqlUtil.getconnection(); //創(chuàng)建對(duì)象 PreparedStatement preparedStatement = connection.prepareStatement("SELECT content FROM book"); //設(shè)置參數(shù) //preparedStatement.setObject(1, "book"); //獲得結(jié)果 ResultSet res = preparedStatement.executeQuery(); //以String的形式獲得大字符內(nèi)容 while(res.next()) { String content = res.getString("content"); System.out.println(content); } //關(guān)閉資源 sqlUtil.close(preparedStatement, connection); }獲得結(jié)果后還有第二種方法:
@Test public void readFromDB() throws Exception { //獲取連接 connection = sqlUtil.getconnection(); //創(chuàng)建對(duì)象 PreparedStatement preparedStatement = connection.prepareStatement("SELECT content FROM book"); //設(shè)置參數(shù) //preparedStatement.setObject(1, "book"); //獲得結(jié)果 ResultSet res = preparedStatement.executeQuery(); FileWriter fileWriter = new FileWriter(new File("d:/11021.txt")); //利用Clob對(duì)象 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(); } //關(guān)閉資源 fileWriter.close(); sqlUtil.close(preparedStatement, connection); }以上就是對(duì)大字符文件的讀入與寫出~下面我們示范來(lái)對(duì)大字節(jié)文件的操作~
4.把大字節(jié)文件寫入數(shù)據(jù)庫(kù) @Test public void writeInDB() throws Exception { //獲取連接 connection = sqlUtil.getconnection(); //獲取對(duì)象 PreparedStatement preparedStatement = connection.prepareStatement("insert into book values(?,?)"); //準(zhǔn)備一個(gè)InputStream用于讀取本地文件 InputStream in = new FileInputStream(new File("f:/computer.jpg")); //設(shè)置大數(shù)據(jù)參數(shù) preparedStatement.setObject(1, 1); preparedStatement.setBlob(2, in); //也可以使用這個(gè) //preparedStatement.setBinaryStream(2, in); //執(zhí)行SQL語(yǔ)句 preparedStatement.executeUpdate(); //關(guān)閉資源 in.close(); sqlUtil.close(preparedStatement, connection); }5.從數(shù)據(jù)庫(kù)把大字節(jié)文件讀取到本地 @Test public void readFromDB() throws Exception { //獲取連接 connection = sqlUtil.getconnection(); //創(chuàng)建對(duì)象 PreparedStatement preparedStatement = connection.prepareStatement("SELECT content FROM book where id=?"); //設(shè)置參數(shù) preparedStatement.setInt(1, 1); //獲得結(jié)果 ResultSet res = preparedStatement.executeQuery(); FileOutputStream out = new FileOutputStream(new File("d:/999.jpg")); //利用Blob對(duì)象 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(); } //關(guān)閉資源 sqlUtil.close(preparedStatement, connection); }新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注