gpt4 book ai didi

Java SQL - 未正确读取 UserInput(用户名和密码)

转载 作者:行者123 更新时间:2023-11-29 20:15:34 25 4
gpt4 key购买 nike

我们显然被困在登录程序中,因为它不读取用户名和密码。即使用户名和密码不正确,它也会运行。

实验工作代码(即使登录详细信息不正确也会登录):

JButton btnLogin = new JButton("Log-In");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {



Connection conn;
java.sql.PreparedStatement ps;

try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tupaness","ivanaldwin","frankscanteen");
Statement mystmt = conn.createStatement();
ps = conn.prepareStatement("SELECT * FROM 'admin' WHERE 'uname' = ? AND 'pass' = ?");


String user = uname.getText(); // Collecting the input
char[] password = pass.getPassword(); // Collecting the input
String pwd = String.valueOf(pass); // converting from array to string
if(validate_login(user,pwd)){

/* ps.setString(1, uname.getText());
ps.setString(2, String.valueOf(pass.getPassword()));
ResultSet result = ps.executeQuery();



if(result.next()){
JOptionPane.showMessageDialog(null, "Login Success");

TupanessHome th = new TupanessHome();
}
*/
}
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "An Error has been Detected: \n" + e);
}TupanessHome th = new TupanessHome();
}

private boolean validate_login(String user, String pwd) {

try{
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tupaness","ivanaldwin","frankscanteen");
PreparedStatement pst = conn.prepareStatement("Select * from admin where uname=? and pass=?");
pst.setString(1, user);
pst.setString(2, pwd);
ResultSet rs = pst.executeQuery();
if(rs.next()){
return true;
}
else
return false;
}
catch(Exception e){
e.printStackTrace();
return false;
}



}
});
btnLogin.setBounds(65, 117, 89, 23);
login.getContentPane().add(btnLogin);

应该是代码(不能工作,说我们有某种语法错误)

    JButton btnLogin = new JButton("Log-In");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Connection conn;
java.sql.PreparedStatement ps;

try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tupaness","ivanaldwin","frankscanteen");
ps = conn.prepareStatement("SELECT * FROM 'admin' WHERE 'uname' = ? AND 'pass' = ?");
ps.setString(1, uname.getText());
ps.setString(2, String.valueOf(pass.getPassword()));
ResultSet result = ps.executeQuery();

if(result.next()){
JOptionPane.showMessageDialog(null, "Login Success");

TupanessHome th = new TupanessHome();
}

}catch(Exception e) {
JOptionPane.showMessageDialog(null, "An Error has been Detected: \n" + e);
}
}
});
btnLogin.setBounds(65, 117, 89, 23);
login.getContentPane().add(btnLogin);

编辑

我们设法修复了该列中的错误,但仍然无法登录

  ps = conn.prepareStatement("SELECT * FROM admin WHERE 'uname' = ? AND 'pass' = ?");

(该语句来自“应该是代码”)

最佳答案

"SELECT * FROM 'admin' WHERE 'uname' = ? AND 'pass' = ?"

应该是

"SELECT * FROM `admin` WHERE `uname` = ? AND `pass` = ?"

甚至

"SELECT * FROM admin WHERE uname = ? AND pass = ?"

关于Java SQL - 未正确读取 UserInput(用户名和密码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39804151/

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