Java中嵌入式MySQL的使用方法
這篇文件主要介紹在Java中嵌入式MySQL的使用,對于一些的應用項目,提供安裝版的Mysql,Oracle是必須的工作。但是有時候如果是一個小的工具,可安裝或者移植性比較強的小軟件。再去安裝數(shù)據(jù)庫可能就比較麻煩了。
其實MySQL也有嵌入式的,不需要安裝,在使用的過程中,會自動創(chuàng)建數(shù)據(jù)庫以及通過代碼的方式啟動或者關(guān)閉。下面提供一些代碼片段,具體的會提供下載地址。
這個是核心代碼類,這個類實現(xiàn)了Mysql 的啟動和停止以及數(shù)據(jù)庫的啟動狀態(tài)。
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 eliyanfei@126.com * 2016年11月2日 下午1:44:55 * */ public final class EmbedMySqlServer { private MysqldResource mysqlInstance; //配置信息 public final Properties props; //端口信息 private String port; /** * 考慮到數(shù)據(jù)庫的性能問題,允許將數(shù)據(jù)庫放在其它磁盤 */ private String embedMySqlHome; public EmbedMySqlServer(final Properties props) { this.props = props; } public EmbedMySqlServer(final Properties props, String embedMySqlHome) { this.embedMySqlHome = embedMySqlHome; this.props = props; } 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() { if (mysqlInstance != null) mysqlInstance.cleanup(); } }
下面這個是啟動Demo,
public static void main(String[] args) { try { Properties pro = new Properties(); //根據(jù)機器配置,設置不同的參數(shù) pro.load(MysqlTest.class.getResourceAsStream("MySql_medium.properties")); new EmbedMySqlServer(pro).startup(); //可以把數(shù)據(jù)庫放到其他磁盤 //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高配機的配置樣例
具體的參數(shù)可以根據(jù)不同需求進行定義,比如端口可以自由定義。
需要引用的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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis-Plus實現(xiàn)公共字段自動填充功能詳解
在開發(fā)中經(jīng)常遇到多個實體類有共同的屬性字段,這些字段屬于公共字段,也就是很多表中都有這些字段,能不能對于這些公共字段在某個地方統(tǒng)一處理,來簡化開發(fā)呢?MyBatis-Plus就提供了這一功能,本文就來為大家詳細講講2022-08-08一文探究ArrayBlockQueue函數(shù)及應用場景
這篇文章主要為大家介紹了一文探究ArrayBlockQueue函數(shù)及應用場景,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03springboot運行到dokcer中 dockerfile的場景分析
這篇文章主要介紹了springboot運行到dokcer中 dockerfile,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03Spring實現(xiàn)在非controller中獲取request對象
這篇文章主要介紹了Spring實現(xiàn)在非controller中獲取request對象方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08mybatis foreach批量插入數(shù)據(jù):Oracle與MySQL區(qū)別介紹
這篇文章主要介紹了,需要的朋友可以參考下2018-01-01