1

I want to create (export) my application into a Jar file to be portable.

How can i put my database contents with jar file?

For e.g for pictures, i put pictures folder beside my jar file, and it shows pictures correctly.

UPDATE

A peace of code to connect to database:

connection = DriverManager.getConnection("jdbc:mysql://localhost/Library", "root", "1234");
Sajad
  • 2,273
  • 11
  • 49
  • 92
  • which database are you using ? – Amit Sharma Dec 06 '13 at 16:58
  • What do you mean 'that has MySQL data'? Do you mean the jar contains the driver for MySQL or the scripts/data that make up the database? – Nick Holt Dec 06 '13 at 17:00
  • Please specify what exactly you want to embed in your jar file. A file tree of images or other normal files? A MySQL database dump? A MySQL driver? A running MySQL server (hint: this one is practically impossible)? – Emil Lundberg Dec 06 '13 at 17:21
  • @NickHolt my IDE has driver for MySQL, and now in don't know that my Jar file has that driver or no! – Sajad Dec 06 '13 at 17:32
  • @EmilLundberg My program used a simple database to save data, and also use images in graphical interface. – Sajad Dec 06 '13 at 17:34
  • @Sajjad You're still not answering my question. – Emil Lundberg Dec 06 '13 at 17:36
  • @EmilLundberg i don't running MySQL server, my application is written is java SE (desktop application, particular a library program). – Sajad Dec 06 '13 at 17:39
  • If you're not using a MySQL database (or any other SQL database, I'll assume) then what exactly is this "database" you're referring to? – Emil Lundberg Dec 06 '13 at 17:41
  • Could you please share the piece of code where you connect to 'database'? – Amit Sharma Dec 06 '13 at 17:42
  • @EmilLundberg I don't say that i don't use mysql MySQL database, I use MySqL database, and install it's driver to my IDE. – Sajad Dec 06 '13 at 17:43
  • @Sajjad : A piece of friendly advice. If you are new to Java, stay away from IDEs, use a good text editor like vim. That way you will learn more. IDEs hide out a lot of stuff which you need to understand. For example, where is your MySQL installation, which mysql driver are you using, etc. – Amit Sharma Dec 06 '13 at 17:55
  • 1
    Possible duplicate: http://stackoverflow.com/questions/3393397/embedding-mysql-in-java-desktop-application – Emil Lundberg Dec 06 '13 at 17:55
  • Perhaps better possible duplicate: http://stackoverflow.com/questions/1791417/java-mysql-is-there-a-way-to-embed-a-mysql-server-with-a-java-program?lq=1 – Emil Lundberg Dec 06 '13 at 18:11

2 Answers2

1

There are two approaches you could use:

Approach 1
Do you really need to use database? If not, store your data on files in file system, that way you can easily export it with data.

Approach 2
Bundle the mysql installation directory in your jar / installer. Write a scripts which starts up both MySQL server and you application.

Amit Sharma
  • 5,844
  • 5
  • 25
  • 34
  • I am Java programmer about one year, I should use your second approach, but HOW? – Sajad Dec 06 '13 at 18:00
  • Give it a try on your own, you have enough clues. That's how you learn. – Amit Sharma Dec 06 '13 at 18:03
  • 1
    @Sajjad: See [this question](http://stackoverflow.com/questions/1791417/java-mysql-is-there-a-way-to-embed-a-mysql-server-with-a-java-program). I think you're probably better off using a database system designed to be embedded, though, see [my answer](http://stackoverflow.com/a/20430595/833844). – Emil Lundberg Dec 06 '13 at 18:06
1

If you want to distribute a copy of your database with each copy of your application, I think using MySQL will be a bit complicated. You may want to look into using a database system designed to be embedded, such as SQLite, instead. A complete SQLite database is a single text file - you'd simply distribute your one mydatabase.db file along with the jar. See the examples at the above link.

Emil Lundberg
  • 7,268
  • 6
  • 37
  • 53