swing - trying to connect the login and database connection classes so that i can execute sql statements in the login.java file -


my code in different folders , different packages. having difficulty connecting login.java database_connection.java file.

i want form , database connection class separate still able access database.

the login.java file java swing file. view. database_connection.java supposed controller. similar in web development. want have connection details in separate class front end.

login.java:

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;     }      /**      * method called within constructor initialize form.      * warning: not modify code. content of method      * regenerated 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 handling code here:         try         {             //the code below capture text input login frame input fields             string username = form_username.gettext();             string password = form_password.gettext();              //flag used validdate login credentials             int correct_credentials;              //make connection database conncection class             database_connection.connectdb();              //perform sql commands             string query1 = "select username,password users username ='"+username+"'and password ='"+password+"'";              preparedstatement = connect.preparestatement(query1);         }             catch(exception e)         {          }          //check if field equals blank , throw error message         if (username.equals("")|| password.equals(""))         {                 joptionpane.showmessagedialog(null," name or password empty!! ","error",joptionpane.error_message);         }         else if (username.endswith(query1) && password.endswith(query1))         {             joptionpane.showmessagedialog(null," ");         }         else         {             joptionpane.showmessagedialog(null,"your login credentials invalid!");         }      }                                                     private void btn_login_cancelactionperformed(java.awt.event.actionevent evt) {                                                          // todo add handling code here:         close();     }                                                      /**      * @param args command line arguments      */     public static void main(string args[])      {         //conn = sqlite_database_connection.connectdb();         /* set nimbus , feel */         //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) ">         /* if nimbus (introduced in java se 6) not available, stay default , feel.          * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html           */         try {             (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 , display form */         java.awt.eventqueue.invokelater(new runnable()          {             @override             public void run()              {                 new login().setvisible(true);             }         });     }      // variables declaration - 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 statement use close current window         windowevent winclosing = new windowevent(this,windowevent.window_closing);         toolkit.getdefaulttoolkit().getsystemeventqueue().postevent(winclosing);     }  } 

database_connectioin.java:

package controller;  import java.sql.*; import javax.swing.joptionpane;      public class database_connection  {     private connection connect;      public static connection connectdb()     {         // url 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 connect      */     public connection getconnect()      {         return connect;     }      /**      * @param connect connect set      */     public void setconnect(connection connect)      {         this.connect = connect;     } } 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -