Search code examples
javasqlcmdprocessbuilderdataformat

Formatting data and inputting to a database in Java


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:

  1. 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?)

  2. 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.


Solution

  • You can try it like this:

    ...
    final Map<String,String> inputValues = new HashMap<>();
    
    //a loop which ensures all the data is read in
    while ((line = reader.readLine()) != null) {
      // Read each line as key and value into a the inputValues map
    
      final String[] pieces = line.split(":",2); // only split at the first ':'!
    
      // Was a ':' found, e.g. the string split into two pieces?
      if ( pieces.length > 1 ) {
    
        String key = pieces[0]; // e.g. "Host Name"
        String value = pieces[1]; // e.g. "                 LJDESKTOP"
    
        value = value.trim(); // remove leading/trailing whitespaces from value
    
        inputValues.put(key,value); // Store key+value to map.
      }
    }
    
    // Now we can access the input values by key, e.g.:
    
    String hostName = inputValues.get("Host Name");
    String osName = inputValues.get("OS Name");
    
    // To do: Do something with the values above, e.g. send to DB...