2012-08-10 85 views
0

我正在尋找一種方法來驗證用戶輸入的數字是否與存儲在數據庫中的數據一致。例如。如果用戶輸入'5132',它會將來自特定列的所有數據加載到數組中,然後循環該數據並將輸入與它進行比較以尋找匹配。如何將數據從Access數據庫加載到int數組中?

簡單說明:如何將數據從數據庫(resultset)解析到int數組中?

什麼我試着讓在

String s = JOptionPane.showInputDialog(...); 
int intS = Integer.parseInt(s); 
boolean correct = false; 

Get database info 
count++; //for every data entry 

int TagNumber [] = new int [count]; 

for (int i=0;i<count;i++) { 
if (intS == TagNumber[i]) { 
    correct =true; 
    break; 
} 
} 
+0

谷歌 - Access數據庫JDBC。 – 2012-08-10 14:57:48

+0

@BheshGurung我明白如何連接和獲取數據到一個結果集,我只是不知道如何得到這個解析到一個數組 – AceFire6 2012-08-10 15:23:36

回答

2

如果你真的想從數據庫加載值和用戶檢查特定的值,那麼你可以使用二進制搜索一個簡單的例子,否則你也可以使用SQL查詢。

下面是一個實現(二進制搜索)來查找數組中的值。

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 < upto) { 
      int mid = (first + upto)/2; // Compute mid point. 
      if (element < sortedArray[mid]) { 
       upto = mid;  // repeat search in bottom half. 
      } else if (element > 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 
    } 
} 

的數據庫功能

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<Integer> elementList = new ArrayList<Integer>(); 
      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<Integer> elemIntegers) 
    { 
     int[] elements = new int[elemIntegers.size()]; 
     for (int i=0; i < elements.length; i++) 
     { 
      elements[i] = elemIntegers.get(i).intValue(); 
     } 
     return elements; 
    } 
} 
+0

Im通過搜索數組(我想)我很好尋找一種方法來獲得數據從數據庫轉換爲數組。 – AceFire6 2012-08-10 15:20:38

+0

好的。我在編輯我的答案,你也可以看到數據庫功能的實現。 – likeToCode 2012-08-10 15:58:10

+0

非常感謝你 – AceFire6 2012-08-10 16:39:23

相關問題