JDBC programming to implement file, picture storage method _java

Source: Internet
Author: User

This article describes the JDBC programming implementation file, image storage methods. Share to everyone for your reference, specific as follows:

/* Implementation of the function: Use the database to store text data, and read out in the current project analysis: Difficult is not very big, the key is to master file flow, database operation is not a lot, but the file stream read and write more than a date: 20131003 Author: Smoke Big Yang Tsai * * public class ckb_t EST {public static void main (string[] args) throws SQLException, IOException {//TODO auto-generated method stub re
 AD ();
  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 pattern 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 pattern conn=jdbcutilssingle.getinstance (). getconnection ();
   3. Create statement st=conn.createstatement ();
   4. Execution Statement Resultset=st.executequery ("Select text from Ckb_test");
    5. Processing 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); Features implemented by}}/*: Use the database to store picture data, and read it out in the current project analysis: Difficult is not very big, the key is to grasp the word throttling, the database operation is not many, but the file stream read and write more, pay attention to change the catalog date of the picture: 20131003 Author: Smoke Yang Zi * * PU Blic 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 ();
   Single Case design pattern conn=jdbcutilssingle.getinstance (). getconnection ();
   3. Create 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 pattern  Conn=jdbcutilssingle.getinstance (). getconnection ();
   3. Create statement st=conn.createstatement ();
   4. Execution Statement Resultset=st.executequery ("Select Big_bit from Blob_test");
    5. Processing result 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 help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.