Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, MySQL: Is there a way to embed a MySQL server with a Java program?

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.

like image 974
Legend Avatar asked Nov 24 '09 16:11

Legend


People also ask

Can we connect MySQL with Java?

In Java, we can connect to our database(MySQL) with JDBC(Java Database Connectivity) through the Java code. JDBC is one of the standard APIs for database connectivity, using it we can easily run our query, statement, and also fetch data from the database.

Can you embed MySQL?

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.

How does MySQL work with Java?

To connect to MySQL in Java, MySQL provides MySQL Connector/J, a driver that implements the JDBC API. MySQL Connector/J is a JDBC Type 4 driver. The Type 4 designation means that the driver is a pure Java implementation of the MySQL protocol and does not rely on the MySQL client libraries.

Can I use Java with xampp?

On the root of the Xampp folder you have one mysql_start. bat and one mysql_stop. bat , for start/stop the mysql database included on the Xampp package. You can use they in another bat you should create to start your Java Desktop application.


2 Answers

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).
like image 103
Simon Baars Avatar answered Sep 17 '22 11:09

Simon Baars


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/

like image 43
Legend Avatar answered Sep 17 '22 11:09

Legend