[英]mysql command prints out help menu when ran from java program
我的mysql命令是从Java程序内部运行的,并打印出帮助菜单,但是当我在终端中运行完全相同的命令时,它运行正常。 不太清楚为什么。
String shellCommand = "mysql -u root < " + destinationDirectory+"/"+ filename;
executeCommand(shellCommand);
这是execute命令方法:
public static void executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process process;
try {
process = Runtime.getRuntime().exec(command);
JOptionPane.showMessageDialog(null, "COMMAND: "+ command);
process.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.toString());
e.printStackTrace();
}
System.out.println(output.toString());
}
编辑:这是另一种方法,但是它给了我这个错误,我尝试运行实际的mysql命令而不是像原来那样使用休眠模式,但仍然无济于事。 等待在那里,可以让还原有一些时间到达由于锁定而挂起的位置,然后关闭程序,以便释放锁定和数据库:
// JDBC driver name and database URL
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://" + HOST + "/" + DATABASE;
Connection conn = null;
final Statement stmt;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
stmt = conn.createStatement();
Services.endServices();
Thread progress = new Thread() {
@Override
public void run() {
try {
String sql = "source /mount/mf/outbox/b3sql.sql";
stmt.executeUpdate(sql);
System.out.println("Database loaded successfully...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}
}
};
progress.start();
TimeUnit.SECONDS.sleep(15);
System.exit(0);
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
--
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'source /mount/mf/outbox/b3sql.sql'
您的Shell可以理解输入重定向“ <”,但是Java不能启动Shell! 尝试这个:
String shellCommand[] =
{"/usr/bin/mysql", "-h", "127.0.0.1",
"-u", "myDBuser",
"--password=aMeHoElA",
"-e", "source /here/is/myscript.sql" };
executeCommand(shellCommand);
并修改(命令现在是字符串数组):
public static void executeCommand(String command[]) {
并且不要忘记阅读错误输出,它将对您有所帮助:
BufferedReader ereader =
new BufferedReader(new InputStreamReader(process.getErrorStream()));
String eline = "";
while ((eline = ereader.readLine())!= null) {
output.append("ERROR: " + eline + "\n");
MySQL需要将“ source myscript.txt”作为单个参数。 当您将其与命令的其余部分放在字符串中时,Java会将其作为两个参数传递,这是不可避免的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.