Question

I need to get the installation path of MySQL in order to perform the export and import the database through java code. currently am working with eclipse. I need to get the installation path in a String variable "mySqlPath".

File fMysqlPath = new File("C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\");
String executeCmd = "mysqldump -u " + Constants.DB_USER + " -p" + 
                    Constants.DB_PASSWORD + " " + Constants.DB_NAME + " -r " + 
                    FilePath + "\\" + FileName;
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd, null, fMysqlPath);

This is what I have done. This has a dependency problem.

How can I solve this?

Was it helpful?

Solution

Have you tried this:

import java.sql.*;
import javax.sql.*;

public class MysqlPathFinderDemo{

public static void main(String args[]){
String dbtime;
String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select * FROM users";

try {

      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection (dbUrl);
      Statement stmt = con.createStatement();
      res = Myconnection.st.executeQuery("select @@datadir");
      String Mysqlpath = "";

      while(res.next()){
          Mysqlpath=res.getString(1) ;
      }

      Mysqlpath = Mysqlpath.replace("Data", "bin"); 
      System.err.println("Mysql path is :"+a);
   } catch(Exception ee) {
   }
 }
}

OTHER TIPS

You can directly query mysql and ask itself to give you the path:

SHOW VARIABLES LIKE 'basedir';

or the easier,

SELECT @@basedir;

Should give you the installation path. Both should result in something like:

C:\Program Files\MySQL\MySQL Server 5.1\

datadir is the equivalent to get path of the data directory (where the data lies).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top