This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC and the fly likes how to upload pdf files to My sql with java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to upload pdf files to My sql with java" Watch "how to upload pdf files to My sql with java" New topic
Author

how to upload pdf files to My sql with java

P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
Hi

I need to save file to mysql database.

I tried through java, compilation no error, while running time iam getting errors please help me to solve the problem.

My inserting file code is:

My Sql Database is:

Compilation: javac SaveImageToDatabase.java
No error
Run : java SaveImageToDatabase
Error
In thread "main: java.lang.NoClassDefFoundError: SaveImageToDatabase
caused by: java.lang.ClassNotFoundException:SaveImageToDatabase
at java.net.URLClassLoader$1.run(URLCLassLoader.java:202)
at java.security.AccessController.doPrivileges(Native Method)
at java.net.URLClassLoader.findClass(URLCLassLoader.java:190)
at java.lang.ClassLoader.LoadClass(CLassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class:SaveImageToDatabase.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Please UseCodeTags. Don't forget to keep your indentation. I've added both for you this time.

You have a class path issue. You've probably set the class path and forgot to include the current directory (.).

I also see two quick improvements:
1) You don't need to cast fis to InputStream. It's a FileInputStream and therefore already an InputStream.
2) You should close the connection after closing the statement. Once the connection is closed its statements are already invalid.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
Hi

Thanks for guide i have solved the class path issue, i run the program its showing Uploaded successfully !. but if i went and saw in MySQL Workbench in image column its blank and its showing blob only the datatype.

I tried to retrieve the fie its showing java.Lang.NullPointerException.



Please help to solve my problem.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

first point, don't always trust mysql workbench, I spent about 2 hours pulling my hair out because my update wasn't working. I closed the workbench, opened it again, and WOW my update HAD worked.

second point, which line had the null pointer exception
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
Thank you
This is my full code:


After running this code its shows:


what are the data i have given its uploaded, i need to check that my file is uploaded sucessfully or not. and i need to see my file as output.

i need to see my image as output after uploading.

Iam new to developing field, if i did any mistakes please guide me.

Thanks in advance.


Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

sorry to be a pain, but is this in a .jsp file, as your first post was in a class?

at least for this stage I would put it back in a class, you will get much better errors out whilst testing it (like you did in the first post.)

Then once it is in a class read this thread http://www.coderanch.com/t/565873/JSP/java/connect-database-display-query-vqlus
it is about seperating your jsp from your pretty stuff on screen
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

and read this page, examples of your exact question:
http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
Hi

I have find out how to upload and download a file through java. now i need to get the upload path from jsp page and then the path want to store in variable, by using variable the file wants to save in MySQL database. iam using Eclipse and i created one jsp page and one class file for java, by using request.getParameter()
Iam getting the values from jsp page. but iam getting error. My java program separately running successfully, but i need to connect with jsp.

Please Help Me, iam new to developing field, please Guide me.

My MySql Creation:


My JSP Code :



My Java Code :


I have coded perfectly or not i don't know please check my code and help me.


Thanks in advance
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Please PostRealCode. That's not your real code, as HTML pages cannot call Java applications through form actions. You need a servlet instead, with the right URL mapping and all.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
Thank you for your Guide

I have change my code into servlet please help me iam getting error. iam using Eclipse for development.


My jsp page:



My servlet Code



My code is for i need to upload one file its wants to save in MySQL and and the copy of the file wants to download as C:\\ filename it is working in java perfect if i gave the path as manually,but i need to upload a file path by using jsp page

My web.xml code :


MySQL Database:



I have copy and pasted the class file in class folder and the jar files are commons-fileupload-1.2.2.jar,commons-io-1.3.2.jar, mysql-connector-java-3.0.17-ga-bin.jar,servlet-api.jar in lib folder.

while iam running the program iam getting error as


Please help me to solve the problem.

Thanks in advance.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

it is handy you are using eclipse, then you can debug the server, the line that must be going wrong is:



I always found it handy to get all of the parameters and loop through printing them out, as looking at the variables in the debug/variables tab is horrible.
but from experience it is usually the passing form, isn't doing what you expect.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
I can't understand what your are coming to tell me. please tell clearly and give any guide to solve this.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Files are not treated the same as other parameters. See http://www.coderanch.com/how-to/java/FileUpload for more information.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
I solved my problem it will store the the file into mysql

jpage.jsp:



upload.jsp:



web.xml


then we need servlet-api jar file, mysql-connector-java-3.0.17-ga-bin jar file, commons-fileupload-1.2.2 jar file, commons-io-1.3.2 jar file.


My program is running sucessfully and the file is loading to the server with file name and current date and time.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
I solved my problem it will store the the file into mysql

jpage.jsp:



upload.jsp:



web.xml


then we need servlet-api jar file, mysql-connector-java-3.0.17-ga-bin jar file, commons-fileupload-1.2.2 jar file, commons-io-1.3.2 jar file.


My program is running sucessfully and the file is loading to the server with file name and current date and time.
 
Consider Paul's rocket mass heater.
 
subject: how to upload pdf files to My sql with java