3

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?

4
  • Were you able to run the app if mysql is started manually? Network errors could come from antivirus software/firewall etc Commented Apr 24, 2013 at 2:04
  • @yan: yes, the app works if mysql server is running. Commented Apr 24, 2013 at 2:13
  • @Sai: I am using OSX but looking for a platform independent solution. Commented Apr 24, 2013 at 2:14
  • If you want platform independence then why not use Apache Derby instead of MySQL? Commented Apr 24, 2013 at 3:09

5 Answers 5

4
  • 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
Sign up to request clarification or add additional context in comments.

Comments

4

you need to run 'mysqld.exe' to start the mysql server. This file can be found in the 'bin' folder in the mysql main folder (Assuming that you are working in windows). In my case 'mysql-5.6.10-win32\bin\mysqld.exe' .

From Java if you want to do the same - I used the following code and it worked. Hope it helps.

======================================================

String command = "~path~\\mysql-5.6.10-win32\\bin\\mysqld.exe";

try
{
    Process process = Runtime.getRuntime().exec(command);
} 
catch (IOException e)
{
    e.printStackTrace();
}

Comments

1

Look into Runtime.exec(), which will allow you to start mySQL via console.

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html

2 Comments

1) I would recommend the use of a ProcessBuilder instead of Runtime.exec() 2) Try to link to more up-to-date documentation in an answer. Right now, you are linking to Java 1.5
Fair enough. It's just the Java Doc link that came up first in Google, still works the same in 1.7.
0
Runtime runTime = Runtime.getRuntime();
Process p = runTime.exec(String[] cmdarray, String[] envp, File dir);
p.waitFor();

Comments

-3

Easiest way is to use JDBC all you need to do then is send queries to the server and interpret the results. A simple way to connect is below.

import java.sql.*;
Connection conn;
private void connect()
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection("jdbc:mysql://serverURL","user","pass);
        System.out.println("Connection Established");
    }
    catch (Exception ex) 
    {
        System.err.println("Unable to load the MySQL driver.");
        ex.printStackTrace();
    }

1 Comment

This expects the server to be running. He needs to start it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.