title: Setting up JDBC to check for correct username and password
body: <p>Trying to set up a JDBC that checks a database for a matching username and password, and they when the login button is pressed if matching the user is granted access, I've got my current code here, but I'm unsure what is missing when I launch the program it seems like its not checking the database for the correct information.</p>
  <p>Updated:</p>
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.sql.*;
  
  public class NewClass extends JFrame {
  
      private JTextField jtfUsername, jtfPassword;
      private JButton backButton, loginButton;
      private JMenuItem jmiLogin, jmiBack, jmiHelp, jmiAbout;
  
      NewClass() {
          //create menu bar
          JMenuBar jmb = new JMenuBar();
  
          //set menu bar to the applet
          setJMenuBar(jmb);
  
          //add menu &quot;operation&quot; to menu bar
          JMenu optionsMenu = new JMenu(&quot;Options&quot;);
          optionsMenu.setMnemonic('O');
          jmb.add(optionsMenu);
  
          //add menu &quot;help&quot;
          JMenu helpMenu = new JMenu(&quot;Help&quot;);
          helpMenu.setMnemonic('H');
          helpMenu.add(jmiAbout = new JMenuItem(&quot;About&quot;, 'A'));
          jmb.add(helpMenu);
  
          //add menu items with mnemonics to menu &quot;options&quot;
          optionsMenu.add(jmiLogin = new JMenuItem(&quot;Login&quot;, 'L'));
          optionsMenu.addSeparator();
          optionsMenu.add(jmiBack = new JMenuItem(&quot;Back&quot;, 'B'));
  
          //panel p1 to holds text fields
          JPanel p1 = new JPanel(new GridLayout(2, 2));
          p1.add(new JLabel(&quot;Username&quot;));
          p1.add(jtfUsername = new JTextField(15));
          p1.add(new JLabel(&quot;Password&quot;));
          p1.add(jtfPassword = new JPasswordField(15));
  
          //panel p2 to holds buttons
          JPanel p2 = new JPanel(new FlowLayout());
          p2.add(backButton = new JButton(&quot;Back&quot;));
          p2.add(loginButton = new JButton(&quot;Login&quot;));
  
          //Panel with image??????
  
          //add panels to frame
          JPanel panel = new JPanel(new GridLayout(2, 1));
          panel.add(p1, BorderLayout.CENTER);
          panel.add(p2, BorderLayout.SOUTH);
          add(panel, BorderLayout.CENTER);
          setTitle(&quot;Main Page&quot;);
  
  
          //listners for exit menuitem and button
          jmiBack.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  Welcome welcome = new Welcome();
                  welcome.setVisible(true);
                  welcome.setSize(500, 500);
                  welcome.setLocationRelativeTo(null);
                  registerInterface regFace = new registerInterface();
                  regFace.setVisible(false);
                  NewClass.this.dispose();
                  NewClass.this.setVisible(false);
              }
          });
  
          backButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  Welcome welcome = new Welcome();
                  welcome.setVisible(true);
                  welcome.setSize(500, 500);
                  welcome.setLocationRelativeTo(null);
                  registerInterface regFace = new registerInterface();
                  regFace.setVisible(false);
                  NewClass.this.dispose();
                  NewClass.this.setVisible(false);
              }
          });
  
          //listner for about menuitem
          jmiAbout.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  JOptionPane.showMessageDialog(null,
                          &quot;This is the login panel&quot;
                          + &quot;\n Assignment for University&quot;,
                          &quot;About&quot;, JOptionPane.INFORMATION_MESSAGE);
              }
          });
  
          //action listeners for Login in button and menu item
          loginButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  try {
                      usernamecheck.checkLogin(jtfUsername.getText(), jtfPassword.getText()); {
                      System.out.println(&quot;User is validated&quot;);
                 }
                  } catch (SQLException se) {
                  }
                  MainMenu mainmenu = new MainMenu();
                  mainmenu.setVisible(true);
                  mainmenu.setSize(500, 500);
                  mainmenu.setLocationRelativeTo(null);
                  registerInterface regFace = new registerInterface();
                  regFace.setVisible(false);
                  NewClass.this.dispose();
                  NewClass.this.setVisible(false);
              }
          });
  
          jmiLogin.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  MainMenu mainmenu = new MainMenu();
                  mainmenu.setVisible(true);
                  mainmenu.setSize(500, 500);
                  mainmenu.setLocationRelativeTo(null);
                  registerInterface regFace = new registerInterface();
                  regFace.setVisible(false);
                  NewClass.this.dispose();
                  NewClass.this.setVisible(false);
              }
          });
      }
  
      public static void main(String arg[]) {
          log frame = new log();
          frame.setSize(500, 500);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }
  }
  
  class usernamecheck {
  
      static final String DATABASE_URL = &quot;jdbc:mysql://localhost:3306/test&quot;;
      static final String USERNAME = &quot;root&quot;;
      static final String PASSWORD = &quot;root&quot;;
  
     // launch the application
      public static boolean checkLogin(String username, String password)
              throws SQLException {
          System.out.print(&quot;dfdF&quot;);
  
          Connection connection = null; // manages connection
          PreparedStatement pt = null; // manages prepared statement
  
          // connect to database usernames and query database
         
  </code></pre>
  <p>try {</p>
  <pre><code>        // establish connection to database
          Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance();
          Connection con = DriverManager.getConnection(DATABASE_URL, &quot;root&quot;, &quot;root&quot;);
  
          // query database
          pt = con.prepareStatement(&quot;select userName,password from test.person where userName=?&quot;);
  
          // process query results
          pt.setString(1, username);
          ResultSet rs = pt.executeQuery();
          String orgUname = &quot;&quot;, orPass = &quot;&quot;;
          while (rs.next()) {
              orgUname = rs.getString(&quot;userName&quot;);
              orPass = rs.getString(&quot;password&quot;);
          } //end while
          if (orPass.equals(password)) {
              //do something
              return true;
          } else {
              //do something
          }
      }//end try
      catch (Exception e) {
      } //end catch  
      return false;
  } //end main
  </code></pre>
  <p>}</p>