I am developing e-library for visually challenged people, in this system i kept a utility to add book from one interface. I want to store the location of the actual book in the database while adding a book.
For e.x.
I am storing the book at c:\books\pdfbooks\bookname.pdf

this location c:\books\pdfbooks\bookname.pdf
I want to store in the database automatically through coding at the background.

Can anyone help???????
Thanx in advance.........

Dani AI

Generated

Short answer: let the user pick the file with a GUI (JFileChooser), then copy or upload the file to a central place you control and store that server-side location (or a unique ID) in the database. Storing a client-local path like C:\books\pdfbooks\book.pdf only works for that one machine. Desktop.open(...) is fine to open local files, but if the canonical copy lives on a server you must download a temporary local copy first.

A simple workflow:

  • Let the user pick a file with JFileChooser and check the dialog result.
  • Stream the selected file to the server (HTTP multipart POST, a file-service endpoint, or copy to a network share). Don’t load the whole file into memory.
  • On the server save the bytes to a repository (filesystem or object store) and insert metadata into the DB: original name, stored path/URL or file-id, MIME type, size, checksum, uploader and timestamp.
  • When a client wants to open a book, download the file to a temp file and call Desktop.getDesktop().open(tempFile).

Minimal client example (concept):

if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
  File src = chooser.getSelectedFile();
  // stream src to server or copy to shared folder
}

DB design tip: store id (UUID), original_name, stored_path_or_url, mime_type, size, checksum, uploader_id, created_at. Decide between BLOBs (atomic backups, DB bloat) and filesystem/object storage (simpler serving, easier scaling).

Troubleshooting notes: if your Save/Upload button “does nothing,” confirm you check showOpenDialog/showSaveDialog return value (compare to JFileChooser.APPROVE_OPTION), verify write permissions on the target, create parent folders, and catch IOExceptions. As and suggested, use File/JFileChooser to pick files; as noted, compare the dialog return value (constant) rather than relying on internal approval handlers.

Recommended Answers

All 16 Replies

As you forgot to shed some light into file selection process, I can only recommend you to have look and consider use of File method getAbsolutePath() that will return String representation of file location

I am planning to use Desktop.getDesktop().Copy() or Move() method to save the file. So, how to store that location automatically.

Where are you getting these methods from? Neither I, nor the API documentation, knows anything about them.

Off topic - I guess somebody nailed your a$$ masijade :D , or somebody uses same name on Sun forum (highly unlikely)

So now what shall i do. I need the solution. Can u please help me?

I am developing the application where I need to open the book in text format word format, and say pdf format. What to do? I have to solve the problems on getting the location of the file too.

Please help

Off topic - I guess somebody nailed your a$$ masijade :D , or somebody uses same name on Sun forum (highly unlikely)

No. It's my nick, and DeskTop, and getDeskTop are valid, it's "Copy" and "Move" that don't exist. ;)

then what shall I do to add the book into the database of my library. Can u suggest anything?
Please help

Without mentioning any classes, describe exactly what it is you want to do again, because the first post, to me, doesn't make much sense.

Where is the "file" coming from?
Why do you need to "copy" it?
And, if you are copying it, why don't you know where you are copying it?

M developing e-library. Where one interface is provided by me for the users to add books into the database. Here, I want to add the books. Ofcourse, one more utility to open the desired book in desired format say .pdf. In my database I am storing the location of the book where they are stored and picking up that location to pass as a parameter to File class and using Desktop.getDesktop().open(File fl) to open the book.(Its working too).I can store the location of the available books directly into the database table. But this would be fine for the available books only. What if someone wants to add the book later on. Thats why i kept utility to add the books to the database through interface.

Questions are======>

1) How to add the books into the database the database is supposed to be on the remote server?

2) Since the user will adding the books how can he add the books and also storing their location to the database from interface.

Hope you got my point.
If you still confused can u please give me your gmail-id we can chat as i am online and hope u r online too
Please help me in any manner I am struggling with this problem from too long.

Thanks

Aren't "adding the books" to the database and "storing the books to the database" the same exact thing? You said them as if they were different. And we don't do chats and stuff like that, keep it on the site.

I still don't understand exactly what you want.

If this is a web application, then the "File" and "DeskTop" classes don't come into play (if you mean an application used through a browser), and so I don't know what you're getting at.

If it is not a web application, but rather a "distributed" application, then you would insert books using the same connection as you do to query them. In this case, see Sun's JDBC Tutorial.

First of all, my application is not web based. It is distributed application.
Continuing problem statement....
As in yahoo(just giving example) when we have to attach a file we are clicking on Browse button, and selecting the file to be attached. Ultimately. the location of the file is coming in the textfield. In this way I too want to read the location of the while where I have stored it. I want my system to read the location automaitcally, but for storing that location.
Suppose somebody is storing the book at

d:/ebooks/book.pdf

So this location should be stored in the database automatically.

Now if someone is storing the book at

e:/books/linux.pdf

So this too get stored properly in the location automatically.

I just want to store the locaiton however it is. My aim is to make my application as dyanmic as possible. I think writing too much is making others confused.

Can anybody help! Otherwise some R&D has to be performed by me.
Please help

Note: Please dont send me insert statement I know them very well.

If it is a "distributed" app, I assume you have a GUI. Look at the JFileChooser class.

I have seen the JFIleChooser utility. It is really usefull thanx for your valueable help. But I am unable to save the file. On clicking the save button it is not saving the file at the requested button.
One more thing
What is the difference between JFileChooser.APPROVE_SELECTION and JFileChooser.approveSelection()?
I have gone through the Javatutorial for the same but still did not understand?

Thanking You

APPROVE_SELECTION is a constant, which cannot do anything on its own. It is simply a variable that has a value. approveSelection() is a method, and methods can "do things". For example APPROVE_SELECTION might be a value indicating whether or not to OK what the user has selected. But the method approveSelection() will go ahead and do something to approve the selection. In other words variables (APPROVE_SELECTION) store values, methods (approveSelection()) do things. I hope that makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.