zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Java中如何使用嵌入MySQL

2023-03-09 22:09:27 时间

这篇文件主要介绍在Java中嵌入式MySQL的使用,对于一些的应用项目,提供安装版的Mysql,Oracle是必须的工作。但是有时候如果是一个小的工具,可安装或者移植性比较强的小软件。再去安装数据库可能就比较麻烦了。

其实MySQL也有嵌入式的,不需要安装,在使用的过程中,会自动创建数据库以及通过代码的方式启动或者关闭。下面提供一些代码片段,具体的会提供下载地址。

这个是核心代码类,这个类实现了Mysql 的启动和停止以及数据库的启动状态。 

  1. package net.simple.mysql;  
  2. import java.io.File; 
  3. import java.util.HashMap; 
  4. import java.util.Map; 
  5. import java.util.Properties; 
  6. import java.util.Set 
  7. import com.mysql.management.MysqldResource;  
  8. /** 
  9.  *  
  10.  * @author 李岩飞 
  11.  * @email eliyanfei@126.com  
  12.  * 2016年11月2日 下午1:44:55 
  13.  * 
  14.  */ 
  15. public final class EmbedMySqlServer { 
  16.     private MysqldResource mysqlInstance; 
  17.     //配置信息 
  18.     public final Properties props; 
  19.     //端口信息 
  20.     private String port; 
  21.     /** 
  22.      * 考虑到数据库的性能问题,允许将数据库放在其它磁盘 
  23.      */ 
  24.     private String embedMySqlHome;  
  25.     public EmbedMySqlServer(final Properties props) { 
  26.         this.props = props; 
  27.     }  
  28.     public EmbedMySqlServer(final Properties props, String embedMySqlHome) { 
  29.         this.embedMySqlHome = embedMySqlHome; 
  30.         this.props = props; 
  31.     }  
  32.     public final String getEmbedMySqlHome() { 
  33.         return null == embedMySqlHome ? getPlatformBaseDir() : embedMySqlHome; 
  34.     }  
  35.     /** 
  36.     * 获得当前应用主目录 
  37.     * @return 当前应用启动程序所在目录. 
  38.     */ 
  39.     public static String getPlatformBaseDir() { 
  40.         return System.getProperty("user.dir"); 
  41.     } 
  42.  
  43.     public static boolean isBlank(final String str) { 
  44.         int strLen; 
  45.         if (str == null || (strLen = str.length()) == 0) { 
  46.             return true
  47.         } 
  48.         for (int i = 0; i < strLen; i++) { 
  49.             if (Character.isWhitespace(str.charAt(i)) == false) { 
  50.                 return false
  51.             } 
  52.         } 
  53.         return true
  54.     }  
  55.     public void startup() { 
  56.         final File baseDir = new File(getEmbedMySqlHome(), "mysql-em"); 
  57.         mysqlInstance = new MysqldResource(baseDir); 
  58.         port = props.getProperty("port"); 
  59.         if (isBlank(port)) 
  60.             props.put("port", port = String.valueOf((int) (Math.random() * 40000))); 
  61.         final Set<Object> keys = props.keySet(); 
  62.         final Map<String, String> options = new HashMap<String, String>(keys.size()); 
  63.         for (final Object key : keys) { 
  64.             final String val = props.getProperty(key.toString()); 
  65.             if ("".equals(val)) 
  66.                 options.put(key.toString(), null); 
  67.             else 
  68.                 options.put(key.toString(), val.replace("{$contextPath}", getPlatformBaseDir())); 
  69.         } 
  70.         if (!mysqlInstance.isRunning()) 
  71.             mysqlInstance.start("Em_MySQL", options, false, keys.contains("defaults-file")); 
  72.     }  
  73.     public String getPort() { 
  74.         return port; 
  75.     }  
  76.     /** 
  77.      * 判断mysql是否正在运行 
  78.      */ 
  79.     public boolean isRunning() { 
  80.         return null == mysqlInstance ? false : mysqlInstance.isRunning(); 
  81.     }  
  82.     public void shutdown() { 
  83.         if (mysqlInstance != null
  84.             mysqlInstance.shutdown(); 
  85.     } 
  86.  
  87.     public void cleanup() { 
  88.         if (mysqlInstance != null
  89.             mysqlInstance.cleanup(); 
  90.     } 

下面这个是启动Demo,

  1. public 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.  

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