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