这篇文件主要介绍在java中嵌入式mysql的使用,对于一些的应用项目,提供安装版的mysql,oracle是必须的工作。但是有时候如果是一个小的工具,可安装或者移植性比较强的小软件。再去安装数据库可能就比较麻烦了。
其实mysql也有嵌入式的,不需要安装,在使用的过程中,会自动创建数据库以及通过代码的方式启动或者关闭。下面提供一些代码片段,具体的会提供下载地址。
这个是核心代码类,这个类实现了mysql 的启动和停止以及数据库的启动状态。
package net.simple.mysql;
import java.io.file;
import java.util.hashmap;
import java.util.map;
import java.util.properties;
import java.util.set;
import com.mysql.management.mysqldresource;
/**
*
* @author 李岩飞
* @email [email protected]
* 2016年11月2日 下午1:44:55
*
*/
public final class embedmysqlserver {
private mysqldresource mysqlinstance;
//配置信息
public final properties props;
//端口信息
private string port;
/**
* 考虑到数据库的性能问题,允许将数据库放在其它磁盘
*/
private string embedmysqlhome;
public embedmysqlserver(final properties props) {
this.props = props;
}
public embedmysqlserver(final properties props, string embedmysqlhome) {
this.embedmysqlhome = embedmysqlhome;
public final string getembedmysqlhome() {
return null == embedmysqlhome ? getplatformbasedir() : embedmysqlhome;
* 获得当前应用主目录
* @return 当前应用启动程序所在目录.
*/
public static string getplatformbasedir() {
return system.getproperty("user.dir");
}
public static boolean isblank(final string str) {
int strlen;
if (str == null || (strlen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strlen; i++) {
if (character.iswhitespace(str.charat(i)) == false) {
return false;
}
return true;
public void startup() {
final file basedir = new file(getembedmysqlhome(), "mysql-em");
mysqlinstance = new mysqldresource(basedir);
port = props.getproperty("port");
if (isblank(port))
props.put("port", port = string.valueof((int) (math.random() * 40000)));
final set<object> keys = props.keyset();
final map<string, string> options = new hashmap<string, string>(keys.size());
for (final object key : keys) {
final string val = props.getproperty(key.tostring());
if ("".equals(val))
options.put(key.tostring(), null);
else
options.put(key.tostring(), val.replace("{$contextpath}", getplatformbasedir()));
if (!mysqlinstance.isrunning())
mysqlinstance.start("em_mysql", options, false, keys.contains("defaults-file"));
public string getport() {
return port;
* 判断mysql是否正在运行
public boolean isrunning() {
return null == mysqlinstance ? false : mysqlinstance.isrunning();
public void shutdown() {
if (mysqlinstance != null)
mysqlinstance.shutdown();
public void cleanup() {
mysqlinstance.cleanup();
}
下面这个是启动demo,
public static void main(string[] args) {
try {
properties pro = new properties();
//根据机器配置,设置不同的参数
pro.load(mysqltest.class.getresourceasstream("mysql_medium.properties"));
new embedmysqlserver(pro).startup();
//可以把数据库放到其他磁盘
//new embedmysqlserver(pro,"f:\\").startup();
connection conn = gettestconnection();
system.out.println(conn.isclosed());
conn.close();
} catch (exception e) {
e.printstacktrace();
}
mysql_general.properties一般机器的配置样例
mysql_medium.properties中等机器的配置样例
mysql_large.properties高配机的配置样例
具体的参数可以根据不同需求进行定义,比如端口可以自由定义。
需要引用的mysql两个jar,mysql-connector-mxj-gpl-6-0-11-db-files.jar,mysql-connector-mxj-gpl-6-0-11.jar
代码在git上,地址是:https://git.oschina.net/eliyanfei/api_tools.git
作者:李岩飞
来源:51cto