0

I am trying to export a single table (location) from MySQL database (keep) but i can't do it by using java. Please help me.

preparedStatement = connection.prepareStatement("mysqldump –uroot –proot keep location> db_test.sql");
        preparedStatement.execute();
        connection.close();
7
  • 3
    you can use Runtime.getRuntime() .i think that is not a sql query Commented Jul 14, 2015 at 13:06
  • 1
    ONLY SQL queries can be fired through JDBC/ODBC. What you are doing is NOT SQL. Commented Jul 14, 2015 at 13:07
  • so what should I do? Commented Jul 14, 2015 at 13:09
  • @Ali yasar Erdogan,follow what 'Fast Snail' said. Commented Jul 14, 2015 at 13:10
  • Runtime.getRuntime().exec("C:\\xampp\\mysql\\bin\\mysqldump –u root –proot keep location> db_test.sql"); where does it save thanks btw Commented Jul 14, 2015 at 13:12

1 Answer 1

1

For a single table here is the code. We can save this table to a file.

    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/abc";
    String username = "root";
    String password = "root";
    Connection connection = DriverManager.getConnection(url, username, password);

    Statement stmt = connection.createStatement();

    String filename = "d:/outfile.sql";
    String tablename = "abc";
    stmt.executeQuery("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for helps. It worked. Can i use it to create an excel file too?
I didn't try it. Ill check.
@Ali yasar Erdogan, link to export a table to excel. mikescode.wordpress.com/2008/02/16/…
I just changed String filenamme = "d:/outfile.xls" and it worked perfectly.

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.