Use Java to store images in sqlserver

Source: Internet
Author: User
Use Java to store images in sqlserver

Connection conn = NULL;
Try {
Drivermanager. registerdriver (new COM. Microsoft. JDBC. sqlserver. sqlserverdriver ());
Conn = drivermanager. getconnection ("JDBC: Microsoft: sqlserver: // ip: 1433; databasename = MOFTEC", "sa", "mymm ");
}
Catch (exception E)
{
// Catch an exception and throw an ioexception
Out. println ("can not connec to the database! The exception is "+ E. tostring ());
}
Fileinputstream FCM = NULL;
File file = new file ("E:/Java/chart.jpg ");
Try {
FS = new fileinputstream (File );
} Catch (filenotfoundexception e ){
Out. println ("not find file! ");
}
Preparedstatement PS = conn. preparestatement ("insert into gs_img (Lei, years, IMG) values (?,?,?) ");
PS. setstring (1, "Ivan ");
PS. setint (2, 4 );
PS. setbinarystream (3, FS, (INT) file. Length ());
Ps.exe cuteupdate ();
PS. Close ();
Try {
FCM. Close ();
Out. println ("written in! ");
} Catch (ioexception e ){
Out. println ("FCM cann' t cloase! ");
}

 

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.