This article describes the JDBC programming method of storing files and pictures through examples. Share it with everyone for your reference, the details are as follows:
/*Function implemented: Use the database to store text data, and read it out and put it in the current project for analysis: It is not very difficult. The key is to master the file flow. There are not many operations on the database, but there are more reading and writing of the file flow. Date: 20131003 Author: Yan Dayangzi*/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. Establish a connection conn=JdbcUtils.getConnection(); //Single case design mode conn=JdbcUtilsSingle.getInstance ().getConnection(); //3. Create statement 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. Execute statement 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. Establish a connection conn=JdbcUtils.getConnection(); //Single case design mode conn=JdbcUtilsSingle.getInstance().getConnection(); //3. Create a statement st=conn.createStatement(); //4. Execute statement resultset=st.executeQuery("select text from ckb_test"); //5. Process the results 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); } }}/*Function implemented: Use the database to store image data, and read it out and put it in the current project for analysis: It is not very difficult. The key is to master the byte stream. There are not many operations on the database, but there are many reading and writing of file streams. Pay attention to changing the directory date of the picture: 20131003 Author: Yan Dayangzi*/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. Establish a connection conn=JdbcUtils.getConnection(); // Singleton design mode conn=JdbcUtilsSingle.getInstance().getConnection(); //3. Create a statement 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. Execute statement 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. Establish a connection conn=JdbcUtils.getConnection(); //Single case design mode conn=JdbcUtilsSingle.getInstance().getConnection(); //3. Create statement st=conn.createStatement(); //4. Execute statement resultset=st.executeQuery("select big_bit from blob_test" ); //5. Process the results 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); } }}
I hope this article will be helpful to everyone in Java programming.