Read a picture from the database and save it as a picture file

Source: Internet
Author: User
Tags database join mysql
Data | database

Import java.sql.*;
Import java.io.*;

public class Writedb {
public static void Main (string[] args) {
Connection Conn=null;
String driver= "Com.mysql.jdbc.Driver";//Drive
String url= "jdbc:mysql://127.0.0.1/test?useunicode=true;characterencoding=8859_1";//Database Join

try{
Class.forName (Driver). newinstance ();
Conn=drivermanager.getconnection (URL, "root", "");
File File=new file (Args[0]);
FileInputStream fis=new fileinputstream (file);
PreparedStatement Pstmt=conn.preparestatement (
"INSERT into the picture (picture) values (?)");
int bytes= (int) file.length ();
System.out.println (bytes);

Pstmt.setbinarystream (1,fis,bytes);//1 for inserted parameters 1, 2 ... Fileinstream for inserted data, bytes for byte length
Pstmt.executeupdate ();

Conn.close ();
Fis.close ();
}catch (Exception e) {
System.out.println (E.getmessage ());
}
}
}




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.