Jump to content

Erro na pesquisa SQL


alphasil

Recommended Posts

Oi ppl;

estou a tentar conectar uma aplicação java na minha bd mas está a dar-me um erro nullpointer

package requisicao;
import java.awt.HeadlessException;
import java.sql.*;
import javax.swing.JOptionPane;
public class AdminLog extends javax.swing.JFrame
{
  Connection con=null;
  ResultSet rs = null;
  PreparedStatement pst= null;


   public AdminLog()
   {
    initComponents();
    this.setLocationRelativeTo(null);
   }


   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">						 
   private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    logBut = new javax.swing.JButton();
    user = new javax.swing.JTextField();
    pass = new javax.swing.JTextField();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jLabel1.setText("Userame :");
    jLabel2.setText("Userpass:");
    logBut.setText("Login");
    logBut.addMouseListener(new java.awt.event.MouseAdapter() {
	    public void mouseClicked(java.awt.event.MouseEvent evt) {
		    logButMouseClicked(evt);
	    }
    });
    pass.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		    passActionPerformed(evt);
	    }
    });
    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()
		    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			    .addComponent(jLabel1)
			    .addComponent(jLabel2))
		    .addGap(39, 39, 39)
		    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
			    .addComponent(user)
			    .addComponent(pass, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
			    .addGroup(layout.createSequentialGroup()
				    .addGap(13, 13, 13)
				    .addComponent(logBut)))
		    .addContainerGap(203, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
	    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	    .addGroup(layout.createSequentialGroup()
		    .addGap(30, 30, 30)
		    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
			    .addComponent(jLabel1)
			    .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
		    .addGap(35, 35, 35)
		    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
			    .addComponent(jLabel2)
			    .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
		    .addGap(18, 18, 18)
		    .addComponent(logBut)
		    .addContainerGap(154, Short.MAX_VALUE))
    );
    pack();
   }// </editor-fold>					   
   private void passActionPerformed(java.awt.event.ActionEvent evt) {									
    // TODO add your handling code here:
   }								   
   private void logButMouseClicked(java.awt.event.MouseEvent evt) {								   

    con = LigacaoBd.conecta();

    String sql = "select * from admin where user = ? and pass = ?";
    try{
	    pst = con.prepareStatement(sql);
	    pst.setString(1, user.getText());
	    pst.setString(2, pass.getText());
	    rs = pst.executeQuery();
	    if(rs.next()){
		    JOptionPane.showMessageDialog(null,"Os dados estão corretos ");
	    }
	    else
	    {
		    JOptionPane.showMessageDialog(null,"Os dados estão errados...");
	    }
    }
	    catch (SQLException | HeadlessException e){
		    JOptionPane.showMessageDialog(null, e);
	    }	  
   }								  
   public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
	 */
    try {
	    for (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(AdminLog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
	    java.util.logging.Logger.getLogger(AdminLog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
	    java.util.logging.Logger.getLogger(AdminLog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
	    java.util.logging.Logger.getLogger(AdminLog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
	    public void run() {
		    new AdminLog().setVisible(true);
	    }
    });
   }
   // Variables declaration - do not modify					
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JButton logBut;
   private javax.swing.JTextField pass;
   private javax.swing.JTextField user;
   // End of variables declaration				  
}

E depois de carregar em "login" aparece-me isso

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at requisicao.AdminLog.logButMouseClicked(AdminLog.java:98)

at requisicao.AdminLog.access$000(AdminLog.java:7)

at requisicao.AdminLog$1.mouseClicked(AdminLog.java:41)

Não estou a ver .....alguma dica?

Obrigado

gmc11

 

Link to comment
Share on other sites

Ligação tem

Ligado à Base de dados

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at requisicao.AdminLog.logButMouseClicked(AdminLog.java:99)

at requisicao.AdminLog.access$000(AdminLog.java:7)

e eu inicio aqui a ligação

public AdminLog()
   {
    initComponents();
    this.setLocationRelativeTo(null);
    con = LigacaoBd.conecta();
   }

E o meu fichero de ligação

package requisicao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class LigacaoBd {

   static Connection conecta()
   {
    Connection conexao;
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/reserva_salas";
    String login = "root";
    String pass = "";
    try
    {
	    Class.forName(driver);
	    conexao = DriverManager.getConnection(url, login, pass);
	    System.out.println("Ligado à Base de dados");		
    }
    catch (ClassNotFoundException | SQLException e)
    {
    }  
    return null;
   }
}
gmc11

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.