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 ){