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 ();
}
}
}
}