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

标签 java mysql shell

我的 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/

相关文章:

java - Eclipse 中的自动导入

java - 删除 Android 列表中的最后一项

java - 短信广播接收器未注册

MySql大数据操作(超3000万条数据记录)——最优方式

java - 如何使用 JSOUP 从网站中提取正文并排除存档和链接到其他网页

mysql - 如何将Mamp的mysql数据文件夹同步到Dropbox

php - 数据库和 Web 服务器位于不同的提供商机器上

Unix shell 代码,可移植性足以在所有 shell 上运行

linux - 如何在linux终端上只打印txt文件?

linux - 后台作业job id后面的后缀 "+"和 "-"是什么意思?