Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you really want to load the values from the database and check for a specific value from the user, then you could use a Binary search, otherwise you could use a SQL query as well. </p> <p>Below is an implementation (Binary Search) to find a value in an Array. </p> <pre><code>public class BinarySearch { public static void main(String[] a) { int[] numArray = {5,6,10,11,19,18,30,25,88,44,55,1,3}; Arrays.sort(numArray); // performing a binary search - here 100 is the element that you want // to search in your array System.out.println(searchElement(numArray, 100)); } private static int searchElement(int[] sortedArray, int element) { int first = 0; int upto = sortedArray.length; while (first &lt; upto) { int mid = (first + upto) / 2; // Compute mid point. if (element &lt; sortedArray[mid]) { upto = mid; // repeat search in bottom half. } else if (element &gt; sortedArray[mid]) { first = mid + 1; // Repeat search in top half. } else { return sortedArray[mid]; // Found it. You can return the position or the element } } return -1; // The element is not in the array } } </code></pre> <p>The database functionality</p> <pre><code>public class RetrieveValues { public static void main(String args[]) throws SQLException { Connection conn = null; Statement select = null; try { Class.forName("com.somejdbcvendor.TheirJdbcDriver"); conn = DriverManager.getConnection( "jdbc:somejdbcvendor:other data needed by some jdbc vendor", "myLogin", "myPassword"); select = conn.createStatement(); ResultSet result = select .executeQuery("SELECT Element FROM TestTable"); List&lt;Integer&gt; elementList = new ArrayList&lt;Integer&gt;(); while (result.next()) { // process results one row at a time elementList.add(result.getInt(1)); } // convert to int array convertIntegers(elementList); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { select.close(); conn.close(); } } public static int[] convertIntegers(List&lt;Integer&gt; elemIntegers) { int[] elements = new int[elemIntegers.size()]; for (int i=0; i &lt; elements.length; i++) { elements[i] = elemIntegers.get(i).intValue(); } return elements; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload