" + cmdarray[2]);

57. writer.flush();

58. writer.close();

59. os.close();

60. }

61.

62. /**

63. * 利用屬性文件提供的配置來拼裝命令語句

64. * 在拼裝命令語句的時(shí)候有一點(diǎn)是需要注意的:一般我們?cè)诿畲翱谥苯邮褂妹顏?/p>

65. * 進(jìn)行導(dǎo)出的時(shí)候可以簡(jiǎn)單使用">"來表示導(dǎo)出到什么地方,即mysqldump -uusername -ppassword databaseName > exportPath,

66. * 但在Java中這樣寫是不行的,它需要你用-r明確的指出導(dǎo)出到什么地方,如:

67. * mysqldump -uusername -ppassword databaseName -r exportPath.

68. * @param properties

69. * @return

70. */

71. private static String getExportCommand(Properties properties) {

72. StringBuffer command = new StringBuffer();

73. String username = properties.getProperty("jdbc.username");//用戶名

74. String password = properties.getProperty("jdbc.password");//用戶密碼

75. String exportDatabaseName = properties.getProperty("jdbc.exportDatabaseName");//需要導(dǎo)出的數(shù)據(jù)庫名

76. String host = properties.getProperty("jdbc.host");//從哪個(gè)主機(jī)導(dǎo)出數(shù)據(jù)庫,如果沒有指定這個(gè)值,則默認(rèn)取localhost

77. String port = properties.getProperty("jdbc.port");//使用的端口號(hào)

78. String exportPath = properties.getProperty("jdbc.exportPath");//導(dǎo)出路徑

79.

80. //注意哪些地方要空格,哪些不要空格

81. command.append("mysqldump -u")。append(username)。append(" -p")。append(password)//密碼是用的小p,而端口是用的大P.

82. .append(" -h")。append(host)。append(" -P")。append(port)。append(" ")。append(exportDatabaseName)。append(" -r ")。append(exportPath);

83. return command.toString();

84. }

85.

86. /**

87. * 根據(jù)屬性文件的配置,分三步走獲取從目標(biāo)文件導(dǎo)入數(shù)據(jù)到目標(biāo)數(shù)據(jù)庫所需的命令

 

88. * 如果在登錄的時(shí)候指定了數(shù)據(jù)庫名則會(huì)

89. * 直接轉(zhuǎn)向該數(shù)據(jù)庫,這樣就可以跳過第二步,直接第三步;

90. * @param properties

91. * @return

92. */

93. private static String[] getImportCommand(Properties properties) {

94. String username = properties.getProperty("jdbc.username");//用戶名

95. String password = properties.getProperty("jdbc.password");//密碼

96. String host = properties.getProperty("jdbc.host");//導(dǎo)入的目標(biāo)數(shù)據(jù)庫所在的主機(jī)

97. String port = properties.getProperty("jdbc.port");//使用的端口號(hào)

98. String importDatabaseName = properties.getProperty("jdbc.importDatabaseName");//導(dǎo)入的目標(biāo)數(shù)據(jù)庫的名稱

99. String importPath = properties.getProperty("jdbc.importPath");//導(dǎo)入的目標(biāo)文件所在的位置 

100. //第一步,獲取登錄命令語句

101. String loginCommand = new StringBuffer()。append("mysql -u")。append(username)。append(" -p")。append(password)。append(" -h")。append(host)

102. .append(" -P")。append(port)。toString();

103. //第二步,獲取切換數(shù)據(jù)庫到目標(biāo)數(shù)據(jù)庫的命令語句

104. String switchCommand = new StringBuffer("use ")。append(importDatabaseName)。toString();

105. //第三步,獲取導(dǎo)入的命令語句

106. String importCommand = new StringBuffer("source ")。append(importPath)。toString();

107. //需要返回的命令語句數(shù)組

108. String[] commands = new String[] {loginCommand, switchCommand, importCommand};

109. return commands;

110. }

111.

112.}

上述使用的jdbc.properties文件

1.jdbc.username=root

2.jdbc.password=password

3.jdbc.host=localhost

4.jdbc.port=3306

5.jdbc.exportDatabaseName=dbName

6.jdbc.exportPath=d:dbName.sql

7.jdbc.importDatabaseName=test

8.jdbc.importPath=d:dbName.sql

分享到

wangyao

相關(guān)推薦