我正在使用 postgresql,并且在将数据插入两个不同的表时遇到了一个问题。
这个想法是插入一些数据 notableamizades
并且他们在tablenotify
. 基本上提出了一个朋友请求,并且有一个与该请求相关联的通知。
我有这个代码:
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 }
如果我这样做,数据将插入tableamizades
但不是 on tablenotify
。我可以看到插入数据的代码是可以的,tablenotify
因为如果我注释掉第 15 到 19 行和第 35 和 36 行,tablenotify
则正确插入了数据。
有人可以告诉我我做错了什么吗?