Java中嵌入式MySQL的使用方法_java

來源:互聯網
上載者:User

這篇檔案主要介紹在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 eliyanfei@126.com  * 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; 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();  //根據機器配置,設定不同的參數  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

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.