2013-11-22 3 views
1

У меня возникли проблемы с формой входа в java, кажется, что нет ничего плохого в соединении с базой данных, но все же, когда я нажал кнопку входа в систему, ничего не происходит. Я предполагаю, что что-то отсутствует или ошибка в моем код, но я могу найти его, я надеюсь, что вы, ребята, помогите мне Спасибо заранее :)Netbeans Форма входа

package splash_screen; 


import java.sql.*; 
import javax.swing.*; 


public class Loginform extends javax.swing.JFrame { 
Connection conn=null; 
ResultSet rs=null; 
PreparedStatement pst=null; 
public Loginform() { 
    initComponents(); 
    conn=Connect.connectDB(); 
} 


@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated  Code">       
private void initComponents() { 

    jTextField1 = new javax.swing.JTextField(); 
    jLabel1 = new javax.swing.JLabel(); 
    jLabel2 = new javax.swing.JLabel(); 
    jButton1 = new javax.swing.JButton(); 
    jPasswordField1 = new javax.swing.JPasswordField(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jLabel1.setText("Username:"); 

    jLabel2.setText("Password:"); 

    jButton1.setText("Login"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(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) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(jLabel1) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(jLabel2) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addComponent(jButton1) 
         .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))) 
      .addContainerGap(197, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addContainerGap() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel1) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel2) 
       .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(18, 18, 18) 
      .addComponent(jButton1) 
      .addContainerGap(197, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold>       

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)   {           
    String sql="Select * from Account where username=? and password=?"; 

     try{ 
      pst=conn.prepareStatement(sql); 
      pst.setString(1,jTextField1.getText()); 
      pst.setString(2,jPasswordField1.getText()); 

      rs=pst.executeQuery(sql); 

      if(rs.next()){ 
       JOptionPane.showMessageDialog(null,"Password and Username is correct"); 
       Home a=new Home(); 
      a.setVisible(true); 

      } 
      else{ 
       JOptionPane.showMessageDialog(null,"Password and Username is incorrect"); 
      } 

     } 
     catch(SQLException e){ 

     } 
}           

/** 
* @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(Loginform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Loginform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Loginform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Loginform.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 Loginform().setVisible(true); 
     } 
    }); 
} 
+0

Начать замену пустого блока catch, который скрывает любое SQLException, которое вы могли бы получить, путем 'throw new RuntimeException (e);'. Вы думаете, что ничего не происходит, потому что вы, вероятно, получаете исключение, но намеренно решили поймать его и полностью игнорировать. Таким образом, вы стреляете в ногу, потому что теперь вы больше не знаете, что может быть неправильным. –

+0

добавить ** e.printStackTrace() ** в catch catch. то вы заметите свою ошибку .. – subash

+0

@JBНизет извините, но не будьте в курсе, я просто спрашиваю, что это так грубо: | –

ответ

1

Заменить

rs.executeQuery(sql); 

по

rs.executeQuery(); 

Строка SQL уже передана инструкции во время построения.

Смежные вопросы