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?