r/javahelp • u/Ad_Which • Aug 11 '20
How to upload image to MySQL database and display it using spring boot and Thymeleaf?
My question has nothing much to explain i just need some guidance on how to insert image into MySql database and show it to webpage when needed using spring boot and thymeleaf.
How I have design our model class like bellow i am posting model class of an product object,assume i have stored image of project in Mysql data base,i want to fetch it using Spring data Jpa and show to user using Thymeleaf Template Engine...
What changes i need to do in below model class and other related things,I searched on google on but unable to understand.
Thanks in advance
Your words are value for me....
u/Entity
class Product{
u/Id
u/GeneratedValue
int pID;
String ProductName;
int ProductCost;
int QuentityAvailable;
//NoArgConstructor
//Constructor
//Getter
//Setter
}13
u/sadjava Aug 11 '20
In 2020, you don’t store the actual image in the db; you store a key (a unique string) to locate it from object storage (like S3) or, if you insist, the local file system.
1
u/dadmda Aug 11 '20
Where I work we use both, for profile pictures we store varbinaries, for other pictures we use file system
1
u/Ad_Which Aug 16 '20
u/polothedawg and u/sadjava i thaught it is easy can you please explain what i can do i am unable to understand any thing.
I am showing 100 products on webpage how i can manage it because all products have different image.
2
u/polothedawg Intermediate Brewer Aug 16 '20
Do you have a GitHub or something to show your code?
1
u/Ad_Which Aug 19 '20
u/polothedawg thanks,no i don't have i am going to create....
1
u/Ad_Which Aug 20 '20
u/polothedawg i have created git hub account,if you don't mind please help me i am getting:-
org.hibernate.tool.schema.spi.CommandAcceptanceException:
Caused by: java.sql.SQLSyntaxErrorException: Specified key was too long; max key length is 1000 bytesgithub repository:- https://github.com/atharva-mishra/demo_ecommerce
1
u/polothedawg Intermediate Brewer Aug 20 '20 edited Aug 20 '20
I don’t see which field in your product model you are trying to store as a lob or blob? Also this error seems to indicate you are trying to index this field, not a good idea. There has to be some code missing, can you please commit it in a branch or something, even if it’s WIP
1
u/Ad_Which Aug 20 '20
u/polothedawg I have not worked on BLOB and LOB yet first i am trying to create basic view of page and then BLOB and LOB. I was trying to get some idea on how to deal with images in spring data JPA in parallel and suddenly you asked for github and then i created github.
1
u/polothedawg Intermediate Brewer Aug 21 '20 edited Aug 21 '20
I see you’ve enabled the show-sql property, can you tell me for what query you are getting this exception? Also see this post for more info on why you might get this error. https://stackoverflow.com/a/50689604
1
u/Ad_Which Aug 21 '20
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table users_roles add constraint FKgd3iendaoyh04b95ykqise6qh foreign key (user_id) references user (id)" via JDBC Statement
Caused by: java.sql.SQLSyntaxErrorException: Specified key was too long; max key length is 1000 bytes================================================
java.sql.SQLException: Field 'rollid' doesn't have a default valueAfter Submitting the user registration form.
2
u/polothedawg Intermediate Brewer Aug 21 '20
Have you configured the storage engine properly(see link I sent)?
1
u/Ad_Which Aug 21 '20 edited Aug 21 '20
yes if you see there are 4 entity classes = products,user,role and users_role and in MySql database there 4 tables created.
I am able to fetch data from product table. If i am running the program first i am getting CommandAcceptanceException
and then the spring boot application is started.
→ More replies (0)
12
u/polothedawg Intermediate Brewer Aug 11 '20
You need to base64 encode the image and store it in a blob if you really want to. As other said already, please rethink this approach unless it’s for a personal project. This could help you along your way https://stackoverflow.com/a/47569292