自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java中如何使用嵌入MySQL

開發(fā) 后端
這篇文件主要介紹在Java中嵌入式MySQL的使用,對(duì)于一些的應(yīng)用項(xiàng)目,提供安裝版的Mysql,Oracle是必須的工作。但是有時(shí)候如果是一個(gè)小的工具,可安裝或者移植性比較強(qiáng)的小軟件。再去安裝數(shù)據(jù)庫(kù)可能就比較麻煩了。

[[175251]]

這篇文件主要介紹在Java中嵌入式MySQL的使用,對(duì)于一些的應(yīng)用項(xiàng)目,提供安裝版的Mysql,Oracle是必須的工作。但是有時(shí)候如果是一個(gè)小的工具,可安裝或者移植性比較強(qiáng)的小軟件。再去安裝數(shù)據(jù)庫(kù)可能就比較麻煩了。

其實(shí)MySQL也有嵌入式的,不需要安裝,在使用的過(guò)程中,會(huì)自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)以及通過(guò)代碼的方式啟動(dòng)或者關(guān)閉。下面提供一些代碼片段,具體的會(huì)提供下載地址。

這個(gè)是核心代碼類,這個(gè)類實(shí)現(xiàn)了Mysql 的啟動(dòng)和停止以及數(shù)據(jù)庫(kù)的啟動(dòng)狀態(tài)。 

  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.      * 考慮到數(shù)據(jù)庫(kù)的性能問(wèn)題,允許將數(shù)據(jù)庫(kù)放在其它磁盤 
  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.     * 獲得當(dāng)前應(yīng)用主目錄 
  37.     * @return 當(dāng)前應(yīng)用啟動(dòng)程序所在目錄. 
  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是否正在運(yùn)行 
  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.     } 

下面這個(gè)是啟動(dòng)Demo,

  1. public static void main(String[] args) {  
  2. try {  
  3. Properties pro = new Properties();  
  4. //根據(jù)機(jī)器配置,設(shè)置不同的參數(shù)  
  5. pro.load(MysqlTest.class.getResourceAsStream("MySql_medium.properties")); 
  6.  new EmbedMySqlServer(pro).startup();  
  7. //可以把數(shù)據(jù)庫(kù)放到其他磁盤  
  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一般機(jī)器的配置樣例

MySql_medium.properties中等機(jī)器的配置樣例

MySql_large.properties高配機(jī)的配置樣例

具體的參數(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

代碼在Git上,地址是:https://git.oschina.net/eliyanfei/api_tools.git

責(zé)任編輯:未麗燕 來(lái)源: 開源中國(guó)社區(qū)
相關(guān)推薦

2021-07-09 12:37:31

GoPython編程語(yǔ)言

2024-01-31 08:53:01

Java數(shù)組代碼

2013-03-04 14:35:05

WordPressEdge AnimatHTML5

2010-08-10 14:08:09

Flex嵌入字體

2015-07-06 09:59:56

JavaScript私有成員

2010-08-09 15:30:00

Flex字體

2021-11-24 15:20:04

FreeDOSLinux

2022-02-14 23:17:16

Gitlab嵌入式工具

2021-07-12 08:06:32

Java

2013-07-03 13:54:26

jQuery

2015-10-16 10:48:03

Gate One嵌入Web

2010-08-10 15:55:20

FlexHTML頁(yè)面

2019-11-14 16:23:07

MySQL索引數(shù)據(jù)庫(kù)

2011-06-24 09:13:30

QT SDL

2020-12-07 06:42:09

LinuxIP命令

2021-08-02 13:38:21

Linuxalias命令

2011-08-15 14:27:51

CocoaRunLoop

2020-02-21 08:00:00

Pythonasyncio編程語(yǔ)言

2024-09-27 09:12:12

JavaScriptscrollTo窗口

2010-03-30 16:22:55

Oracle不同版本
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)