title: Embedded Database Login System (netbeans java)
body: <p>Alright, so i'm <strong>working on a database</strong> where <strong>the only criteria to login is a password.</strong> The database is to be only run from one computer in an offline state (Local) I read around and it pointed me in the direction of creating an embedded database. <strong>I believe im working with derby in Netbeans using Java</strong>. </p>
  
  <p>My table consists of a primary key as an account number, name and password.</p>
  
  <p><strong>I'm trying to</strong>: Compare inputted password with a "password" column in a table(Users) within the database, so Table = various user info + password column</p>
  
  <p><strong>My problem</strong>: It throws some errors when i try to access the database to compare the inputted password (That was in the passwordField) to the database password.</p>
  
  <p><strong>Errors</strong></p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at MainApplication.Login.loginButtMouseClicked(Login.java:112)
  at MainApplication.Login.access$000(Login.java:7)
  at MainApplication.Login$1.mouseClicked(Login.java:37)
  at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
  at java.awt.Component.processMouseEvent(Component.java:6528)
  at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
  at java.awt.Component.processEvent(Component.java:6290)
  at java.awt.Container.processEvent(Container.java:2234)
  at java.awt.Component.dispatchEventImpl(Component.java:4881)
  at java.awt.Container.dispatchEventImpl(Container.java:2292)
  at java.awt.Component.dispatchEvent(Component.java:4703)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
  at java.awt.Container.dispatchEventImpl(Container.java:2278)
  at java.awt.Window.dispatchEventImpl(Window.java:2750)
  at java.awt.Component.dispatchEvent(Component.java:4703)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
  at java.awt.EventQueue.access$500(EventQueue.java:97)
  at java.awt.EventQueue$3.run(EventQueue.java:709)
  at java.awt.EventQueue$3.run(EventQueue.java:703)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
  at java.awt.EventQueue$4.run(EventQueue.java:731)
  at java.awt.EventQueue$4.run(EventQueue.java:729)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  </code></pre>
  
  <p><strong>Here is my connection Helper class</strong></p>
  
  <pre><code>package MainApplication;
  
  import java.sql.*;
  import javax.swing.JOptionPane;
  
  public class DBConnect {
  
      public static Connection Connect() {
          Connection conn;
  
          try {
          String host = "jdbc:derby://localhost:1527/USERS";
          String uName = "test";
          String uPass = "test";
          conn = DriverManager.getConnection(host, uName, uPass);
          JOptionPane.showMessageDialog(null, "Database Connected");
          return conn;
          }
  
          catch (SQLException err){
              JOptionPane.showMessageDialog(null ,err);
              return null;
          }
  
      }
  }
  </code></pre>
  
  <p><strong>And this is my Login Base code without swing components</strong></p>
  
  <pre><code>package MainApplication;
  
  import java.sql.*;
  import javax.swing.*;
  
  public class Login extends javax.swing.JFrame {
      Connection conn;
      PreparedStatement stmt;
      ResultSet rs;
  
  //Initializer
      public Login() {
          initComponents();
          DBConnect.Connect();
      }
  </code></pre>
  
  <p><strong>Login Button</strong></p>
  
  <pre><code>    private void loginButtMouseClicked(java.awt.event.MouseEvent evt) {                                       
      String pass = passText.getText();
      try {
          stmt = (PreparedStatement) conn.createStatement();
          String SQL = "select * from users where password  ='" + pass + "'";
          rs = stmt.executeQuery(SQL);
          if (rs.next()){
              int id = rs.getInt("ACCOUNTNUMBER");
              String firstName = rs.getString("firstName");
              String lastName = rs.getString("lastName");
              String accInfo = id + " " + firstName + ", " + lastName;
              System.out.println(accInfo);
          }    
      }
      catch (SQLException e) {
              JOptionPane.showMessageDialog(null, e);
      }
  }
  </code></pre>
  
  <p>Side note: I read that hashing or salting would probably be a good idea for security... but I would probably have to be able to connect before implementing it? Also heard that using a prepared statement is more secured which is why that's there.</p>
  
  <p>Thanks for your time :) </p>