gpt4 book ai didi

java - 从java程序运行时mysql命令打印出帮助菜单

转载 作者:行者123 更新时间:2023-11-29 17:52:51 25 4
gpt4 key购买 nike

我的 mysql 命令是从 java 程序内部运行的,并打印出帮助菜单,但是当我在终端中运行完全相同的命令时,它工作正常。不太确定为什么。

String shellCommand = "mysql -u root < " + destinationDirectory+"/"+ filename;
executeCommand(shellCommand);

这是执行命令的方法:

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 命令,而不是像最初那样使用 hibernate,但仍然无济于事。等待是为了给恢复一些时间,以到达由于锁定而挂起的位置,然后关闭程序,以便释放锁定和数据库:

// 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 将它作为两个参数传递,这是不可避免的。

关于java - 从java程序运行时mysql命令打印出帮助菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49161077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com