I am building a login system in Java using mysql database, the passwords in the database are encrypted. So when I put a plain text password manually in the database the system logs me in, but it fails to read the ones which are encrypted. I would like someone to help me.
Here is what I tried:
package techtight;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.management.Query.and;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class myLogin extends JFrame {
public void contentMethod(){
JFrame myframe = new JFrame("login Techtight");
myframe.setLayout(null);
myframe.setSize(200,170);
myframe.setVisible(true);
JTextField txtuser = new JTextField();
JPasswordField txtpass = new JPasswordField();
JButton btnLoging = new JButton("Login");
txtuser.setBounds(40, 20, 120, 25);
txtpass.setBounds(40, 50, 120, 25);
btnLoging.setBounds(40,80, 80, 30);
myframe.add(txtuser);
myframe.add(txtpass);
myframe.add(btnLoging);
///How to code the button
btnLoging.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// connect to the db
try {
Connection conn;
String dbuser = "root";
String dbpassw = "12345";
String databasename = "taxi";
String url = "jdbc:mysql://localhost/taxi";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,dbuser,dbpassw);
Statement st = conn.createStatement();
String username = txtuser.getText();
char[] pass = txtpass.getPassword();
String password = new String (pass);
//String password = String.copyValueOf(pass); // converting from array to string
//String password = txtpass.getText();
ResultSet rs = st.executeQuery("SELECT * FROM users where username='" + username + "' and password='" + password + "'");
if(rs.next()){
JOptionPane.showMessageDialog(null, "Login Succesful");
myframe.dispose();// make login for disapear
}else {
JOptionPane.showMessageDialog(null, "Login Failed ");
new Techtight();
}
//JOptionPane.showMessageDialog(null, "okay");
} catch (SQLException ex) {
Logger.getLogger(myLogin.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(myLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
}); //
}
public static void main (String args[]){
myLogin m = new myLogin();
m.contentMethod();
}
}