我想在Web应用程序上使用java导入mysql数据库但不幸的是mysql.exe进程被卡住了,下一行代码没有被执行,这使得浏览器继续加载。
restore.xhtml
<h:panelGrid columns="1" style="margin: 0 auto;">
<p:commandButton value="1 click restore"
action="#restoreBean.oneClickRestore()}" update="all,g" ajax="false"/>
</h:panelGrid>
RestoreBean.java
String absPath ="C:/Users/sms/build/web/resources/backup/backuproot.sql";
String[] executeCmd = new String[]{"mysql ", dbName, "-u " + dbUser, "-p " + dbPwd, "-e ", " \"source "+ absPath+" \""};
try{
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();// after this line no line of code get executed; in taskmanager u can see mysql.exe
if (processComplete == 0) {
restoreMsg = "Successfully restored from SQL : " + absPath;
} else {
restoreMsg = "Error at restoring";
}
} catch (InterruptedException ex) {
Logger.getLogger(RestoreBean.class.getName()).log(Level.SEVERE, null, ex);
}
但是相同的代码运行良好,并且在控制台应用程序中使用时导入数据库
RestoreDbTest.java
public class RestoreDbTest{
public static void main(String[] args) {
RestoreDbTest b = new RestoreDbTest();
b.restoreDatabase("root", "", "b.sql");
}
public boolean restoreDatabase(String dbUserName, String dbPassword, String source) {
String[] executeCmd = new String[]{"mysql ", dbName, "-u " + dbUser, "-p " + dbPwd, "-e ", " \"source b.sql \""};
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
// log.info("Backup restored successfully with " + source);
return true;
} else {
//log.info("Could not restore the backup " + source);
}
} catch (Exception ex) {
System.out.println(ex.getCause());
}
return false;
}
}
mysql进程列表