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?

2
  • 3
    Please don't swallow exceptions like that. Always print them out or log them. With what you have, you don't even know if there's an exception thrown in there. Commented Oct 21, 2012 at 11:45
  • 1
    bobby-tables.com/java.html Commented Oct 21, 2012 at 12:41

1 Answer 1

3

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.