Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package View;
- import Controller.Database_Connection;
- import java.awt.Toolkit;
- import java.awt.event.WindowEvent;
- import javax.swing.JOptionPane;
- public class Login extends javax.swing.JFrame
- {
- /*** Creates new form Login */
- public Login()
- {
- initComponents();
- //static Connection Conn = null;
- //PreparedStatement Query = null;
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jPanel1 = new javax.swing.JPanel();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- form_username = new javax.swing.JTextField();
- form_password = new javax.swing.JPasswordField();
- btn_login_cancel = new javax.swing.JToggleButton();
- btn_login_enter = new javax.swing.JToggleButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setText("LOGIN ");
- jLabel2.setText("USERNAME: ");
- jLabel3.setText("PASSWORD:");
- btn_login_cancel.setText("CANCEL");
- btn_login_cancel.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btn_login_cancelActionPerformed(evt);
- }
- });
- btn_login_enter.setText("ENTER");
- btn_login_enter.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btn_login_enterActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(200, 200, 200)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(form_username))
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addComponent(btn_login_cancel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btn_login_enter))
- .addComponent(form_password, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))))
- .addGap(84, 84, 84))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1)
- .addGap(11, 11, 11)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(form_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel3)
- .addComponent(form_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(btn_login_cancel)
- .addComponent(btn_login_enter))
- .addContainerGap(46, Short.MAX_VALUE))
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void btn_login_enterActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- try
- {
- //The code below is to capture the text input from the login frame input fields
- String username = form_username.getText();
- String password = form_password.getText();
- //Flag to be used to validdate login credentials
- int correct_credentials;
- //Make a connection to the database conncection class
- Database_Connection.ConnectDB();
- //Perform the SQL Commands
- String Query1 = "SELECT Username,Password FROM Users WHERE Username ='"+username+"'AND Password ='"+password+"'";
- PreparedStatement = connect.prepareStatement(Query1);
- }
- catch(Exception e)
- {
- }
- //Check if field equals to blank and throw error message
- if (username.equals("")|| password.equals(""))
- {
- JOptionPane.showMessageDialog(null," Name or Password is empty!! ","Error",JOptionPane.ERROR_MESSAGE);
- }
- else if (username.endsWith(Query1) && password.endsWith(Query1))
- {
- JOptionPane.showMessageDialog(null," ");
- }
- else
- {
- JOptionPane.showMessageDialog(null,"Your Login Credentials are Invalid!");
- }
- }
- private void btn_login_cancelActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- close();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[])
- {
- //Conn = Sqlite_Database_Connection.ConnectDB();
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable()
- {
- @Override
- public void run()
- {
- new Login().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JToggleButton btn_login_cancel;
- private javax.swing.JToggleButton btn_login_enter;
- private javax.swing.JPasswordField form_password;
- private javax.swing.JTextField form_username;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JPanel jPanel1;
- // End of variables declaration
- private void close()
- {
- //This is a statement to use to close the current window
- WindowEvent winClosing = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
- Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosing);
- }
- }
- package Controller;
- import java.sql.*;
- import javax.swing.JOptionPane;
- public class Database_Connection
- {
- private Connection connect;
- public static Connection ConnectDB()
- {
- // The URL to the Java Derby Server
- String connectionURL = "jdbc:derby://localhost:1739`enter code here`/TEST_DB";
- try
- {
- Connection connect;
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- connect = DriverManager.getConnection(connectionURL,"###","####");
- JOptionPane.showMessageDialog(null, "Database Connection Successful");
- return connect;
- }
- catch(ClassNotFoundException | SQLException e)
- {
- JOptionPane.showMessageDialog(null, "Database Connection Not Successful");
- }
- return null;
- }
- public Database_Connection() {
- this.connect = null;
- }
- /**
- * @return the connect
- */
- public Connection getConnect()
- {
- return connect;
- }
- /**
- * @param connect the connect to set
- */
- public void setConnect(Connection connect)
- {
- this.connect = connect;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement