Salve ho un problema, il codice e giusto, non mi da errori, solo che quando avvio la form, e clicco sul pulsante, anche se inserisco bene password e username, mi da solo il messaggio Login incorrect.
package com.login;
import java.sql.*;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Login extends JFrame {
private JPanel contentPane;
private JTextField user;
private JPasswordField pass;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login() {
setTitle("Login");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("LOGIN");
lblNewLabel.setBounds(10, 11, 46, 14);
contentPane.add(lblNewLabel);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(10, 48, 66, 14);
contentPane.add(lblUsername);
user = new JTextField();
user.setBounds(86, 45, 86, 20);
contentPane.add(user);
user.setColumns(10);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(10, 92, 66, 14);
contentPane.add(lblPassword);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//ISTRUZIONI PER DATABASE
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","");
Statement stmt = con.createStatement();
String sql="Select * from login.identita where UserName='"+user.getText()+"'and Password='"+pass.getText().toString()+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
JOptionPane.showMessageDialog( null,"Login Successfully...");
else
JOptionPane.showMessageDialog(null,"Login Incorrect...");
con.close();
}catch(Exception ex) {System.out.print(ex);}
}
});
btnLogin.setBounds(10, 136, 89, 23);
contentPane.add(btnLogin);
pass = new JPasswordField();
pass.setBounds(86, 89, 86, 20);
contentPane.add(pass);
}
}