Skip to content
Advertisement

Can not issue data manipulation statements with executeQuery

I’m getting an error when im trying to run my gui to inset data into a table, the error being Can not issue data manipulation statements with executeQuery(). The goal of this gui is to get the user to input information into the text area and then click submit and this information is uploaded to the database.

error : java.sql.SQLException: Can not issue data manipulation statements with executeQuery().

my code is

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();
            }
        });
    }
}

     

and this is the table im using

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)
            );

Advertisement

Answer

You have to use this method PreparedStatement#executeUpdate() instead of executeQuery

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement