title: Why jdbc return null in first time when execute the query?
body: <p>The below code is working with <code>sql-server db</code>  &amp; <code>ZXingScannerView.ResultHandler</code> barcode scanner.</p>
  
  <p>When I scan the barcode for the first time I get <code>null</code> from executed db query and  I have to scan the barcode twice to get the result. I checked the <em>barcode scanner lib</em> and everything is working fine and the same to <code>jdbc</code>.</p>
  
  <p>Code for <strong>SqlServerConnection.java</strong>:</p>
  
  <pre><code>public class SqlServerConnection {
  
  @SuppressLint("NewApi")
  public Connection SqlConnection(String server, String database, String user, String password)
  {
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
      Connection connection = null;
      String ConnectionURL = null;
  
      try {
          Class.forName("net.sourceforge.jtds.jdbc.Driver");
          ConnectionURL = "jdbc:jtds:sqlserver://" + server + ":1433/" +  database + ";user=" + user+ ";password=" + password + ";";
          connection = DriverManager.getConnection(ConnectionURL);
      }
      catch (SQLException se){Log.e("error here 1 : ", se.getMessage());}
      catch (ClassNotFoundException e){Log.e("error here 2 : ", e.getMessage());}
      catch (Exception e){Log.e("error here 3 : ", e.getMessage());}
  
      return connection;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Code for <strong>Search.java</strong>:</p>
  
  <pre><code>public class Search extends AsyncTask&lt;String, String, String&gt; {
  
      // Declaring connection variables
      Connection con;
      public static String ScannerResult;
      public static String z = "";
      public static Boolean isSuccess = false;
  
      SqlServerConnection sqlsrverconnection = new SqlServerConnection ();
  
      @Override
      protected String doInBackground(String... params) {
  
          if (ScannerResult.trim().equals(""))
              z = "Please enter User Id and Password";
          else {
              con = sqlsrverconnection.SqlConnection("mySqlServer", "mydb", "myUser", "Password");
              try {
  
                  if (con == null) {
                      z = "Error in connection with SQL server";
                  } else {
                      String query = "SELECT * FROM [EmpDevicesList] WHERE Record='" + ScannerResult + "';";
                      Statement stmt = con.createStatement();
                      ResultSet rs = stmt.executeQuery(query);
  
                      if (rs.next()) {
                          Device.setDeviceTag(rs.getString("Record"));
                          Device.setDeviceType(rs.getString("Type"));
                          Device.setDeviceBrand(rs.getString("Brand"));
                          Device.setDeviceModel(rs.getString("Model"));
                          isSuccess = true;
                      } else {
                          z = "Your search - " + ScannerResult + " - did not match any record.\n"
                          isSuccess = false;
                      }
                  }
              } catch (Exception ex) {
                  isSuccess = false;
                  z = ex.getMessage();
              }
          }
          return z;
      }
  
  }
  </code></pre>
  
  <p>Code for <strong>ScannActivity.java</strong>:</p>
  
  <pre><code>public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
  
      MediaTools mediaTools = new MediaTools();
      private ZXingScannerView scannerView;
      Search search = new Search();
  
      @Override
      public void onCreate(Bundle state) {
          super.onCreate(state);
          scannerView = new ZXingScannerView(this);    
          setContentView(scannerView);                 
      }
  
      @Override
      public void onResume() {
          super.onResume();
          scannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
          scannerView.startCamera();          // Start camera on resume
          scannerView.setAutoFocus(true);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          scannerView.stopCamera();           // Stop camera on pause
      }
  
      @Override
      public void handleResult(Result rawResult) {
          Search.ScannerResult = rawResult.getText();
          search.execute("");
          mediaTools.Beep();
  
          String s1 = Device.getDeviceTag();
          String s2 = Device.getDeviceType();
          String s3 = Device.getDeviceBrand();
          String s4 = Device.getDeviceModel();
          Toast.makeText(getBaseContext(),
                  "Tag   : " + s1
                          + "\nType  : " + s2
                          + "\nBrand : " + s3
                          + "\nModel : " + s4,
                  Toast.LENGTH_LONG).show();
  
          finish();
      }
  }
  </code></pre>
  
  <p><strong>Table Schema</strong></p>
  
  <pre><code>Record | Type  | Brand | Modle
  --------------------------------
   1002  |  PC   |  HP   |  X-1
   1003  |  PC   |  Dell |  788
  </code></pre>