這篇文件主要介紹在java中嵌入式mysql的使用,對(duì)于一些的應(yīng)用項(xiàng)目,提供安裝版的mysql,oracle是必須的工作。但是有時(shí)候如果是一個(gè)小的工具,可安裝或者移植性比較強(qiáng)的小軟件。再去安裝數(shù)據(jù)庫可能就比較麻煩了。
其實(shí)MySQL也有嵌入式的,不需要安裝,在使用的過程中,會(huì)自動(dòng)創(chuàng)建數(shù)據(jù)庫以及通過代碼的方式啟動(dòng)或者關(guān)閉。下面提供一些代碼片段,具體的會(huì)提供下載地址。
這個(gè)是核心代碼類,這個(gè)類實(shí)現(xiàn)了Mysql 的啟動(dòng)和停止以及數(shù)據(jù)庫的啟動(dòng)狀態(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; } /** * 獲得當(dāng)前應(yīng)用主目錄 * @return 當(dāng)前應(yīng)用啟動(dòng)程序所在目錄. */ 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是否正在運(yùn)行 */ 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(); } }
下面這個(gè)是啟動(dòng)Demo,
public static void main(String[] args) { try { Properties pro = new Properties(); //根據(jù)機(jī)器配置,設(shè)置不同的參數(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一般機(jī)器的配置樣例
立即學(xué)習(xí)“Java免費(fèi)學(xué)習(xí)筆記(深入)”;
MySql_medium.properties中等機(jī)器的配置樣例
MySql_large.properties高配機(jī)的配置樣例
如果需要初始化用戶名和密碼,在啟動(dòng)的配置文件里面,加入下面3句話 initialize-user=trueinitialize-user.user=root initialize-user.password=123456
具體的參數(shù)可以根據(jù)不同需求進(jìn)行定義,比如端口可以自由定義。
需要引用的mysql兩個(gè)jar,mysql-connector-mxj-gpl-6-0-11-db-files.jar,mysql-connector-mxj-gpl-6-0-11.jar
java怎么學(xué)習(xí)?java怎么入門?java在哪學(xué)?java怎么學(xué)才快?不用擔(dān)心,這里為大家提供了java速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)