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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:16:56+00:00 2026-06-16T13:16:56+00:00

This seems like a really simple problem, but I cannot figure out what my

  • 0

This seems like a really simple problem, but I cannot figure out what my problem is. I have a method addTask which adds some info to our database as seen in this code:

public static boolean addTask(String name, String question, int accuracy, int type){
    StringBuilder sql = new StringBuilder();
      sql.append("INSERT INTO tasks (name, question, type, accuracy) ");
      sql.append("VALUES(?, ?, ?, ?)");
      try {
        Connection c = DbAdaptor.connect();
        PreparedStatement preparedStatement = c.prepareStatement(sql.toString());
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, question);
        preparedStatement.setInt(3, type);
        preparedStatement.setInt(4, accuracy);
        preparedStatement.execute();
        preparedStatement.close();
        c.close();
            return true;
      }       
      catch (SQLException e) {
          e.printStackTrace();
          return false;
      }
}

my problem is that preparedStatement.execute() always returns false, indicating the information hasnt been added to the database. I can run psql and this confirms that nothing has been written to the db. The connection definitely connects to the correct database (i put in some other printlns etc. to check this). I am trying to insert into a newly initialised table that looks like this:

CREATE TABLE tasks
(
  id SERIAL PRIMARY KEY,
  submitter INTEGER REFERENCES accounts (id),
  name VARCHAR(100) NOT NULL,
  question VARCHAR(100) NOT NULL,
  accuracy INTEGER NOT NULL,
  type INTEGER REFERENCES types (id),
  ex_time TIMESTAMP,
  date_created TIMESTAMP
); 

code for DbAdaptor.connect():

public static Connection connect(){
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    Properties properties = new Properties();
      properties.setProperty("user", USER);
      properties.setProperty("password", PASSWORD);
    try {
        return DriverManager.getConnection(URL, properties);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

where USER and PASSWORD are static fields in the class

  • 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-16T13:16:58+00:00Added an answer on June 16, 2026 at 1:16 pm

    You misunderstood the return value of PreparedStatement#execute().

    Please carefully read the javadoc:

    Returns:

    true if the first result is a ResultSet object; false if the first result is an update count or there is no result.

    It thus returns — as fully expected — false on an INSERT query. It returns only true on a SELECT query (for which you’d however usually like to use executeQuery() instead which returns directly a ResultSet).

    If you’re interested in the affected rows, rather use PreparedStatement#executeUpdate() instead. It returns an int as per the javadoc:

    Returns:

    either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

    A return value of 1 or greater would then indicate a successful insert.


    Unrelated to the concrete problem: your code is leaking DB resources. Please carefully read How often should Connection, Statement and ResultSet be closed in JDBC?

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

Sidebar

Related Questions

This seems like a really simple problem but I can't seem to figure it
This seems like it should be really simple, but I'm unable to figure this
I have what seems like it should be a really simple problem, but somehow
Ok, so this seems like a really silly problem but I can't get my
This seems like such a simple issue but I cannot find an elegant solution.
This seems like it should be relatively straightforward, I cannot figure out what I'm
This seems like it should be really easy but I couln't find an answer
This really seems like a bug to me, but perhaps some databinding gurus can
this seems to be a simple question, but i cannot get my head around
This seems like a really simple task, so bear with me. I'm trying to

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.