A JSP program that accesses image files to the database

Source: Internet
Author: User
Tags insert connect sql mysql string stmt mysql database
js| Program | data | Database I posted the code to the MySQL database to write the image code of the program, but a lot of people are Java beginners, for this code, they can not convert it to JSP, so I am here to write with JSP how to write image files to the database. You first build a table in the database, and the following code is common to any database, as long as you support blob-type
As long as you will be connected to the database to change the parameters on it.
Sql>create table image (ID int,content varchar (), image blob);
If you are in a sqlserver2000 database, you can change the BLOB field to the image type, which is new in SqlServer2000.
The contents of the testimage.html file are as follows:
<HTML>
<HEAD>
<title>image File </TITLE>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
</HEAD>
<form method=post action= "testimage.jsp" >
<input type= "text" name= "content" ><BR>
<input type= "file" name= "image" ><BR>
<input type= "Submit" ></FORM>
<BODY>
</BODY>
</HTML>

We define an action testimage.jsp in the action of form, which reads as follows:
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<%@ page import= "java.util.*"%>
<%@ page import= "java.text.*"%>
<%@ page import= "java.io.*"%>
<body>
<%class.forname ("Org.gjt.mm.mysql.Driver"). newinstance ();
String url= "jdbc:mysql://localhost/mysql?user=root&password=&useunicode=true&characterencoding=8859 _1 ";
MySQL is the name of your database, user for you to connect the database users, password for you to connect the database user's password, you can change
Connection conn= drivermanager.getconnection (URL);
String content=request.getparameter ("content");
String Filename=request.getparameter ("image");
FileInputStream str=new fileinputstream (filename);
String sql= "INSERT INTO Test (id,content,image) VALUES (1,?,?)";
PreparedStatement pstmt=dbconn.conn.preparestatement (SQL);
Pstmt.setstring (1,content);
Pstmt.setbinarystream (2,str,str.available ());
Pstmt.execute ();
Out.println ("Success,you Have Insert an Image successfully");
%>

Let me write a test image output example to see if we have written the above program is correct, testimageout.jsp content is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<%@ page import= "java.util.*"%>
<%@ page import= "java.text.*"%>
<%@ page import= "java.io.*"%>
<body>
<%class.forname ("Org.gjt.mm.mysql.Driver"). newinstance ();
String url= "jdbc:mysql://localhost/mysql?user=root&password=&useunicode=true&characterencoding=8859 _1 ";
MySQL is the name of your database, user for you to connect the database users, password for you to connect the database user's password, you can change
Connection conn= drivermanager.getconnection (URL);
String sql = "Select image from Test where id=1";
Statement Stmt=null;
ResultSet Rs=null;
try{
Stmt=conn.createstatement ();
Rs=stmt.executequery (SQL);
}catch (SQLException e) {}
try {
while (Rs.next ()) {
Res.setcontenttype ("Image/jpeg");
Servletoutputstream sout = Response.getoutputstream ();
InputStream in = Rs.getbinarystream (1);
byte b[] = new byte[0x7a120];
for (int i = In.read (b); I!=-1;)
{
Sout.write (b);
In.read (b);
}
Sout.flush ();
Sout.close ();
}
}
catch (Exception e) {System.out.println (e);}
%>
</body>
You run this program and you will see just what you have written to the beautiful picture that will be shown in front of you. How, use JSP to try.
This method of writing a picture to the database will allow the database to soar in a short time, affecting performance, and another way to upload the picture to the server,
It's a good way to store only the path of a picture in a database. I suggest that you take the latter approach.

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.