# Solved: Java login with Java Derby



## ahamed (Dec 8, 2014)

*This is my database connection class*


```
import java.sql.*;

public class connectWithDB {
  

public static void DBconnection(){
    
  Connection conn = null;
  
  String url = "jdbc:derby://localhost:1527/";
  String dbName = "MyTinyShopDB";
  String driver = "org.apache.derby.jdbc.ClientDriver";
  String userName = "root"; 
  String password = "root";
  
  try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,userName,password);

  //conn.close();
 
  } 
   
 catch (Exception e)
 {
  e.printStackTrace();
  }
  
 }

}
```
*This is code for login button*


```
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
   Connection conn = null;
   PreparedStatement prestmnt = null;
   ResultSet Reltset = null;
   
        
       try {
           String sql = "SELECT * FROM LOGINDETAILS WHERE LOGINID='"+txtFieldUserName.getText()+"'AND USERPASSWORD='"+txtFieldPassword.getText()+"'";
           prestmnt=conn.prepareStatement(sql);
           Reltset=prestmnt.executeQuery();
           
           if (Reltset.next()){
           
           AdminMainForm adminform = new AdminMainForm();
           adminform.setVisible(true);
           }
           
           else
           {
           JOptionPane.showMessageDialog(null, "User Name or Password is Wrong");
           }
           
           
       } catch (SQLException ex) {
           Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
       }
          
    }
```
*Program is running but when i click the login button these errors appears in netbeans*

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at mytinyshop.Login.btnLoginActionPerformed(Login.java:159)
at mytinyshop.Login.access$200(Login.java:18)
at mytinyshop.Login$3.actionPerformed(Login.java:77)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)

*and more* *errors*

i have added user name and password in database it has to check the database if the username and passsword are correct it has to goto the other form otherwise it has to show wrong password dialog please someone help me..


----------

