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

时间:2018-03-07 20:57:43

标签: 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'

1 个答案:

答案 0 :(得分:0)

你的shell理解输入重定向“&lt;”,但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将它作为两个参数传递,这是不可避免的。