简体   繁体   中英

Java login with Java derby DB

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..

You set your connection to null at the start of btnLoginActionPerformed()

Connection conn = null;

And then you try and use it 5 lines later

prestmnt=conn.prepareStatement(sql);

Calling a method on null reference will cause a NullPointerException

Java exceptions are generally very good at telling you what is wrong and where. You have a NullPointerException in the btnLoginActionPerformed method; it tells you the exact line, though we cannot tell from what you've posted. The prestmnt could be null (error in the SQL), or there could be an error in the query so that you're getting null results so Reltset is null. Debug it.

import java.swing.*;

import java.sql.*;

Connection c=null;
        PreparedStatement pst = null;
        ResultSet r=null;
        String s;

        try
        {
            c=DriverManager.getConnection("jdbc:derby://localhost:1527/users","rishi","123");
            s="SELECT * FROM RISHI.USERDETAILS WHERE name='"+txt_name.getText()+"' AND password='"+txt_pass.getText()+"'";
            pst=c.prepareStatement(s);
            r=pst.executeQuery();

                  if(!txt_name.getText().trim().isEmpty())
                  {
                     if(!txt_pass.getText().trim().isEmpty())
                     {
                        if(r.next())
                        {
                           JOptionPane.showMessageDialog(this,"Welcome"); 
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(this,"Username or password is wrong");
                        }
                     }
                     else
                     {
                         JOptionPane.showMessageDialog(this,"Enter The password");
                     }
                  }
                  else
                  {
                     JOptionPane.showMessageDialog(this,"Enter The Username");
                  }
              }

        catch(Exception n)
        {
            JOptionPane.showMessageDialog(null,n.getMessage()); 
        }
    }                                 


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new login().setVisible(true);
            }
        });
    }


}
Connection c=null;
        PreparedStatement pst=null;
        ResultSet r=null;
        String s;

        try
        {
            c=DriverManager.getConnection("jdbc:derby://localhost:1527/users","rishi","123");
            s="SELECT * FROM RISHI.USERDETAILS WHERE name='"+txt_name.getText()+"' AND password='"+txt_pass.getText()+"'";
            pst=c.prepareStatement(s);
            r=pst.executeQuery();

                  if(!txt_name.getText().trim().isEmpty())
                  {
                     if(!txt_pass.getText().trim().isEmpty())
                     {
                        if(r.next())
                        {
                           JOptionPane.showMessageDialog(this,"Welcome"); 
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(this,"Username or password is wrong");
                        }
                     }
                     else
                     {
                         JOptionPane.showMessageDialog(this,"Enter The password");
                     }
                  }
                  else
                  {
                     JOptionPane.showMessageDialog(this,"Enter The Username");
                  }
              }

        catch(Exception n)
        {
            JOptionPane.showMessageDialog(null,n.getMessage()); 
        }
    }                                 


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton b;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txt_name;
    private javax.swing.JTextField txt_pass;
    // End of variables declaration                   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM