This paper mainly introduces the use of embedded MySQL in Java, for some applications, to provide installation version of the mysql,oracle is necessary work. But sometimes, if it is a small tool, it can be installed or portable software is relatively strong. It may be more cumbersome to install the database.
In fact, MySQL also has embedded, do not need to install, in the use of the process, will automatically create a database and through the way of code to start or shut down. Here are some snippets of code that will provide the download address.
This is the core code class, which enables MySQL to start and stop, and the database to start the state.
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 li Yan @email eliyanfei@126.com * * November 2, 2016 PM 1:44:55 * * * * * Public final class Embedmysqlserver {
Private Mysqldresource mysqlinstance;
Configuration information public final Properties props;
Port information private String port;
/** * Allows the database to be placed on other disks/private String embedmysqlhome, taking into account the performance of the database;
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;
/** * Get current application home directory * @return current Application Launcher directory.
*/public static String Getplatformbasedir () {return System.getproperty ("User.dir"); public static Boolean IsBlank (finalString 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; /** * Determine if MySQL is running/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 ();
}
}
Here is the launch demo,
public static void Main (string[] args {
try {
Properties Pro = new properties ();
According to the machine configuration, set different parameters
Pro.load (MysqlTest.class.getResourceAsStream ("mysql_medium.properties"));
New Embedmysqlserver (PRO). startup ();
You can put the database into other disks
//new embedmysqlserver (pro, "f:\\"). Startup ();
Connection conn = Gettestconnection ();
System.out.println (conn.isclosed ());
Conn.close ();
} catch (Exception e) {
e.printstacktrace ();
}
}
mysql_general.properties General machine configuration Examples
mysql_medium.properties Medium machine Configuration sample
Configuration example of mysql_large.properties high-distribution machine
Specific parameters can be defined according to different requirements, such as the port can be defined freely.
Need to refer to MySQL two Jar,mysql-connector-mxj-gpl-6-0-11-db-files.jar,mysql-connector-mxj-gpl-6-0-11.jar
The code is on Git, and the address is: https://git.oschina.net/eliyanfei/api_tools.git
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.