Esta es la clase que contiene el codigo para la conexion con la base de datos
package drone;
import java.sql.*;
import javax.swing.*;
public class Conexion {
public String db = "prototipo";
public String user= "root";
public String pass="";
public String url= "jdbc:mysql://localhost/"+db;
public Conexion()
{
}
public Connection conectar()
{
java.sql.Connection link=null;
Statement st=null;
try{
Class.forName("com.mysql.jdbc.Driver");
link=DriverManager.getConnection(this.url, this.user, this.pass);
if(link!=null){
JOptionPane.showMessageDialog(null, "conexion establecida");
}
/*if(link!=null){
st=link.createStatement();
}*/
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "fallo conexion");
JOptionPane.showMessageDialog(null, e);
}
return link;
}
}
Esta es la clase Login que contiene el llamado al metodo conectar() de la clase Conexion, aqui tambien se genera el evento de boton
package drone;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.lang.*;
public class Login extends javax.swing.JFrame {
public Login() {
initComponents();
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
PnlFondo fondo = new PnlFondo();
this.setLayout(new BorderLayout());
this.add(fondo,BorderLayout.CENTER);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
usuario = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
contraseña = new javax.swing.JPasswordField();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Registrarse");
jLabel1.setText("Iniciar Sesion");
jLabel2.setText("Usuario");
jLabel3.setText("Contraseña");
jButton2.setText("Aceptar");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(46, 46, 46))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addGap(75, 75, 75)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(usuario, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
.addComponent(contraseña)))
.addGroup(layout.createSequentialGroup()
.addGap(163, 163, 163)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(116, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jButton1)
.addGap(33, 33, 33)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 27, Short.MAX_VALUE)
.addComponent(jLabel3)
.addGap(159, 159, 159))
.addGroup(layout.createSequentialGroup()
.addComponent(usuario, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(contraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(jButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String user=usuario.getText();
String pass=contraseña.getText();
if ((user.length()==0)||(pass.length()==0)){
JOptionPane.showMessageDialog(rootPane, "Campo usuario o contraseña vacio");
}else{
Conexion mysql= new Conexion();
Connection conn= mysql.conectar();
if(conn!=null){
JOptionPane.showMessageDialog(rootPane, "Conexion con base de datos");
}
try {
Statement sentencia= conn.createStatement();
ResultSet rs;
rs = sentencia.executeQuery("SELECT * FROM usuarios WHERE Usuario="+user+" && Contraseña="+pass+"");
int encontrado=rs.getRow();
if(encontrado==1){
Menu inicio=new Menu();
inicio.setVisible(true);
}
else{
JOptionPane.showMessageDialog(rootPane, "Los datos son incorrectos");
}
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
//if (user.equals()&&pass.equals());
}
/**
* @param args the command line arguments
*/
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(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.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 Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPasswordField contraseña;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField usuario;
// End of variables declaration
}
Gracias por adelantado quien me pueda ayudar
Debo agregar que e IDE que utilizo es netbeans el cual me indica Dereferencing possible null pointer en la linea Statement sentencia= conn.createStatement(); de la clase Login