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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:03:16+00:00 2026-06-13T03:03:16+00:00

I’m using postgresql and having one problem inserting data into two different tables. The

  • 0

I’m using postgresql and having one problem inserting data into two different tables.

The idea is to insert some data no tableamizades and them insert some data in tablenotify. Basically a friend request is made and there is a notification associated with that request.

I have this code:

01    @Override
02    public void AddFriends(int userMakesRequest, int userReceivesRequest, boolean TrueOrFalse, boolean TrueOrFalse2) {
03        String notificacao, userMakesRequestName;
04        Connection connection = null;
05        Statement stmt = null;
06        ResultSet rs = null;
07
08        try {
09            Class.forName("org.postgresql.Driver");
10
11            connection = DriverManager.getConnection("jdbc:postgresql:"
12                    + "//localhost:5432/projecto", "postgres", "admin");
13
14            stmt = connection.createStatement();
15            try {
16
17                stmt.executeQuery("INSERT INTO tableamizades (iduser1,iduser2,amigoaceite1,amigoaceite2) VALUES ( "
18                        + userMakesRequest + " , '" + userReceivesRequest + "' , '" + TrueOrFalse + "' , '"
19                        + TrueOrFalse2 + "');");
20                try {
21
22
23                    rs = stmt.executeQuery("SELECT username FROM utilizadores WHERE iduser = '" + userMakesRequest + "';");
24                    rs.next();
25                    userMakesRequestName = rs.getString("username");
26
27                    notificacao = " O utilizador " + userMakesRequestName + " fez-lhe um pedido de amizade";
28
29                    stmt.executeQuery("INSERT INTO tablenotify (iduser,notificacao) VALUES ( " + userReceivesRequest + " , '" + notificacao + "');");
30                    rs.close();
31
32                } catch (SQLException e) {
33                }
34
35            } catch (SQLException e) {
36            }
37            stmt.close();
38
39            connection.close();
40
41        } catch (Exception e) {
42            e.printStackTrace(System.out);
43        } finally {
44            try {
45                connection.close();
46            } catch (Exception e) {
47                e.printStackTrace(System.out);
48            }
49        }

If I do this, the data is insert on tableamizades but not on tablenotify. I can see that the code for inserting data on tablenotify is ok because if i comment out lines 15 to 19 and 35 and 36, the data on tablenotify is correctly inserted.

Can someone tell me what am I doing wrong?

  • 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-13T03:03:17+00:00Added an answer on June 13, 2026 at 3:03 am

    You have several things wrong:

    • inserting data must be done with executeUpdate(), and not executeQuery()
    • you should learn about prepared statements which would avoid SQL injection attacks and incorrect queries if your parameters contain characters like quotes, which must be escaped.
    • you should not ignore SQLExceptions as you’re doing, since they contain the error message which would help identifying the problem. Don’t catch those exceptions. If you catch them, throw a runtime exception wrapping them.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've tracked down a weird MySQL problem to the two different ways I was
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
I'm making a simple page using Google Maps API 3. My first. One marker
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
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
I have just tried to save a simple *.rtf file with some websites and

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.