国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

JDBC之 大數(shù)據(jù)內(nèi)容的傳輸

2019-11-14 22:11:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
JDBC之 大數(shù)據(jù)內(nèi)容的傳輸JDBC之 大數(shù)據(jù)內(nèi)容的傳輸

什么是大數(shù)據(jù)內(nèi)容?

  在數(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ō)明一下MySQLOracle中的大數(shù)據(jù)類型

數(shù)據(jù)種類數(shù)據(jù)大小MySQLOracle
字符char,varcharvarchar2
text/longtextclob
字節(jié)bit,blob,longblobblob

1.把大字符數(shù)據(jù)存進(jìn)數(shù)據(jù)庫(kù)(把一個(gè)文本的數(shù)據(jù)存進(jìn)MySQL中的text類型字段)
@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);    }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 望江县| 双峰县| 曲周县| 佛教| 文水县| 河西区| 隆回县| 北安市| 道孚县| 昌都县| 宁城县| 松滋市| 喀什市| 扎兰屯市| 钟祥市| 广南县| 高陵县| 饶平县| 鹿邑县| 宁德市| 克什克腾旗| 台南市| 腾冲县| 辽阳市| 云南省| 天津市| 北辰区| 鹿泉市| 龙陵县| 台山市| 尖扎县| 曲阳县| 江阴市| 西青区| 甘孜县| 新乐市| 凤台县| 囊谦县| 交口县| 孙吴县| 嵊泗县|