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

  • Home
  • SEARCH
  • 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 7169939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:06:40+00:00 2026-05-28T15:06:40+00:00

I am getting disturbing errors due to issues on primary keys and inserting values

  • 0

I am getting disturbing errors due to issues on primary keys and inserting values to table part of my code is here….

In the table I have 3 columns userName, userPwd and userId(auto_incremented primary key)

    public boolean addUserBean(UserBean ub)
        {
    Connection conn = null;
    PreparedStatement prepar = null;


            boolean flag = false;
            String sql = "insert into admin values(?,?)";

            if (hasUser(ub.getUserName()))
            {
                return false;
            }
            try
            {
                prepar = conn.prepareStatement(sql,prepar.RETURN_GENERATED_KEYS);
                prepar.setString(1, ub.getUserName());
                prepar.setString(2, ub.getUserPwd());
                int result = prepar.executeUpdate();
                if (result > 0)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }

            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }finally{

            if (prepar != null) {
                try {
                    prepar.close();
                } catch (SQLException logOrIgnore) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException logOrIgnore) {
                }
            }
}

            return flag;
        }
  • 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-05-28T15:06:40+00:00Added an answer on May 28, 2026 at 3:06 pm

    You should not be manually inserting values into auto increment field. Just leave it out of insert query alltogether.

    String sql = "insert into admin(userName, userPwd) values(?,?)";
    

    the correct auto incremented value will be insseted automatically.


    Added after more details from OP:

    if MySQL complains that Field ‘userId’ doesn’t have a default value then what you told us about your table is wrong, and column userId is not in fact defined as auto increment

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

Sidebar

Related Questions

Getting data from a database table to an object in code has always seemed
Getting an error with my code found HERE ... Which is a culmination of
Getting unique rows/values in SQL. I have same some data like this valueA ValueB
Getting the above error in following code. How to rectify it. Thanks. Please look
Getting Invalid Application Path error. Here are the steps I've taken. Right click on
getting a couple errors that it's a virtual function with non-virtual destructor. how can
Getting the following error, which I understand and have seen before. But one thing
Getting the active application is not a problem, I already have this implemented. What
Getting an error message Nested functions are disabled, use -fnested etc in XCode/ObjC. Here's
Getting more and more familiar with DI but I still have few niggles. Read

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.