java - Netbeans GUI 和 SQL 问题

标签 java sql user-interface netbeans project

//我在使用 Netbeans 时遇到问题。当我单击“注册”时,我试图将注册表中输入的值转到 SQL 表,但什么也没有发生。有人可以帮忙吗?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networking;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

/**
 *
 * @author user
 */
public class Registration extends javax.swing.JFrame {

    /**
     * Creates new form Registration
     */
    public Registration() {
        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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        FullNameField = new javax.swing.JTextField();
        EmailField = new javax.swing.JTextField();
        Username = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        AdressField = new javax.swing.JTextArea();
        PasswordField = new javax.swing.JPasswordField();
        Registeration = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Registration Form ");

        jLabel2.setText("Full Name");

        jLabel3.setText("Email");

        jLabel4.setText("Username");

        jLabel5.setText("Password");

        jLabel6.setText("Adresse");

        FullNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FullNameFieldActionPerformed(evt);
            }
        });

        AdressField.setColumns(20);
        AdressField.setRows(5);
        jScrollPane1.setViewportView(AdressField);

        Registeration.setText("Register");
        Registeration.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegisterationActionPerformed(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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(105, 105, 105)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Registeration)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(PasswordField)
                                    .addComponent(Username)
                                    .addComponent(EmailField)
                                    .addComponent(FullNameField)
                                    .addComponent(jScrollPane1)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(105, 282, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(FullNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(EmailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(Username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel6)))
                .addGap(18, 18, 18)
                .addComponent(Registeration)
                .addContainerGap(27, Short.MAX_VALUE))
        );

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

    private void RegisterationActionPerformed(java.awt.event.ActionEvent evt) {
     try{
         Connection con = DriverManager.getConnection("jdbc:mysql://localhost/networks","root","mamaendbaba");
         Statement stat = (Statement) con.createStatement();
         String Name = FullNameField.getText();
         String UserName= Username.getText();
         String Adr = AdressField.getText();
         String Pass = PasswordField.getText();
         String Emai = EmailField.getText(); 
         String excmd = "INSERT INTO networks registerd(Name, UserName, Address, Password, Email)" +
    " VALUES (?, ?, ?, ?, ?)";
         @SuppressWarnings("UnusedAssignment")
PreparedStatement statement =con.prepareStatement(excmd);
statement = (PreparedStatement) con.createStatement();

// get field values ... 
statement.setString(1, Name);
statement.setString(2, UserName);
statement.setString(3, Adr);
statement.setString(4, Pass);
statement.setString(5, Emai);
statement.executeUpdate();
     }
     catch(Exception e)
     {
     }    

    }

    private void FullNameFieldActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
     * @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(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Registration.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Registration.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 Registration().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextArea AdressField;
    private javax.swing.JTextField EmailField;
    private javax.swing.JTextField FullNameField;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JButton Registeration;
    private javax.swing.JTextField Username;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration
}

这是完整的代码,不需要任何解释,basiclu 它是一个用于注册的 jform,只需看看可以做什么,这样你就可以看到我正在从表单中获取值并尝试将它们插入表中,怎么不管用..

最佳答案

您的 SQL 参数变量中缺少多个单引号:

String excmd = "INSERT INTO networks registerd VALUES ('" + Name + "','"+ UserName +"','"+ Adr +"','"+ Pass +"','"+ Emai +"')";

此外,您没有在异常 block 中显示任何内容,这会使代码无声地失败。您可以添加:

e.printStackTrace();

更好用PreparedStatement在这里,因为它不仅消除了提供这些报价的需要,而且还可以防止 SQL Injection攻击。

<小时/>

另一个潜在的错误来源是列顺序可能不完全匹配,例如可能有一个自动增量列,这意味着您必须在 SQL 中指定列名称。您也可以省略数据库名称,例如:

String excmd =
    "INSERT INTO registerd(Name, UserName, Address, Password, Email)" +
    " VALUES (?, ?, ?, ?, ?)";
PreparedStatement statement = con.createPreparedStatement(excmd);
// get field values ... 
statement.setString(1, Name);
statement.setString(2, UserName);
statement.setString(3, Adr);
statement.setString(4, Pass);
statement.setString(5, Emai);
statement.executeUpdate();

关于java - Netbeans GUI 和 SQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14087314/

相关文章:

java - 矩形碰撞检测系统不起作用

java - Tomcat 上的 AbstractMethodError setBinaryStream

SQL 用随机数据填充表

mysql - 对大量条件使用 SQL 的 CASE?

java - 我的 gui 程序做错了什么?

java - 使用 root Android 应用程序运行二进制文件

php - Mysql php更新功能问题

ios - 为 CarPlay 音频应用程序添加 UI

带有 GUI 的 Linux 任务调度程序

java - 如何在 Android Studio 中修复 "Failed to resolve: com.google.gson:gson:2.8.5"