JDBC實現檔案、圖片的儲存執行個體

來源:互聯網
上載者:User

/*實現的功能:用資料庫儲存文本資料,並且讀取出來放在當前項目裡分析:難度不是很大,關鍵是掌握檔案流,資料庫的操作不是很多,但是檔案流的讀寫比較多日期:20131003作者:煙大陽仔*/public class Ckb_test {public static void main(String[] args) throws SQLException, IOException {// TODO Auto-generated method stubread();}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);}}}

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.