1

Currently I am importing a database dump file sample.sql to the application. I need to import the database and append to the existing tables since the database contains same tables and fields.

How can I solve the issue? What is the command to append the imported database to existing database?

The code to import file

try {

    String[] command = new String[]{"\""+this.m_MySqlPath+"mysql\"", Constants.DB_NAME, "-u" + Constants.DB_USER, "-p" + Constants.DB_PASSWORD, "-e", " source "+"\""+FileName+"\"" };
    Process runtimeProcess = Runtime.getRuntime().exec(command, null, new File(this.m_MySqlPath));
    int processComplete = runtimeProcess.waitFor();

    if (processComplete == 0)
    {
        System.out.println("DatabaseManager.restore: Restore Successfull");
    }
    else 
    {
        System.out.println("DatabaseManager.restore: Restore Failure!");
    }
8
  • Was the database dump created with mysqldump? Commented Dec 11, 2012 at 10:53
  • Also what is the database structure and does it use auto incrementing on any columns that could overlap with the data your importing? Commented Dec 11, 2012 at 10:55
  • Tables contains auto increment. Commented Dec 11, 2012 at 10:59
  • @Gapchoos - Salam, can you explain your exact requirement? Because I cant understand question correctly Commented Dec 11, 2012 at 11:45
  • 1
    @Gapchoos - Your database script contain more data than in database. i mean you created database script first then deleted some values from database. In that case your database script contin more data. Is that correct? Commented Dec 11, 2012 at 12:19

1 Answer 1

1

Assume your script is "myscript.sql" and its stored in "c:\" then following command can be used to import that script to your database

c:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe --host=127.0.0.1 --user=root --password=root < c:/myscript.sql

This same command can be used in Runtime.getRuntime().exec . But when i tried there is some simple issues and its not working properly.So i used another method.

Before that i am assuming that your database script contain more data than in database. So even if i delete database and then import database using database script , i dont loss any data. I am just dropping(removing) current database and importing database from your database script file.

To solve above mentioned Runtime.getRuntime().exec , i created a batch file named "ba.bat" . And stored that batch file in c: drive as "c:\bat.bat". Content of that batch file is given below

%1\mysql.exe --host=%2 --user=%3 --password=%4 < c:/myscript.sql
exit

You can call with following syntax ba.bat arg1 arg2 arg3 arg4

where arg1=path to mysql bin folder example =c:\Program Files\MySQL\MySQL Server 5.1\bin arg2=mysql host ip address arg3=mysql username arg4=mysql password

And the final java program is given below

import java.sql.*;

public class NewClass {

    public static void main(String[] args) {
        Connection conn = null;
      String mysqlPath="c:/Program Files/MySQL/MySQL Server 5.1/bin";
        String url = "jdbc:mysql://192.168.100.86/";
        String databaseIp="192.168.100.86";
        String dbName = "databasename";
        java.sql.Statement stmt = null;
        String query = "";
        ResultSet result = null;
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "root";
        try {
            Class.forName(driver).newInstance();            
            conn = DriverManager.getConnection(url , userName, password);
            stmt = conn.createStatement();
            result = null;
            String test,input;
            stmt.execute("drop database if exists  "+dbName);
            String fullPathWithArgs ="C:/ba.bat \""+mysqlPath+"\" "+databaseIp+" "+userName+" "+password+"";
            Process runtimeProcess =Runtime.getRuntime().exec(fullPathWithArgs);
            int processComplete = runtimeProcess.waitFor();
            if (processComplete == 0)
                      {
                           System.out.println("DatabaseManager.restore: Restore Successfull");
                      }
            else
                  {
                     System.out.println("DatabaseManager.restore: Restore Failure!");
                  }

            conn.close();
        } catch (Exception e) {           
            System.out.println("exception: " + e.getMessage());
        }
    }
}

In the above java program i am dropping database and importing database from script. You must use CREATE DATABASE IF NOT EXISTS in your script.From your question i felt this is what you want. Please let me know in case of any problem or above is not your expected answer.

One more thing if you want to append data to existing database then please delete

stmt.execute("drop database if exists  "+dbName);

from java code and also make the required column in your database table to 'unique' ie.. create unique index for required table columns

Sign up to request clarification or add additional context in comments.

1 Comment

Rehman: this is not wat am expected to do. but still this gives the output. I need to export each table separately using INFILE and LOAD OUTFILE.

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.