1

テーブルにデータを挿入するために GUI を実行しようとすると、エラーが発生します。エラーは、executeQuery() でデータ操作ステートメントを発行できません。この GUI の目的は、ユーザーにテキスト領域に情報を入力してから送信をクリックさせることです。この情報はデータベースにアップロードされます。

エラー: java.sql.SQLException: executeQuery() でデータ操作ステートメントを発行できません。

私のコードは

class registerInterface extends JFrame {

    static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String USERNAME = "root";
    static final String PASSWORD = "root";
    private JTextField jtfFname, jtfLname, jtfAddress1, jtfAddress2, jtfCity, jtfZipcode, jtfState, jtfUsername, jtfPassword, jtfPassConfirm, jtfEmail, jtfdtype;
    private JButton exitButton, backButton, clearButton, submitButton;
    private JMenuItem jmiBack, jmiClear, jmiSubmit, jmiExit, jmiHelp, jmiAbout;
    String first, last, email, address, username, password, dtype;
    
       // launch the application
    public void Create() {

        Connection conn = null;
        try {
            Class.forName(JDBC_DRIVER).newInstance();
            conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

            PreparedStatement statement = conn.prepareStatement("INSERT INTO person ('firstName', 'lastName', 'email', 'address', 'userName', 'password', 'dtype') "
                    + "VALUES ('" + first + "', '" + last + "', '" + email + "', '" + address + "', '" + username + "', '" + password + "', '" + dtype + "')");
            statement.executeQuery();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    registerInterface() {
        //create menu bar
        JMenuBar regMenuBar = new JMenuBar();

        //set menu bar to the applet
        setJMenuBar(regMenuBar);

        //add menu "operation" to menu bar
        JMenu optionsMenu = new JMenu("Options");
        optionsMenu.setMnemonic('O');
        regMenuBar.add(optionsMenu);

        //add menu "help"
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic('H');
        helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
        regMenuBar.add(helpMenu);

        //add menu items with mnemonics to menu "options"
        optionsMenu.add(jmiSubmit = new JMenuItem("Submit", 'S'));
        optionsMenu.add(jmiClear = new JMenuItem("Clear", 'C'));
        optionsMenu.add(jmiBack = new JMenuItem("Back", 'B'));
        optionsMenu.addSeparator();
        optionsMenu.add(jmiExit = new JMenuItem("Exit", 'E'));

        //panel p1 to holds text fields
        JPanel p1 = new JPanel(new GridLayout(11, 11));
        p1.add(new JLabel("First Name: "));
        p1.add(jtfFname = new JTextField(15));
        p1.add(new JLabel("Last Name: "));
        p1.add(jtfLname = new JTextField(15));
        p1.add(new JLabel("Street Address 1: "));
        p1.add(jtfAddress1 = new JTextField(15));
        
        p1.add(new JLabel("E-mail Address: "));
        p1.add(jtfEmail = new JTextField(15));
        p1.add(new JLabel("Username: "));
        p1.add(jtfUsername = new JTextField(15));
        p1.add(new JLabel("Password: "));
        p1.add(jtfPassword = new JPasswordField(15));
        p1.add(new JLabel("jtfdtype: "));
        p1.add(jtfdtype = new JTextField(15));


        //panel p2 to holds buttons
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(exitButton = new JButton("Exit"));
        p2.add(backButton = new JButton("Back"));
        p2.add(clearButton = new JButton("Clear"));
        p2.add(submitButton = new JButton("Submit"));

        //Panel with image??????

        //add panels to frame
        JPanel panel = new JPanel(new GridLayout(2, 1));
        panel.add(p1, BorderLayout.CENTER);
        panel.add(p2, BorderLayout.SOUTH);
        add(panel, BorderLayout.CENTER);


        //listners for exit menuitem and button
        jmiExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        //listner for about menuitem
        jmiAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "This is the registration panel"
                        + "\n Assignment for University",
                        "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });


        //listners for clear menuitem and button
        jmiClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jtfFname.setText("");
                jtfLname.setText("");
                jtfAddress1.setText("");
                
                
                jtfEmail.setText("");
                jtfUsername.setText("");
                jtfPassword.setText("");
            }
        });

        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jtfFname.setText("");
                jtfLname.setText("");
                jtfAddress1.setText("");
  
               
                jtfEmail.setText("");
                jtfUsername.setText("");
                jtfPassword.setText("");
            }
        });

        //action listeners for back buttons and redister menuitem
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Welcome welcome = new Welcome();
                welcome.setVisible(true);
                welcome.setSize(500, 500);
                welcome.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                registerInterface.this.dispose();
                registerInterface.this.setVisible(false);
            }
        });

        jmiBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Welcome welcome = new Welcome();
                welcome.setVisible(true);
                welcome.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                registerInterface.this.dispose();
                registerInterface.this.setVisible(false);

            }
        });

        //action listeners for Login in button and menu item
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String a = jtfFname.getText();
                String b = jtfLname.getText();
                String c = jtfEmail.getText();
                String d = jtfAddress1.getText();
                String u = jtfUsername.getText();
                String f = jtfPassword.getText();
                String g = jtfdtype.getText();
                
              
                Create();
            }
        });
    }
}

     

これは私が使用しているテーブルです

CREATE TABLE PERSON (
    ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    firstName VARCHAR (50) NOT NULL,
    lastName VARCHAR (50) NOT NULL,
    email VARCHAR (50) NOT NULL,
    address VARCHAR (50),
    city VARCHAR (20),
    userName VARCHAR (20) NOT NULL,
    password VARCHAR (20) NOT NULL,
    dtype VARCHAR (20) NOT NULL,
    PRIMARY KEY (ID),
    UNIQUE KEY (email)
            );
4

2 に答える 2

1

代わりに、このメソッドPreparedStatement#executeUpdate()を使用する必要がありますexecuteQuery

この PreparedStatement オブジェクトの SQL ステートメントを実行します。これは、INSERT、UPDATE、DELETE などの SQL データ操作言語 (DML) ステートメントである必要があります。または、DDL ステートメントなど、何も返さない SQL ステートメント。

于 2013-09-29T05:20:50.717 に答える