Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Java in General
help with running dos commands using java
Mohan Mehra
Ranch Hand
Posts: 74
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.io.*; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; public class Dos { static Connection conn = null; Statement st; public static void main(String[] args) { try { String[] command = new String[4]; command[0] = "cmd"; command[1] = "/C"; command[2] = "ping"; command[3] = "10.1.1.181"; conn = DbConnection.openConnection(); Process p = Runtime.getRuntime().exec(command); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // read the output from the command String s = null; System.out.println("Here is the standard output of the command:\n"); while ((s = stdInput.readLine()) != null) { System.out.println(s); String insDisQuery = "INSERT INTO nnm_health_status(TIME,ping_response) VALUES (NOW(),?)"; java.sql.PreparedStatement stmt = conn.prepareStatement(insDisQuery); stmt.setString(1,s.toString()); conn.setAutoCommit(false); stmt.executeUpdate(); conn.setAutoCommit(true); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } System.out.println("I am In try"); } catch(Exception e){ System.out.println("I am In catch"); } } }
This program will print 11 lines in the DB whether its response is positive or negative. i just want it to cut down to one line. for example when pinging is success insert a
word
caled success in DB and failure when not.
Mohan Mehra
Ranch Hand
Posts: 74
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
got it
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Executing dir from Java
Having trouble in running a unix command and getting the output
java program run external command on Unix
executing linux commands through java socket
how to execute commands from java
More...