【发布时间】:2016-12-23 05:22:20
【问题描述】:
在使用 Python 很长一段时间后,我最近回到了 Java,我正在努力再次适应它。我想要的第一个项目是制作一个可以(首先)登录的小型应用程序。我正在使用 mySQL 来保存用户名和密码的数据库。
到目前为止,我已经使用 Java 的 swing GUI 制作了一个弹出框,询问登录信息。输入的用户名和密码将根据 SQL 表中的用户名和密码进行测试。
问题是我正在使用 while 循环来测试 SQL 表的输入。因此,输入的信息会根据 SQL 表创建的 Hashmap 中的每个键和值进行检查。
我不确定如何从 while 循环和 SQL 的 try 语句中“提取”我的变量 g(Hashmap),以便在它被来自 mySQL 的数据填充后使用它。
我正在使用 Eclipse (Java Neon)。
我不知道如何“提取”哈希图,当我尝试让 while 循环或 try 语句返回哈希图时,Eclipse 告诉我 void 方法无法返回值(很明显)。但是,我无法将返回类型从 void 更改为 HashMap)String, String>,因为“返回类型与 ActionListener.actionPerformed(ActionEvent e) 不兼容”和“实现了 java.awt.event.ActionListener.actionPerformed”。
这是我的代码:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
public class Hello0 extends JFrame {
private static final long serialVersionUID = 1487932324102279819L;
public static void main(String[] args) {
JFrame frame = new JFrame("Frame Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 200);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Username");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10, 50, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100, 50, 165, 25);
panel.add(passwordText);
JButton loginB = new JButton("Login");
loginB.setBounds(10, 80, 80, 25);
panel.add(loginB);
loginB.addActionListener(new ActionListener() {
public HashMap<String, String> actionPerformed(ActionEvent e) {
String username0 = "root";
String password = "javaSQLmy98";
try {
String url = "jdbc:mysql://localhost:3306/javabase?useSSL=false";
Connection connection = DriverManager.getConnection(url, username0, password);
PreparedStatement stmt0 = connection.prepareStatement("SELECT * from userids");
String pass0 = null;
String user1 = userText.getText().toString();
char[] pass1 = passwordText.getPassword();
pass0 = String.valueOf(pass1);
System.out.println(user1);
System.out.println(pass1);
ResultSet rs = stmt0.executeQuery();
rs = stmt0.executeQuery("SELECT * from userids ");
while (rs.next()) {
String user = rs.getString("username");
String pass = rs.getString("paswrd");
Map<String, String> g = new HashMap<>();
g.put(user, pass);
return g;
// This was the alternative: if(g.keySet().contains("Jacob") && g.values().contains("root")) {
/*This code below was originally outside the while loop but I could not
figure out how to make it work without it being inside, and accessible
to the hashmap g. Now it is being checked each time the while loop
is ran, with a new pair of usernames and passwords on each loop. */
if (user1.equals(user) && pass0.equals(pass)) {
System.out.println("Good!");
}
/*The problem here is that the checker IS inside the loop, so it
tests the input against each of the entries in the SQL table. */
else {
System.err.println("The username or password is incorrect.");
}
}
connection.close();
} catch (Exception e1) {
System.err.println(e1.getMessage());
}
}
});
}
}
干杯!
【问题讨论】:
-
为什么要将所有内容加载到 HashMap 中,而不是在 SQL 查询中仅包含 WHERE 子句?
-
你知道如何在 SQL 中编写查询来获取用户 ID 的单行,而不是遍历表中的所有行吗?
-
@RiaanNel 我不确定这会有什么帮助。 WHERE 关键字不只是充当过滤器吗?
-
@JimGarrison 我也不确定这会有什么帮助;但我相信你的意思是说要做这样的事情:
-
获取提交的用户名并将其与 SQL 表中的用户名进行比较。如果匹配,请同时检查密码。 ? (事后看来,我相信这也是@RiaanNel 的建议。)
标签: java mysql methods hashmap inner-classes