Image Storage of sqlserver database (implemented in Java) and displayed on JSP

Source: Internet
Author: User
First case: Read and Write images in Java.

I wanted to find some code to test the method for accessing images in SQL Server. After the results were searched, I found that the process was too complicated, not based on the application in JSP. Today, I suddenly saw an article about how to implement database image storage in Java. It seems that I have been learning Java for a year, and I have been learning JDBC for a long time, but I haven't written about it yet. I wrote some articles on the Internet for one afternoon. Finally, I wrote it. Now paste the code. For your reference.

The code can definitely run, and the result on my machine is completely correct.
My machine is jdk1.5 + WINXP + eclipse
Package CN;

Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;

Public class photodemo {

 

  /**
    * @ Param ARGs
    */
    Connection conn = NULL;
    Public photodemo (){
        Try {
            String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = Master ";
            Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
                      Conn = drivermanager. getconnection (URL, "sa", "xiaoxin ");    
        }
        Catch (sqlexception e ){
            E. printstacktrace ();
        }
              Catch (classnotfoundexception CE ){
            Ce. printstacktrace ();
        }
    }
    Public void insert (){
        Try {
            String SQL = "insert into picture values (?,?) ";
            Preparedstatement PS = conn. preparestatement (SQL );
            File F = new file ("E:/Photo/baby/1.jpg ");
           
             
              Fileinputstream input = new fileinputstream (f );
              PS. setstring (1, "cute ");
              PS. setbinarystream (2, input, (INT) F. Length ());
              Ps.exe cuteupdate ();
              PS. Close ();
              Input. Close ();
          }
          Catch (sqlexception e ){
              E. printstacktrace ();
          }
              Catch (ioexception IE ){
Related Article

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.