JSP实现图片上传存入MySQL(jsp上传图片mysql)

Hello friends, I’m John. Today I’m going to share with you my experience of how to upload pictures to a MySQL database through JSP.

First, let’s talk about the preparation work. We need to create a database table named `pic` in MySQL. The table structure is as follows:

“` sql

CREATE TABLE pic(

id INT AUTO_INCREMENT NOT NULL,

data MEDIUMBLOB NOT NULL,

PRIMARY KEY(id)

) DEFAULT CHARSET = utf8;


Then we need to create a JSP page to upload the image, let's call it `index.jsp`.

```jsp




Upload images









The next step is to create another JSP page to save the uploaded image to the database, let’s name it `upload.jsp`.

“`jsp

Upload images

<%try{

// connect to mysql database through the specified url

Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”root”);

PreparedStatement pstmt = con.prepareStatement(“insert into pic values(null,?)”);

// type of data

pstmt.setBinaryStream(1, request.getInputStream());

pstmt.executeUpdate();

pstmt.close();

con.close();

%>

<% }catch(Exception e){

out.println(e);

%>


Finally, after the two JSP pages have been created, we can upload the image to the MySQL database. We will open the `index.jsp` page in the browser, select an image, click the “upload” button, and the uploaded image will be written to the database table.

In this way, we can use JSP language to upload pictures to MySQL database. Although the code looks quite complicated, in fact, by understanding its structure, we can use JSP as a convenient tool to implement this task quickly. I hope this tutorial can help more friends.

Thank you for your listening. See you next time!

数据运维技术 » JSP实现图片上传存入MySQL(jsp上传图片mysql)