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

首頁 > 編程 > Java > 正文

JDBC編程實現文件、圖片的存儲方法

2019-11-26 14:55:19
字體:
來源:轉載
供稿:網友

本文實例講述了JDBC編程實現文件、圖片的存儲方法。分享給大家供大家參考,具體如下:

/*實現的功能: 用數據庫存儲文本數據,并且讀取出來放在當前項目里分析: 難度不是很大,關鍵是掌握文件流,數據庫的操作不是很多,但是文件流的讀寫比較多日期:20131003作者:煙大陽仔*/public class Ckb_test { public static void main(String[] args) throws SQLException, IOException {  // TODO Auto-generated method stub  read(); } static void create() throws SQLException, IOException {  Connection conn=null;  PreparedStatement prest=null;  ResultSet resultset=null;  try {   //2.建立連接   conn=JdbcUtils.getConnection();   //單例設計模式   conn=JdbcUtilsSingle.getInstance().getConnection();   //3.創建語句   String sql="insert into ckb_test(text) values(?)";   prest=conn.prepareStatement(sql);   File file=new File("src/cn/com/JDBC/JdbcUtils.java");   Reader reader=new BufferedReader(new FileReader(file));   prest.setCharacterStream(1, reader, (int)file.length());   //4.執行語句   int i=prest.executeUpdate();   reader.close();   System.out.println("i="+i);  } finally  {   JdbcUtils.free(resultset, prest, conn);  } } static void read() throws SQLException, IOException {  Connection conn=null;  Statement st=null;  ResultSet resultset=null;  try {   //2.建立連接   conn=JdbcUtils.getConnection();   //單例設計模式   conn=JdbcUtilsSingle.getInstance().getConnection();   //3.創建語句   st=conn.createStatement();   //4.執行語句   resultset=st.executeQuery("select text from ckb_test");   //5.處理結果   while(resultset.next())   {    Clob clob=resultset.getClob(1);    Reader reader=clob.getCharacterStream();    //reader=resultset.getCharacterStream(1);    File file=new File("JdbcUtils.java");    Writer writer=new BufferedWriter(new FileWriter(file));    char[] buff=new char[1024];    for(int i=0;(i=reader.read(buff))>0;)    {     writer.write(buff,0,i);    }    writer.close();    reader.close();   }  } finally  {   JdbcUtils.free(resultset, st, conn);  } }}/*實現的功能: 用數據庫存儲圖片數據,并且讀取出來放在當前項目里分析: 難度不是很大,關鍵是掌握字節流,數據庫的操作不是很多,但是文件流的讀寫比較多,注意更改圖片的目錄日期:20131003作者:煙大陽仔*/public class PictureBlob { public static void main(String[] args) throws SQLException, IOException {  read(); } static void create() throws SQLException, IOException {  Connection conn=null;  PreparedStatement prest=null;  ResultSet resultset=null;  try {   //2.建立連接   conn=JdbcUtils.getConnection();   //單例設計模式   conn=JdbcUtilsSingle.getInstance().getConnection();   //3.創建語句   String sql="insert into blob_test(big_bit) values(?)";   prest=conn.prepareStatement(sql);   File file=new File("C://Documents and Settings//Administrator//My Documents//My Pictures//cxg.jpg");   InputStream in=new BufferedInputStream(new FileInputStream(file));   prest.setBinaryStream(1, in, (int)file.length());   //4.執行語句   int i=prest.executeUpdate();   in.close();   System.out.println("i="+i);  } finally  {   JdbcUtils.free(resultset, prest, conn);  } } static void read() throws SQLException, IOException {  Connection conn=null;  Statement st=null;  ResultSet resultset=null;  try {   //2.建立連接   conn=JdbcUtils.getConnection();   //單例設計模式   conn=JdbcUtilsSingle.getInstance().getConnection();   //3.創建語句   st=conn.createStatement();   //4.執行語句   resultset=st.executeQuery("select big_bit from blob_test");   //5.處理結果   while(resultset.next())   {    Blob blob=resultset.getBlob(1);    InputStream in=blob.getBinaryStream();    //reader=resultset.getCharacterStream(1);    File file=new File("1.jpeg");    OutputStream out=new BufferedOutputStream(new FileOutputStream(file));    byte[] buff=new byte[1024];    for(int i=0;(i=in.read(buff))>0;)    {     out.write(buff,0,i);    }    out.close();    in.close();   }  } finally  {   JdbcUtils.free(resultset, st, conn);  } }}

希望本文所述對大家Java程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漠河县| 武鸣县| 苍溪县| 揭西县| 兴仁县| 崇信县| 同德县| 凤台县| 达拉特旗| 阿合奇县| 边坝县| 密山市| 海丰县| 定兴县| 巴彦县| 霍林郭勒市| 苗栗市| 扬中市| 江孜县| 弋阳县| 吴江市| 博客| 临沧市| 博爱县| 白沙| 嘉荫县| 大埔区| 天气| 阿勒泰市| 诸暨市| 永修县| 茶陵县| 陆良县| 平安县| 唐海县| 白朗县| 湘西| 历史| 铁岭市| 岳西县| 蒲江县|