gpt4 book ai didi

java - 查询 MySQL 数据库并将整列存储在 ArrayList 中

转载 作者:行者123 更新时间:2023-11-29 19:45:00 24 4
gpt4 key购买 nike

我正在制作一个简单的登录表单,我将在其中查询数据库中的用户和密码并保存所有内容(这可能不是最好/最安全的方法,但我仍在学习,我不需要高级代码)在数组列表中,然后可以使用它们的索引来比较它们(如果索引相同,则它将关联为同一个帐户)。我可以让查询工作,但我无法将行值逐一存储在列表中...使用我的代码,我可以使用第一个帐户登录,但其他帐户不起作用。有人可以解释一下如何做到这一点(尽量不要立即提供现成的代码,我想学习)。

最佳答案

嘿,如果我没记错的话,你看起来是一个 java 新手,这里是你需要遵循的一系列步骤,从 Mysql 数据库中提取所有用户及其密码。

Step-1  Establish the connection with your databases and table.
Step-2 Use the ResultSet interface of JDBC API to query the database for users and thier passwords.
Step-3 Get the users from ResultSet and put it into one arrayList called User using the add method of arraylist as:- arraylistobj.add(user).
Steps-4 Now Get the passwords from resultSet into second arrayList called Passwords using the add method arraylistobj.add(password).
Steps-5 As you mentioned you want to compare arraylist objects here is a Sample code:

for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < list.size(); j++) {
// compare list.get(i) and list.get(j)
}
}

Instead of using two arraylist's one for storing users from database and other for storing passwords,
it is much better to use Maps if you are familiar with as the store data in key value pair,
in this example user could be the key and passwords could be the values associated with the keys.

关于java - 查询 MySQL 数据库并将整列存储在 ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41079913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com