How does Java store images in the database in a binary format?

Source: Internet
Author: User
Import Java. SQL. *; import Java. io. *; public class insertimg {public static void main (string ARGs []) {try {class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); connection con = drivermanager. getconnection ("JDBC: ODBC: dbimg"); string SQL = "insert into dbimg (image) values (?) "; Preparedstatement pstmt = con. preparestatement (SQL); fileinputstream Fi = new fileinputstream (". /room.jpg "); pstmt. setbinarystream (1, Fi, Fi. available (); pstmt.exe cute (); FI. close (); pstmt. close (); con. close ();} catch (exception e) {system. out. println (E. getmessage () ;}}output the image to the File Import Java. io. *; import Java. SQL. *; public class imgread {public static void main (string ARGs []) {try {class. forname ("Com. microsoft. JDBC. sqlserver. sqlserverdriver "); string url =" JDBC: Microsoft: SQL SERVER: // billgates: 1433 "; connection Cn = drivermanager. getconnection (URL, "sa", "8310"); statement ST = cn. createstatement (); string STR = "select IMG from test where id = 1"; resultset rs1_st.exe cutequery (STR); RS. next (); inputstream in = Rs. getbinarystream (1); fileoutputstream file = new fileoutputstream (". /88.mp3 "); byte C [] = New byte [1024]; int r = 0; while (r = in. Read (c ))! =-1) {file. write (C, 0, R);} file. close (); In. close ();} catch (exception e) {e. printstacktrace () ;}} outputs the image to the jlabel control import Java. AWT. *; import Java. applet. *; import Java. AWT. image. bufferedimage; import com.sun.image.codec.jpeg. *; import javax. swing. *; import Java. SQL. *; import Java. io. *; public class displayimg {public displayimg () {image IMG = NULL; try {class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); connection con = drivermanager. getconnection ("JDBC: ODBC: dbimg"); statement stmt = con. createstatement (); string SQL = "select image from dbimg"; resultset rs1_stmt.exe cutequery (SQL); RS. next (); inputstream in = Rs. getbinarystream (1); required imagedecoder decoder = required codec. createjpegdecoder (in); IMG = decoder. decodeasbufferedimage (); RS. close (); con. close ();} catch (exception e) {system. out. println (E. getmessage ();} jframe F = new jframe (); jlabel label = new jlabel (); imageicon icon = new imageicon (IMG); label. seticon (icon); jscrollpane JSP = new jscrollpane (Label); Container content = f. getcontentpane (); content. add (JSP); F. setsize (535,400); F. setvisible (true);} public static void main (string ARGs []) {displayimg di = new displayimg () ;}} the driver of the above program database is inconsistent. Modify it by yourself. select the "image" type for the fields to store images in sqlserver.

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.