有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将Access数据库中的数据加载到int数组中?

我正在寻找一种方法来验证用户输入的数字是否与数据库中存储的数据相符。例如,如果用户输入“5132”,它会将特定列中的所有数据加载到数组中,然后我会循环该数据并将输入与之进行比较,希望找到匹配项

简单地说:如何将数据库(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;
 }
}

共 (1) 个答案

  1. # 1 楼答案

    如果确实希望从数据库加载值并检查用户的特定值,则可以使用二进制搜索,否则也可以使用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;
        }
    }