Java Call mysqldump production backup file is empty

Source: Internet
Author: User
Tags stmt

Today, try to write a Java applet, the function is to call mysqldump under Windows to backup a database, the code is as follows, pay attention to the red section.

Import java.sql.*;
Import java.io.*;

public class Mqldumptest {
public static void Main (string[] args) {
try {
String url = "JDBC:MYSQL://LOCALHOST/HXL";
String user = "root";
String pwd = "MySQL";

Load driver, this sentence can also be written as: Class.forName ("Com.mysql.jdbc.Driver");
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
Establish a connection to MySQL
Connection conn = drivermanager.getconnection (URL, user, pwd);

Execute SQL statement
Statement stmt = Conn.createstatement ();//Create statement object to execute SQL language
ResultSet rs = stmt
. ExecuteQuery ("Select Schema_name db_name from INFORMATION_SCHEMA. schemata where schema_name= ' Hxl ' ");

Working with result sets
while (Rs.next ()) {
String l_db_name = rs.getstring ("db_name");
System.out.println (L_db_name);

String command = "cmd/c mysqldump-h localhost-uroot-pmysql--default-character-set=gbk--single-transaction--databas Es hxl>e:\\mysqlbak\\hxl_bak_20140915.sql ";
SYSTEM.OUT.PRINTLN (command);
Runtime r = Runtime.getruntime ();

Process p = r.exec (command);

}
Rs.close ();//Close Database
Conn.close ();
} catch (Exception ex) {
System.out.println ("Error:" + ex.tostring ());
}
}
}

As the program executed successfully, but the problem is that the generated backup file is 0 bytes, here mysqldump is itself called the MySQL installation path under the C:\Program files\mysql\mysql Server 5.5\bin, online check that the path has a space problem, I try to copy the Mysqldump.exe to the C:\mysqldump\ directory, make sure that the path has no spaces, and then modify the code in the Java code, specify the run C:\mysqldump\mysqldump.exe between, and modify the following
String command = "cmd/c c:\\ Motor Oil\\mysqldump-h localhost-uroot-pmysql--default-character-set=gbk--single-transaction--databases hxl>E:\\ Mysqlbak\\hxl_bak_20140915.sql ";

After running again, the backup file is generated normally.

--The End--

Java Call mysqldump production backup file is empty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.