• Post Reply Bookmark Topic Watch Topic
  • New Topic

try to run sql script

 
Pranoj Ashtamkar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class RealTimeMap2 {
public static void main(String argv[]) {
try {
System.out.println("going to process");

String username = "root";
String password = "root";
String database = "test_db";

//Successfully executed
String[] executeCmd = new String[]{"mysql","test_db","--user=root","--password=root","-e"," source d:/test_query.sql" };

Runtime.getRuntime().exec(executeCmd);

// but if i want to pass this way with user varible its not process that file, nor throwing any runtime exception
//String executeCmd = "mysql "+database+" --user="+username+" --password="+password+" -e source d:/test_query.sql";

System.out.println("---> cmd : "+executeCmd);


//Runtime.getRuntime().exec(new String[]{"mysql",database,"--user="+username,"--password="+password,"-e"," source d:/test_query.sql"});

System.out.println("---> done");

} catch (Exception err) {
System.out.println("---> error: "+err);
}
}

}


any body help please.
 
Campbell Ritchie
Marshal
Posts: 53207
124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code button.

This may not be an SQL problem, but a problem with Runtime.exec(). You must empty the two streams associated with the Runtime, and you must wait until the process has completed before trying anything else. Look at the ProcessBuilder class, which can make things slightly easier by combining the two streams, but you really must read Michael Daconta's classic article "When Runtime.exec() won't" before going anywhere near Runtime.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!