My Java application needs to connect to a Mysql database file which is present on disk.
For that, it needs to start a Mysql server and use the server to read the file. The problem is that I am not sure how to start the server from within my Java code, read the Mysql file, make modifications to it, and then stop the server.
Currently, I am using something like below, but I don't see a way to start the server from code.
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setServerName("MysqlServer");
Connection conn = dataSource.getConnection();
Using this, I run into
Exception in thread "main" java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
which I think is because the server is not up. How do I start the server from code?
解决方案Option 1 : Find out how to start mysql from command line. Then use ProcessBuilder to do that
Option 2: Use embeddable version of mysql
, if that suits you : See details