我有一个带有登录屏幕的 swing jframe
,我正在尝试执行 phpmyadmin
数据库查询。它连接到数据库,但不允许我登录,只是无限显示“无效的用户名或密码”这是代码。
JPasswordField jpfpass;
JLabel jlabuser, jlabpass;
public RychlikSystemversion0(String name) {
super(name);
btnlogin = new JButton("Login");
btenreset = new JButton("Reset");
btnexit = new JButton("Exit");
jtfuser = new JTextField();
jpfpass = new JPasswordField();
jlabuser = new JLabel("Ussername");
jlabpass = new JLabel("Password");
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnlogin.addActionListener(this);
btenreset.addActionListener(this);
btnexit.addActionListener(this);
jlabuser.setBounds(10, 10, 120, 20);
jlabpass.setBounds(10, 30, 120, 20);
jtfuser.setBounds(140, 10, 300, 20);
jpfpass.setBounds(140, 30, 300, 20);
btnlogin.setBounds(140, 55, 100, 20);
btenreset.setBounds(240, 55, 100, 20);
btnexit.setBounds(340, 55, 100, 20);
this.add(jlabuser);
this.add(jlabpass);
this.add(jtfuser);
this.add(jpfpass);
this.add(btnlogin);
this.add(btenreset);
this.add(btnexit);
this.setSize(500, 300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
//String USERNAME = "root";
//String PASSWORD = "root ";
//String CONN_STRING = "jdbc:mysql://localhost::3307/osdesign";
Connection connection;
PreparedStatement ps;
try {
String dbName = "osdesign";
String dbUserName = "root";
String dbPassword = "";
//String u = jtfuser.getText();
//String p =jpfpass.getName();
String connectionString = "jdbc:mysql://localhost/" + dbName + "?user="
+ dbUserName + "&password=" + dbPassword
+ "&useUnicode=true&characterEncoding=UTF-8";
connection = DriverManager.getConnection(connectionString);
JOptionPane.showMessageDialog(null, "Connected!");
String sql = "SELECT * FROM `userlist` WHERE `UsrName` = ? AND `UsrPass` = ?";
ps = connection.prepareStatement(sql);
jtfuser.setText("Test");
jpfpass.setText("Test");
ps.setString(1, jtfuser.getText());
ps.setString(2, jpfpass.getName());
//JFrame panel = new JFrame();
ResultSet result = ps.executeQuery();
btnlogin.equals(result);
// panel.add(btnlogin);
// panel.add(jpfpass.getName(),btnlogin);
if (result.next()) {
JOptionPane.showMessageDialog(null, "Login Sucessful"); //Wont execute statement
} else {
JOptionPane.showMessageDialog(null, "Invalid user name or password"); //Always does this
}
} catch (SQLException ex) {
Logger.getLogger(RychlikSystemversion0.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
RychlikSystemversion0 ls = new RychlikSystemversion0("System login");
}
我已经尝试了几种方法,但仍然不允许我登录。有什么帮助吗?
最佳答案
问题在于:
jpfpass = new JPasswordField();
//[..]
ps.setString(2, jpfpass.getName());
方法getName()
,不会在字段中为您提供密码,而是为您提供组件的名称(在您的代码中甚至未设置)。
如果想从JPasswordField
获取密码,需要使用getPassword()
。此方法返回一个 char[]
,因此您需要更改代码以使用:
ps.setString(2, new String(jpfpass.getPassword()));
如果将来你遇到这样的问题,首先检查你的假设。在调试器中单步执行代码并检查这些值是否符合您的预期,或者只是添加一些相关值的调试 System.out.println(..)
。
关于Java 数据库连接。尝试通过sql登录jframe获取逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563428/