这篇文件主要介绍在Java中嵌入式MySQL的使用,对于一些的应用项目,提供安装版的Mysql,Oracle是必须的工作。但是有时候如果是一个小的工具,可安装或者移植性比较强的小软件。再去安装数据库可能就比较麻烦了。
其实MySQL也有嵌入式的,不需要安装,在使用的过程中,会自动创建数据库以及通过代码的方式启动或者关闭。下面提供一些代码片段,具体的会提供下载地址。
这个是核心代码类,这个类实现了Mysql 的启动和停止以及数据库的启动状态。
1package net.simple.mysql; 2 3import java.io.File; 4import java.util.HashMap; 5import java.util.Map; 6import java.util.Properties; 7import java.util.Set; 8 9import com.mysql.management.MysqldResource; 10 11/** 12 * 13 * @author 李岩飞 14 * @email eliyanfei@126.com 15 * 2016年11月2日 下午1:44:55 16 * 17 */ 18public final class EmbedMySqlServer { 19 private MysqldResource mysqlInstance; 20 //配置信息 21 public final Properties props; 22 //端口信息 23 private String port; 24 /** 25 * 考虑到数据库的性能问题,允许将数据库放在其它磁盘 26 */ 27 private String embedMySqlHome; 28 29 public EmbedMySqlServer(final Properties props) { 30 this.props = props; 31 } 32 33 public EmbedMySqlServer(final Properties props, String embedMySqlHome) { 34 this.embedMySqlHome = embedMySqlHome; 35 this.props = props; 36 } 37 38 public final String getEmbedMySqlHome() { 39 return null == embedMySqlHome ? getPlatformBaseDir() : embedMySqlHome; 40 } 41 42 /** 43 * 获得当前应用主目录 44 * @return 当前应用启动程序所在目录. 45 */ 46 public static String getPlatformBaseDir() { 47 return System.getProperty("user.dir"); 48 } 49 50 public static boolean isBlank(final String str) { 51 int strLen; 52 if (str == null || (strLen = str.length()) == 0) { 53 return true; 54 } 55 for (int i = 0; i < strLen; i++) { 56 if (Character.isWhitespace(str.charAt(i)) == false) { 57 return false; 58 } 59 } 60 return true; 61 } 62 63 public void startup() { 64 final File baseDir = new File(getEmbedMySqlHome(), "mysql-em"); 65 mysqlInstance = new MysqldResource(baseDir); 66 port = props.getProperty("port"); 67 if (isBlank(port)) 68 props.put("port", port = String.valueOf((int) (Math.random() * 40000))); 69 final Set<Object> keys = props.keySet(); 70 final Map<String, String> options = new HashMap<String, String>(keys.size()); 71 for (final Object key : keys) { 72 final String val = props.getProperty(key.toString()); 73 if ("".equals(val)) 74 options.put(key.toString(), null); 75 else 76 options.put(key.toString(), val.replace("{$contextPath}", getPlatformBaseDir())); 77 } 78 if (!mysqlInstance.isRunning()) 79 mysqlInstance.start("Em_MySQL", options, false, keys.contains("defaults-file")); 80 } 81 82 public String getPort() { 83 return port; 84 } 85 86 /** 87 * 判断mysql是否正在运行 88 */ 89 public boolean isRunning() { 90 return null == mysqlInstance ? false : mysqlInstance.isRunning(); 91 } 92 93 public void shutdown() { 94 if (mysqlInstance != null) 95 mysqlInstance.shutdown(); 96 } 97 98 public void cleanup() { 99 if (mysqlInstance != null) 100 mysqlInstance.cleanup(); 101 } 102}
下面这个是启动Demo,
1public static void main(String[] args) { 2 try { 3 Properties pro = new Properties(); 4 //根据机器配置,设置不同的参数 5 pro.load(MysqlTest.class.getResourceAsStream("MySql_medium.properties")); 6 new EmbedMySqlServer(pro).startup(); 7 //可以把数据库放到其他磁盘 8 //new EmbedMySqlServer(pro,"f:\\").startup(); 9 Connection conn = getTestConnection(); 10 System.out.println(conn.isClosed()); 11 conn.close(); 12 } catch (Exception e) { 13 e.printStackTrace(); 14 } 15 }
MySql_general.properties一般机器的配置样例
MySql_medium.properties中等机器的配置样例
MySql_large.properties高配机的配置样例
1如果需要初始化用户名和密码,在启动的配置文件里面,加入下面3句话 2initialize-user=true 3initialize-user.user=root 4initialize-user.password=123456
具体的参数可以根据不同需求进行定义,比如端口可以自由定义。
需要引用的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