I need to create a simple Java application that connects to a local database file, and will run on a mac.
I've figured that JDBC is a good option, but what file format/drivers should I use?
Is .MDB files a possibility?
Thanks for any help!
For a small DB I would suggest using a DB written in Java. The DB's below or all
< 2mb. Keeping it in java means it is easy to transfer to Windows / Linux if need be.
Possible DB's include:
H2 - H2 has a mixed mode where the first time the DB is opened it is op
Advantages:
Mixed Mode
Only a couple of Files
has built-in SQL
HSQLDB - Version HSQLDB 1.80 is the smallest jar of the three (by a big margin)
The 2.* jars are similar in size to H2
Advantages:
Small size (Version 1.80)
Only a couple of Files
Apache Derby
All three offer Server and Embedded mode, H2 has mixed mode as well. All three are open source.
Use Apache Derby embedded in your application. This keeps it simple while still using standard JDBC.
H
Is .MDB files a possibility?
It could be, via a library like UCanAccess. However, using an Access database (.mdb or .accdb) would only be advisable if there was some other compelling reason to do so, e.g., to take a copy of the database file and use it with some other application that requires an Access database.
Otherwise, one of the suggestions from the other answers would probably be a better choice.
Related
I've downloaded and imported into my project this JDBC driver that supposedly supports Linux (albeit tested only on SUSE Linux).
What I want is to be able to use a .mdf database through Java on Linux. The queries obviously need something to run on, not simply connect to a database - if I am understanding this correctly.
I found a question on SO that seems to have valuable information concerning my issue.
So I was thinking a FreeTDS + JDBC combination. Will queries execute as expected or do I have to go use the database natively, on Windows? The latter basically crushes OS agnosticism that Java provides which I would like avoid, if possible.
The database itself isn't anything fancy, only a few tables - this is merely a school assignment, after all.
I will provide additional information, if needed.
An mdf file can be an old (JET based) Microsoft Access database file, so a JDBC driver for SQL Server, a totally different product, isn't going to help you much. Here on SO there are a couple of posts that explain how to use the JDBC-ODBC bridge to access Access databases via the ODBC driver detour, but since you specifically mention Linux that's not going to help you.
I see two possible (free) approaches:
use the open source jackcess package that allows Java programs across platforms to open, read and write MS Access database
transfer the content of the MSAccess database file to some database that has native Java drivers (that is, most of them that are not MS Access).
However, an mdf file could also be a SQLServer database, but using that database under Linux is also going to be quite tricky, as you'll need some edition of SQL Server to open it - see here for some details on how to "attach" such an mdf file to an installed instance of SQLServer - but to my knowledge there is no edition of SQL Server that runs under Linux. So, unless you can find some Windows machine to run SQLServer on, and then connect to it using the JDBC driver you have both options mentioned before may be valid alternatives (ie, option 1 = copy your data into an MSAccess file and access that file with jackcess or option 2, copy the data into some Linux-tolerant database with a decent JDBC driver and use that)
Some people have been mislead by my question (I am not a native speaker of English, could be that), the database was meant to be run locally (explicitly not on a remote server) which simply cannot happen with the SQL Server + Linux combination. Wine is a possibility, but that is just too much hassle. So, the solution is either to simply use SQL Server on Windows or use an alternative cross-platform RDBMS like MySQL.
Nevertheless, I'll accept fvu's edited answer and would like to thank other users for the time they've invested in their own answers.
Suppose I created a standalone application in java and distributed it through my website.
What I need to know is what is the ideal Database solution I can use so that all the users can use the application without installing anything extra.
For example, if I use SQL server or MySql database, the downloader will need those installed so that I can dump my database on it. Another solution I see is to use Database file like Microsoft Access .mdb file. But that too will need Microsoft Office installed on the client. XML files are platform independent but anyone can open and change them.
What is the solution used by most of the applications?
H2 is a very potent candidate for your need. We use it as a default RDBMS in all our demo applications and also for db-driven unit testing.
You could use an embedded database. There are hundreds for Java some which use SQL and many which don't. Any database you use, you may want the users to be able to maintain, so users editing the data may be a good thing.
You could consider Derby as it ships with Java.
I would use plain flat files which are CSV formatted unless you have a more specific requirement. e.g. you can load and edit them in excel/spreadsheet editor/text editor.
H2 is the successor of HSQLDB (same developer). It is not only one of the finest embedded DBs for JAVA, it is also free, even for commercial use. It's great with hibernate and comes with with a powerful web-interface (you can start it by running the h2 jar file). Additionally there is the possibility to encrypt you database (AES). We use that feature for getting some security aspects right with some of our projects.
check javadb. it is easy and can be redistributed
Firebird can do this : there is an embedded version.
There is some good java drivers
Using MDB files on Windows doesn't require any additional installation requirements such as Microsoft Office. DAO and ADO have come with the Windows OS since Windows 2000.
I have a Java SE application, and it uses a database. I am currently using XML files to store data, but I´m afraid it causes some errors in later use.
So it would be good to use a Postgree/MySQL like DB. A real database. but the problem is, it is a commercial application, it runs under windows, and should be 2 clicks to install. I really don't like the idea of installing a database together with my application, and then running scripts to build the tables.
Is there a database that I can use as a Java API? Or should I just continue to use XML? (I'm synchronizing every access to my XML files). Whats the best choice?
One of following embedded databases:
HSQLDB
H2
Derby
They are lightweight, take up very little space and can be embedded without problems in your application.
Since they are written in Java, and each one is a simple jar file, your deployment headaches will be kept to a minimum.
You've mentioned MySQL and PostgreSQL. Although I haven't tried it, H2 features several compatibility modes for various popular databases, including ones you've mentioned.
H2 Database
It can be embedded in your application with no clicks to install and it supports JDBC and most ORMs.
It is definitely worth to look for a simple SQLite database as the storage. It is just a single file on your file system. No need to set up a server. Check out the following thread for an introduction: Java and SQLite Another database you could check out is HSQLDB (link inside another answer inside this thread). That one is a relational database engine written in Java also based on a single file.
Based on my previous naive experience of storing data into XML, I wrote a entire forum system on XML and it's based on RSS parser and writer. The XML files became very big and thus causing frequent request time outs on the application.
You can instead use something like SQLite, where data is stored in a file but more managed and allows declarative statements like SELECT * FROM table.
i am not clear about steps/configuration details about how i can embed mysql in a Java desktop application so that it(application) can be installed on any machine through a single executable file and doing so sets up database and also provides an exe to run the app.Till now i have built my app using netbeans and i have used mysql to set up database.plz guide me further.
MySQL isn't an embedded database - the only JAR related to it is the JDBC driver. It requires a installation process, which might be able to be scripted via Java, but the process will definitely function outside of the Java application you intend it to support. Meaning, you can turn off your Java application but the MySQL service/daemon will still be running.
Only the libmysqld is embeddable.
There are embedded databases - SQLite, Firebird - and embedded databases made in Java - HSQL, Derby/(can't remember what it was called before). I believe SQL Server Compact Edition is embeddable, while SQL Server Express/MSDE is not. I don't know if Oracle has an embeddable version....
I would strongly recommend H2. It is a very fast embedded database written in Java and I've found it easier to use than some of the others mentioned such as HSQL.
Edit:
On the H2 website, you can see a speed comparison of H2 vs Derby, HSQL, MySql, etc...
Here's information on how to backup the database.
While theoritcaly possable it would not be easy. The standard MySql distributions assume you want to set up a general purpose database server with separate from the client applications cominicating via odbc etc.
You may be better looking at the "pure java" options like HSQL or JavaDB which are designed to be embedded in a java application, and need little or no "setup".
Another possibility is Sqlite which only needs a single binary plus the sqljbbc jar file. This is again designed from scratch to be embedded inside an application and requires zero admin apart from allocating a file for the database.
Take a look at http://dev.mysql.com/doc/refman/5.0/en/connector-mxj.html. I do not remember the exact details but I was able to embed MySQL db in desktop application without user needing to install it separately.
The key class is com.mysql.management.MysqldResource.
Here is the example, http://dev.mysql.com/doc/refman/5.0/en/connector-mxj-configuration-java-object.html
The mysql-connector-mxj-gpl-db-files.jar file contains MySQL installation files for all the platforms. If you know which is your target platform, you can strip other platform versions from jar, to reduce download size for end user.
If you want an embedded database with java, then use one written in Java designed to be embedded. I know Apache Derby Can be embedded and apparently H2 too.
How big amount of data dó you need the database to handle?
One thing I love about .NET is the ability to have a database file along with the project. I know that using a SQLite database, this can be done, but did someone achieve this with a MySQL database backend?
So for instance, if I run a java program, it should be able to start its own mini MySQL server and manipulate data. So essentially, I want the same flow as with a SQLite but I need the power of MySQL.
If you don't mind using MariaDB (the open source variant of MySQL, basically works the same) MariaDB4j can be the perfect option for production enviroments.
MariaDB4j is a Java (!) "launcher" for MariaDB (the "backward
compatible, drop-in replacement of the MySQL(R) Database Server", see
FAQ and Wikipedia), allowing you to use MariaDB (MySQL(R)) from Java
without ANY installation / external dependencies. Read again: You do
NOT have to have MariaDB binaries installed on your system to use
MariaDB4j!
As it works completely without any requirements that have to be on the users' pc it is probably the best option to get MySQL embedded. Converting a project that doesn't use an Embedded database into MariaDB4j is as easy as calling:
DB db = DB.newEmbeddedDB(3306);
Read the github page for more information. Maven central dependency is:
<dependency>
<groupId>ch.vorburger.mariaDB4j</groupId>
<artifactId>mariaDB4j</artifactId>
<version>2.2.3</version>
</dependency>
You can combine this with the newest driver to get access to all functionality of MySQL 8.0 (win64/win32=windows, mac64=macos, linux64=linux):
<dependency>
<groupId>org.craftercms.mariaDB4j</groupId>
<artifactId>mariaDB4j-db-win64</artifactId>
<version>10.4.6.2</version>
</dependency>
If you do mind using MariaDB, another option is Wix Embedded MySQL.
Wix Embedded MySQL is a library that provides a way to run real MySql within integration tests.
Why?
Your tests can run on production-like environments: match version, encoding, timezone, database/schema/user settings;
Its easy, much easier than installing right version by hand;
You can use different versions/configuration per project without any local set-up;
Supports multiple platforms: Windows, Linux, and OSX;
Provides constantly updated multiple versions of MySql - 5.5, 5.6, 5.7, 8.0;
Testing matrix for all supported OSes (x86/x64) and versions (5.5, 5.6, 5.7, 8.0).
A quick search shows this: MySQL Connector/MXJ — for embedding MySQL server in Java applications on the MySQL Downloads page at:
http://dev.mysql.com/downloads/
For future reference to anyone looking to embed mysql, there is a utility from the mysql guys that does this http://downloads.mysql.com/archives/c-mxj/
It sounds like you want an embedded database. While MySQL Connector seems nice, it will launch a separate server process. If you want the database server to run in the Java virtual machine, there are several embedded databases for Java.
The two that I've seen used the most are:
Apache Derby / JavaDB
HSQL