zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

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

mysqlJSP上传 实现 图片 存入
2023-06-13 09:12:17 时间

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!

我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题

本站部分文章参考或来源于网络,如有侵权请联系站长。
数据库远程运维 JSP实现图片上传存入MySQL(jsp上传图片mysql)