2017-10-12 58 views
0

我在我的java應用程序中輸入的信息不是在mysql服務器中創建一行。我可以連接mysql服務器沒有問題。我努力解決這個問題。JAVA和SQL,不能在mysql服務器中存儲信息

這裏是處理MySQL和Java之間的連接我的第一類

public class MySqlConnect { 
    Connection conn=null; 
    public static Connection ConnectDB(){ 
     try{ 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/singindbase","root",""); 
      JOptionPane.showMessageDialog(null,"Connected to database"); 
      return conn; 
     } 
    catch(Exception e){ 
    JOptionPane.showMessageDialog(null, e); 
    return null; 
    } 
    } 
} 

這裏是我的第二類

public class SignUp extends javax.swing.JFrame { 
    Connection conn=null; 
    PreparedStatement pst=null; 
    private String Sql; 

    /** 
    * Creates new form SignUp 
    */ 
    public SignUp() { 
     initComponents(); 
    } 

    /** 
    * 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() { 

     textid = new javax.swing.JTextField(); 
     textusernameR = new javax.swing.JTextField(); 
     textpasswordR = new javax.swing.JPasswordField(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 
     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel1.setText("ID"); 

     jLabel2.setText("Username"); 

     jLabel3.setText("Password"); 

     jButton1.setText("Submit"); 
     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() 
       .addGap(21, 21, 21) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(jLabel1) 
        .addComponent(jLabel2) 
        .addComponent(jLabel3)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jButton1) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
         .addComponent(textpasswordR, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE) 
         .addComponent(textusernameR) 
         .addComponent(textid))) 
       .addContainerGap(84, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(73, 73, 73) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(textid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel1)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(textusernameR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel2)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(textpasswordR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel3)) 
       .addGap(18, 18, 18) 
       .addComponent(jButton1) 
       .addContainerGap(35, Short.MAX_VALUE)) 
     ); 

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           


    conn=MySqlConnect.ConnectDB(); 
    String sql;  
     sql = "insert into login values(?,?,?)"; 

     try { 

      pst=conn.prepareStatement(sql); 
      pst.setString(1,textid.getText()); 
      pst.setString(2,textusernameR.getText()); 
      pst.setString(3,textpasswordR.getText()); 

      pst.execute(); 

      JOptionPane.showMessageDialog(null,"Account created sucessfully"); 

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

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JTextField textid; 
    private javax.swing.JPasswordField textpasswordR; 
    private javax.swing.JTextField textusernameR; 
    // End of variables declaration     
} 

的問題是,我不能填充在MySQL數據庫中的表

enter image description here

+0

你可以在catch(SQLException)裏打印stacktrace,目前它的空白嗎? –

+0

永遠不會默默吞下異常'catch(SQLException e){ } ' –

+0

你也應該(很好的做法)在你的insert語句中聲明列名,也可能需要一個事務 –

回答

1

我想rec省略一些東西

  1. 請不要忘記打印異常堆棧跟蹤。
  2. 插入數據之前,您必須打印這些數據控制檯,這只是爲了確認您要將適當的數據插入到數據庫中,並且一旦完成刪除打印命令。

3.指定插入查詢中的列總是一個好主意。

所以請檢查下面的代碼。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    conn=MySqlConnect.ConnectDB(); 
    System.out.println("id"+textid.getText()+" username"+textusernameR.getText()+" password"+textpasswordR.getText()); // print the data (please take this to a DTO class would be better) 
    String sql ="insert into login(column1,column2,column3)values(?,?,?)";// please mention the coulmn name 
    try { 
    pst=conn.prepareStatement(sql); 
    pst.setString(1,textid.getText());// if id is an integer column you will get error here 
    pst.setString(2,textusernameR.getText()); 
    pst.setString(3,textpasswordR.getText()); 
    boolean flag = pst.execute();//this will return true or false 
    //pst.executeUpdate() it will return effected row number i prefer this method always for DDL commands 
    if(flag)JOptionPane.showMessageDialog(null,"Account created sucessfully"); 
    else JOptionPane.showMessageDialog(null,"Account creation failed"); 

    } catch (SQLException e) { 
     e.printStackTrace(); // print the stack trace 
    } 

} 

我希望我的解釋能幫助你。

1

正如@Vishnu KR所提到的,打印堆棧跟蹤將爲您提供有關錯誤的重要信息。我個人也喜歡使用調試器在執行之前檢查我的最終SQL語句到底是什麼。