天天看點

Java中如何使用嵌入MySQL

Java中如何使用嵌入MySQL

這篇檔案主要介紹在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