I am getting hardware data from cmd using a process builder in java.
//get info from cmd
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("cmd.exe", "/c", "systeminfo ");
try {
Process process = processBuilder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
//write the output to the variables
String line;
//a loop which ensures all the data is read in
while ((line = reader.readLine()) != null) {
hardwareInfo.add(line);//hardwareInfo is an arraylist i currently save all this information to
}
This returns all the relevant information and more. my output looks like the following:
[, Host Name: LJDESKTOP, OS Name: Microsoft Windows 10 Education, OS Version: 10.0.18363 N/A Build 18363
etc...
I want to add some of these fields into an SQL database based on their names(e.g. Host Name: - yes, OS Name: - No). The SQL connection is set up I just need to find the best way to save these varibles so I can insert them straight into my database.
So how do I get rid of the Host Name:
and still enter LJDESKTOP
into my database and the same principle for the rest of the information I get from the cmd command.
I am also trying to consider efficiency, I want this to be as computationally "light" as possible but this isn't essential.
What I have tried so far:
I have tried splitting the string at the ":" for each varible and trimming. This gives me exactly the information I need but then I can't save it to individual variables. This is because the bit I trimmed is how I determine what my varibles are. (Could I potentially add the trim function to my setter?)
I have tried:
while ((line = reader.readLine()) != null) {
if (line.startsWith("Host Name:")) { setHostname(line.replaceFirst("Host Name: ", ""));}
the if statements are repeated for each variable, however everytime this adds each variable to my array everytime it goes through the while loop.