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

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

認(rèn)識(shí)JDBC 2.0中的高級(jí)數(shù)據(jù)類(lèi)型

2019-11-17 06:00:36
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  JDBC 2.0中提供了對(duì)SQL3標(biāo)準(zhǔn)中引入的新的數(shù)據(jù)類(lèi)型,如Blob(binary large object)、Clob(character large object)、Array 對(duì)象、REF(對(duì)象參考,object reference)和 UDT(用戶(hù)定義數(shù)據(jù)類(lèi)型,user-defined datatype)等的支持。這些新的數(shù)據(jù)類(lèi)型結(jié)合在一起,使得數(shù)據(jù)庫(kù)設(shè)計(jì)人員可以創(chuàng)建更豐富的模式,并簡(jiǎn)化了對(duì)復(fù)雜數(shù)據(jù)的處理和持久化。

  例如,我們要向tbl_User表中插入用戶(hù)的照片,這時(shí)就可以使用流將Blob對(duì)象導(dǎo)入數(shù)據(jù)庫(kù)中:

String sql = "intsert into tbl_User values(?, ?)";
PReparedStatement pstmt = con.prepareStatement(sql) ;

File file = new File("C:/images/photo.jpg") ;
FileInputStream fis = new FileInputStream(file);

pstmt.setString(1, "John");
pstmt.setBinaryStream(2, fis, (int)file.length());

pstmt.executeUpdate();

pstmt.close();
fis.close();
  其中SQL語(yǔ)句的第一個(gè)參數(shù)為用戶(hù)名,第二個(gè)參數(shù)為photo,它是一個(gè)Blob型對(duì)象。這樣在將數(shù)據(jù)插入數(shù)據(jù)庫(kù)之后,我們就可以用程序獲取該數(shù)據(jù)了:

String sql = "select photo from tbl_User where username = ?";
PreparedStatement pstmt = con.prepareStatement(selectSQL) ;

pstmt.setString(1, "John");
ResultSet rs = pstmt.executeQuery() ;

rs.next();
Blob blob = rs.getBlob("photo") ;

ImageIcon icon = new ImageIcon(blob.getBytes(1, (int)blob.length())) ;
JLabel photo = new JLabel(icon);

rs.close();
pstmt.close();
  類(lèi)似地,我們也可以對(duì)Clob對(duì)象進(jìn)行相應(yīng)的操作。下面是一個(gè)從 ASCII 流中直接將 Clob對(duì)象插入數(shù)據(jù)庫(kù)中的例子:

String sql = "insert into tbl_Articles values(?,?)";
PreparedStatement pstmt = con.prepareStatement(sql) ;

File file = new File("C:/data/news.txt") ;
FileInputStream fis = new FileInputStream(file);

pstmt.setString(1, "Iraq War");
pstmt.setAsciiStream(2, fis, (int)file.length());

pstmt.executeUpdate();

pstmt.close();
fis.close();
  同樣,我們也可以用類(lèi)似的方法將Clob對(duì)象從數(shù)據(jù)庫(kù)中取出:

String sql = "select content from tbl_Articles where title = ?";
PreparedStatement pstmt = con.prepareStatement(sql) ;

pstmt.setString(1, "Iraq War");
ResultSet rs = pstmt.executeQuery() ;

rs.next() ;
Clob clob = rs.getClob("content") ;

InputStreamReader in = new InputStreamReader(clob.getAsciiStream()) ;

JTextArea text = new JTextArea(readString(in)) ;

rs.close();
pstmt.close();

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汉源县| 黑水县| 平泉县| 陵川县| 淳安县| 仁寿县| 广元市| 温泉县| 耒阳市| 新和县| 怀来县| 大兴区| 炎陵县| 垫江县| 佛学| 德阳市| 历史| 出国| 防城港市| 财经| 泾川县| 尤溪县| 信阳市| 辽中县| 双桥区| 建宁县| 定西市| 天门市| 武鸣县| 乌兰浩特市| 黔南| 长治市| 漾濞| 阿勒泰市| 深水埗区| 肥乡县| 简阳市| 丰县| 济源市| 通州市| 兴业县|