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 7995701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:29:58+00:00 2026-06-04T14:29:58+00:00

I’m using the following codes to add a user to a table called USERS

  • 0

I’m using the following codes to add a user to a table called USERS in my database, the connection is correct because I have tried other queries and they worked, I tried this query in Oracle itself and it worked, I’m just having problems with queries in java, mostly because of SPACE and the syntax, what is wrong with this one?

try
{
    stmt=conn.createStatement();
    //query="INSERT INTO Users (user_id,username,password,first_name,last_name) " + " VALUES (seq_users.nextval,'"+usernameCreateField.getText()+"','"+new String(passwordCreateField.getPassword())+"','"+firstnameCreateField.getText()+"','"+lastnameCreateField.getText()+"') ";
    query="INSERT INTO Users (user_id,username,password,first_name,last_name) " +" VALUES (seq_users.nextval,'test','test','test','test') ";
    rset=stmt.executeQuery(query);
}
catch(SQLException | NumberFormatException e)
{
    System.out.println("result error, " +e.getMessage());
}
finally
{
    try
    {
        rset.close();
        stmt.close();
    }
    catch(Exception e)
    {
        System.out.println("Error in closing "+e.getMessage());
    }
}
  • 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-04T14:29:59+00:00Added an answer on June 4, 2026 at 2:29 pm

    So problem is that you calling executeQuery(query) on your statement and this is reason why it doesn’t work. executeQuery() method you cannot use for DML (Data Manipulation Language) statements like are INSERT, UPDATE and DELETE. For these statements Java offers method named executeUpdate() which exists for these DML statements.

    From the docs:

    executeUpdate method 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.

    So only replace actual method with this and it would will work.

    And sorry for my curiosity but why you aren’t using parametrized SQL statements? You would know that not using parametrized statements there is hight danger of SQL Injection and many hackers using this for damage databases. You should think about it. It is very important if you want to your database will be more safer. You wouldn’t believe what prove SQL Injection with database.

    Only for example for you, should create query like this:

    query = "INSERT INTO Users (user_id,username,password,first_name,last_name) values(?,?,?,?,?) ";
    

    This ? char is representing one parameter. Numbering starts with 1,2,3, etc.. not from zero.

    And then you must replace there params with real data before you execute statement like this:

    PreparedStatement ps = conn.prepareStatement(query);
    // no set parameters
    ps.setInt(1, someDTO.getId());
    ps.setString(2, someDTO.getUserName());
    ps.setString(3, someDTO.getPassword());
    ps.setString(4, someDTO.getFirstName());
    ps.setString(5, someDTO.getLastName());
    ps.executeUpdate();
    System.out.println("Data was inserted successfully.");
    return true;
    

    So more about SQL Injection: here and here

    EDIT:

    I forgot to tell you when i just watching your code, when you opens connection and then of course you will close it, you not must call close() method for statements or ResultSet, all will be closed after when you close connection.

    Hope that helps you.

    Regards

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the

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.