如何以编程方式在Java中启动Mysql服务器? [英] How to start a Mysql server in Java programatically?
问题描述
我的Java应用程序需要连接到磁盘上存在的Mysql数据库文件.
My Java application needs to connect to a Mysql database file which is present on disk.
为此,它需要启动Mysql服务器并使用该服务器读取文件.问题是我不确定如何从Java代码中启动服务器,读取Mysql文件,对其进行修改,然后停止服务器.
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();
使用它,我遇到了
线程"main"中的异常java.lang.RuntimeException:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败
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?
推荐答案
- 选项1:了解如何从命令行启动mysql.然后使用 ProcessBuilder >
- 选项2:使用可嵌入版本的mysql ,如果适合的话:请参见详细信息
- 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
这篇关于如何以编程方式在Java中启动Mysql服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!