Store images to the database

Source: Internet
Author: User

Database SQL Server 2000

/*
--------------- Table structure ------------
Table Name: student2

+ -------- + ------------- + ------ + ----- + --------- + ------- +
| FIELD | type | null | key | default | extra |
+ -------- + ------------- + ------ + ----- + --------- + ------- +
| ID | int (4) | no | pri | null |
| Name | varchar (20) | Yes | null |
| Stupic | image | Yes | null |
+ -------- + ------------- + ------ + ----- + --------- + ------- +

*/

Store images to the database

/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package storeblob;

Import java. SQL .*;
Import java. Io .*;

/**
*
* @ Author Administrator
*/
Public class storeblob {

/**
* @ Param ARGs the command line arguments
*/
Public static void main (string [] ARGs ){
String url = "JDBC: sqlserver: // localhost: 1433; databasename = test ";

String user = "sa"; // replace it with your own database username
String Password = ""; // replace it with your own Database User Password

Connection con = NULL;
Preparedstatement PS = NULL;
Inputstream in = NULL;
Try {
Con = drivermanager. getconnection (URL, user, password );
// Read a read part from the local hard disk
In = new fileinputstream ("D:/111/001 .png ");
PS = con. preparestatement ("insert into student2 values (?,?,?) ");
PS. setint (1, 1 );
PS. setstring (2, "Qixin ");
PS. setbinarystream (3, in, in. Available ());
Ps.exe cuteupdate ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Finally {
Try {
// Close the stream
If (in! = NULL ){
In. Close ();
}
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
// Close the connection
PS. Close ();
Con. Close ();
}
Catch (exception E)
{
E. printstacktrace ();
}
}

}
}

 

Extract the image from the database

/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package storeblob;

Import java. Io .*;
Import java. SQL .*;

/**
*
* @ Author Administrator
*/
Public class getblob {

Public static void main (string [] ARGs ){
String url = "JDBC: sqlserver: // localhost: 1433; databasename = test ";

String user = "sa"; // replace it with your own database username
String Password = ""; // replace it with your own Database User Password
Connection con = NULL;
Statement ST = NULL;
Resultset rs = NULL;
Inputstream in = NULL;
Outputstream out = NULL;

Try {
Con = drivermanager. getconnection (URL, user, password );
St = con. createstatement ();
Rs = st.exe cutequery ("select stupic from student2 where id = 1 ");
Rs. Next (); // point the cursor to the first line
// Read stupic from RS and put it into inputstream object
In = Rs. getbinarystream ("stupic ");
// Declare the byte array to store image streams
Byte [] B = new byte [40000];
In. Read (B); // read data from the inputstream object and put it into the byte array.
// Instantiate the outputstream object and create an image file on disk D.
Out = new fileoutputstream ("D:/222.jpg ");
// Output the file, and the content is the data in the byte array
Out. Write (B );
Out. Flush ();

} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Finally {
Try {
If (in! = NULL ){
In. Close ();
}
If (OUT! = NULL ){
Out. Close ();
}
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
// Close the connection
Rs. Close ();
St. Close ();
Con. Close ();
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}
}

 

 

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.