Created
August 22, 2018 09:19
-
-
Save luchtech/4318167ed074cfa2905ca6c7a9bd6bbb to your computer and use it in GitHub Desktop.
Login System with Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.Scanner; | |
public class Database { | |
/* | |
* Create a text file first named "Account.txt" and fill in some usernames and passwords | |
*/ | |
private String fileName; | |
private File myFile; | |
private FileReader fRead; | |
private FileWriter fWrite; | |
private Scanner scan; | |
public Database() { | |
fileName = "Account.txt"; | |
myFile = new File(fileName); | |
} | |
public Database(String fileName) { | |
this.fileName = fileName; | |
myFile = new File(fileName); | |
} | |
public void setFileName(String fileName) throws IOException { | |
this.fileName = fileName; | |
myFile = new File(fileName); | |
if(myFile.exists()) { | |
System.err.println("File already exist."); | |
} | |
else { | |
fWrite = new FileWriter(fileName); | |
} | |
} | |
public String getFileName() { return fileName; } | |
public boolean isValid(String un, String pwd) { | |
boolean valid = false; | |
try { | |
fRead = new FileReader(myFile); | |
scan = new Scanner(fRead); | |
while(scan.hasNext()) { | |
if(un.equals(scan.next()) && pwd.equals(scan.next())) { | |
valid = true;} | |
} | |
fRead.close(); | |
}catch (Exception e) { | |
System.err.println("Err 101: isValid+\n"+e.getMessage()); | |
} | |
return valid; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JTextField; | |
public class Login implements ActionListener { | |
JFrame myFrame; | |
JTextField txtUsername, txtPassword; | |
JLabel lblUsername, lblPassword; | |
JButton btnOk, btnCancel; | |
JPanel panel; | |
public Login() { | |
myFrame = new JFrame("Login"); | |
txtPassword = new JTextField("Username", 20); | |
txtUsername = new JTextField("Password", 20); | |
lblUsername = new JLabel("Username"); | |
lblPassword = new JLabel("Password"); | |
btnOk = new JButton("OK"); | |
btnCancel = new JButton("Cancel"); | |
panel = new JPanel(); | |
panel.add(lblUsername); | |
panel.add(txtUsername); | |
panel.add(lblPassword); | |
panel.add(txtPassword); | |
panel.add(btnOk); | |
panel.add(btnCancel); | |
btnOk.addActionListener(this); | |
btnCancel.addActionListener(this); | |
myFrame.add(panel); | |
myFrame.setVisible(true); | |
myFrame.setSize(350, 150); | |
myFrame.setLocationRelativeTo(null); | |
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE); | |
} | |
public static void main(String[] args) { | |
new Login(); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if(e.getSource().equals(btnOk)) { | |
Database db = new Database("Account.txt"); | |
String user = txtUsername.getText(); | |
String pass = txtPassword.getText(); | |
if(db.isValid(user, pass)) { | |
JOptionPane.showMessageDialog(myFrame, "Login Successful", "Success", JOptionPane.INFORMATION_MESSAGE); | |
myFrame.dispose(); | |
}else { | |
JOptionPane.showMessageDialog(myFrame, "Login Failure", "Failed", JOptionPane.ERROR_MESSAGE); | |
//myFrame.dispose(); | |
} | |
} | |
else { | |
System.exit(0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment