Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:08:54+00:00 2026-06-13T11:08:54+00:00

My code can execute select statements just fine, but for some reason I can’t

  • 0

My code can execute select statements just fine, but for some reason I can’t get the insert and delete segments to work. I believe they should be the same right? I just don’t know what’s wrong with my code right here.

    else if (e.getSource() == executeButton) {
        //Create a statement
        if (enterSQLText.getText().toLowerCase().contains("select")){
            try{
                String commandString = enterSQLText.getText();
                Statement statement = conn.createStatement();
                //Execute a statement
                ResultSet resultSet = statement.executeQuery(commandString);
                //Process query results
                ResultSetMetaData metaData = resultSet.getMetaData();
                int numberOfColumns = metaData.getColumnCount();

                for(int i = 1; i<= numberOfColumns; i++){
                  executionResultText.append(metaData.getColumnName(i) + "\t");
                  System.out.printf("%20s\t", metaData.getColumnName(i));
                }
                executionResultText.append("\n");
                System.out.println();

                while (resultSet.next()){
                    for (int i = 1; i <= numberOfColumns; i++){
                        executionResultText.append(resultSet.getObject(i).toString() + "\t");
                        System.out.printf("%20s\t", resultSet.getObject(i));
                    }
                    executionResultText.append("\n");
                    System.out.println();
                 }
                 System.out.println("Execute Button Works!");
              }
              catch (Exception h){
                h.printStackTrace();
              }
        }
        else if (enterSQLText.getText().toLowerCase().contains("delete") || enterSQLText.getText().toLowerCase().contains("insert")) {
            try {
                String commandString = enterSQLText.getText();
                Statement statement = conn.prepareStatement(commandString);
                ResultSet resultSet = statement.executeUpdate(commandString);
            }
            catch (Exception l){
                l.printStackTrace();
            }
        }
        else {
            executionResultText.setText("Sorry, that is not a valid command");
            System.out.println("Not a vilid command.");
        }
    }

Within the else if statement is where I’m having the problem. It usually just throws a stack trace error if I try to run an insert or delete statement. Can someone please help?

EDIT:

To get the code to execute, I had to change the else if segment to the following:

else if (enterSQLText.getText().toLowerCase().contains("delete") || enterSQLText.getText().toLowerCase().contains("insert")) {
                try {
                    String commandString = enterSQLText.getText();
                    Statement statement = conn.prepareStatement(commandString);
                    @SuppressWarnings("unused")
                    int resultSet = statement.executeUpdate(commandString);
                }
                catch (Exception l){
                    l.printStackTrace();
                }
            }

When I try to execute an insert or delete command, I get a stack trace like this one:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Mark Renshaw' for key 'PRIMARY'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at ConnectToDB.actionPerformed(ConnectToDB.java:203)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

No matter what I do to that segment, it either prevents me from being able to connect to the database, or throws an error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:08:55+00:00Added an answer on June 13, 2026 at 11:08 am

    com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘Mark Renshaw’ for key ‘PRIMARY’

    Is quite obvious. You are trying to insert a duplicate entry for a primary key. Primary Keys are unique and you cannot use the same key twice.

    You probably don’t want your name column to be a primary key. Use a autogenerated ID column for that.

    Otherwise delete the column containing “Mark Renshaw” and run your code again. However it will only work once as you run into a conflict when running it twice with the same name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I execute some code, once a day. e.g. call MyMethod() at 3pm
Can anyone tell me how I can execute my custom jQuery code when Drupal's
What do you call a programming language that can execute its own code (passed
Can the CLR 4.0 execute CLR 2.0 IL without the need for source code
I would like to execute code sequentially in an NSOperation. This can be easily
To test if i can connect to my database, I execute the following code
I want to execute a query of the form: INSERT INTO table(field1, field2) SELECT
I'm a pdo newbie and need to prepare and execute some php/pdo code: This
I’m moving some old code over to the new msqli interface using prepared statements,
I can't seem to get this statement or statements alike to work with prepared

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.