我尝试创建用户的过程将通过输入“ userID,userName,Resident,selectedTeam”创建粉丝,并将其保存到“ FAN”表中。但是,在创建“ FAN”的同时,我想从“ FAN”中获取“ selectedTeam”,并通过获取selectedTeam并将其与teamID匹配来更新“ TEAM”表的“ VOTES”列,如果成功,它将会将投票记录增加1(功能类似于COUNT)。

我遇到的问题是正在创建FAN,但VOTES记录未更新。什么都没发生。

这是更新命令


addEntry =
           connection.prepareStatement( "UPDATE TEAM SET VOTES = VOTES + 1 "
                 + "WHERE TEAM_ID =" + getTeamID());
         addEntry.executeUpdate(); // insert the entry





SQL脚本



DROP TABLE Fan;
DROP TABLE Player;
DROP TABLE Team;

CREATE TABLE Team
(Team_id INT,
Team_name VARCHAR (20),
Region VARCHAR (10),
League VARCHAR (30),
Manager VARCHAR (20),
Votes INT,
CONSTRAINT PK_Team PRIMARY KEY (Team_id));

CREATE TABLE Player
(Player_id INT ,
Player_name CHAR (30),
Player_age INT ,
Player_position CHAR (3),
Nationality CHAR (20),
Team_id INT ,
CONSTRAINT PK_Player PRIMARY KEY (Player_id),
CONSTRAINT FK_Player FOREIGN KEY (Team_id) REFERENCES Team (Team_id));

CREATE TABLE Fan
(User_id INT ,
User_name CHAR (20),
Resident CHAR (20),
Selected_team INT,
CONSTRAINT PK_User PRIMARY KEY (User_id));

INSERT INTO Team
VALUES (1, 'Arsenal FC', 'England', 'Barclays Premier League', 'Arsene Wenger', 0);
INSERT INTO Team
VALUES (2, 'Aston Villa FC', 'England', 'Barclays Premier League', 'Tim Sherwood', 0);
INSERT INTO Team
VALUES (3, 'Reading FC', 'England', 'Championship', 'Steve Clark', 0);
INSERT INTO Team
VALUES (4, 'Liverpool FC', 'England', 'Barclays Premier League', 'Brendan Rodgers', 0);

INSERT INTO Player
VALUES (1001, 'Wojciech Szczensy', 24, 'GK', 'Poland', 1);
INSERT INTO Player
VALUES (1002, 'Kieran Gibbs', 25, 'DF' , 'England', 1);
INSERT INTO Player
VALUES (1003, 'Per Mertesacker', 30, 'DF', 'Germany', 1);
INSERT INTO Player
VALUES (1004, 'Laurent Koscielny', 29, 'DF', 'France', 1);
INSERT INTO Player
VALUES (1005, 'Tomas Rosicky', 34, 'MF', 'Czech Republic', 1);
INSERT INTO Player
VALUES (1006, 'Mikel Arteta', 32, 'MF', 'Spain', 1);
INSERT INTO Player
VALUES (1007, 'Jack Wilshere', 23, 'MF', 'England', 1);
INSERT INTO Player
VALUES (1008, 'Mesut Ozil', 26, 'MF', 'Germany', 1);
INSERT INTO Player
VALUES (1009, 'Olivier Giroud', 28, 'FW', 'France', 1);
INSERT INTO Player
VALUES (1010, 'Theo Walcott', 25, 'FW', 'England', 1);
INSERT INTO Player
VALUES (1011, 'Alex Oxlade-Chamberlain', 21, 'FW', 'England', 1);
INSERT INTO Player
VALUES (1012, 'Aaron Ramsey', 24, 'MF', 'Wales', 1);
INSERT INTO Player
VALUES (1013, 'Alexis Sanchez', 26, 'FW', 'Chile', 1);
INSERT INTO Player
VALUES (1014, 'Nacho Monreal', 29, 'DF', 'Spain', 1);
INSERT INTO Player
VALUES (1015, 'Santi Cazorla', 30, 'MF', 'Spain', 1);
INSERT INTO Player
VALUES (1016, 'Mathieu Flamini', 31, 'MF', 'France', 1);
INSERT INTO Player
VALUES (1017, 'Calum Chambers', 20, 'DF', 'England', 1);
INSERT INTO Player
VALUES (1018, 'Danny Welbeck', 24, 'FW', 'England', 1);
INSERT INTO Player
VALUES (1019, 'Francis Coquelin', 23, 'MF', 'France', 1);
INSERT INTO Player
VALUES (1020, 'Brad Guzan', 30, 'GK', 'United States', 2);
INSERT INTO Player
VALUES (1021, 'Nathan Baker', 23, 'DF', 'England', 2);
INSERT INTO Player
VALUES (1022, 'Joe Bennett', 24, 'DF', 'England', 2);
INSERT INTO Player
VALUES (1023, 'Ron Vlaar', 30, 'DF', 'Netherlands', 2);
INSERT INTO Player
VALUES (1024, 'Jores Okore', 22, 'DF', 'Denmark', 2);
INSERT INTO Player
VALUES (1025, 'Ciaran Clark', 25, 'DF', 'Ireland', 2);
INSERT INTO Player
VALUES (1026, 'Phillipe Senderos', 30, 'DF', 'Switzerland', 2);
INSERT INTO Player
VALUES (1027, 'Alan Hutton', 30, 'DF', 'Scotland', 2);
INSERT INTO Player
VALUES (1028, 'Aly Cissokho', 27, 'DF', 'France', 2);
INSERT INTO Player
VALUES (1029, 'Matthew Lowton', 25, 'DF', 'England', 2);
INSERT INTO Player
VALUES (1030, 'Leandro Bacuna', 23, 'MF', 'Netherlands', 2);
INSERT INTO Player
VALUES (1031, 'Tom Cleverley', 25, 'MF', 'England', 2);
INSERT INTO Player
VALUES (1032, 'Scott Sinclair', 25, 'MF', 'England', 2);
INSERT INTO Player
VALUES (1033, 'Fabian Delph', 25, 'MF', 'England', 2);
INSERT INTO Player
VALUES (1034, 'Ashley Westwood', 24, 'MF', 'England', 2);
INSERT INTO Player
VALUES (1035, 'Charles Nzogbia', 28, 'MF', 'France', 2);
INSERT INTO Player
VALUES (1036, 'Jack Grealish', 19, 'MF', 'Ireland', 2);
INSERT INTO Player
VALUES (1037, 'Andreas Weimann', 23, 'FW', 'Austria', 2);
INSERT INTO Player
VALUES (1038, 'Gabriel Agbonlahor', 28, 'FW', 'England', 2);
INSERT INTO Player
VALUES (1039, 'Christian Benteke', 24, 'FW', 'Belgium', 2);
INSERT INTO Player
VALUES (1040, 'Adam Federici', 30, 'GK', 'Australia', 3);
INSERT INTO Player
VALUES (1041, 'Chris Gunter', 25, 'DF', 'Wales', 3);
INSERT INTO Player
VALUES (1042, 'Stephen Kelly', 31, 'DF', 'Ireland', 3);
INSERT INTO Player
VALUES (1043, 'Alex Pearce', 26, 'DF', 'Ireland', 3);
INSERT INTO Player
VALUES (1044, 'Michael Hector', 22, 'DF', 'England', 3);
INSERT INTO Player
VALUES (1045, 'Oliver Norwood', 23, 'MF', 'Northern Ireland', 3);
INSERT INTO Player
VALUES (1046, 'Danny Guthrie', 27, 'MF', 'England', 3);
INSERT INTO Player
VALUES (1047, 'Hal Robson-Kanu', 25, 'MF', 'Wales', 3);
INSERT INTO Player
VALUES (1048, 'Jordan Obita', 21, 'MF', 'England', 3);
INSERT INTO Player
VALUES (1049, 'Garath McCleary', 27, 'MF', 'Jamaica', 3);
INSERT INTO Player
VALUES (1050, 'Hope Akpan', 23, 'MF', 'Nigeria', 3);
INSERT INTO Player
VALUES (1051, 'Jem Karacan', 26, 'MF', 'Turkey', 3);
INSERT INTO Player
VALUES (1052, 'Danny Williams', 26, 'MF', 'USA', 3);
INSERT INTO Player
VALUES (1053, 'Jake Taylor', 23, 'MF', 'Wales', 3);
INSERT INTO Player
VALUES (1054, 'Pavel Pogrebnyak', 31, 'FW', 'Russia', 3);
INSERT INTO Player
VALUES (1055, 'Simon Cox', 27, 'FW', 'Ireland', 3);
INSERT INTO Player
VALUES (1056, 'Jamie Mackie', 29, 'FW', 'Scotland', 3);
INSERT INTO Player
VALUES (1057, 'Nick Blackman', 25, 'FW', 'England', 3);
INSERT INTO Player
VALUES (1058, 'Simon Mignolet', 27, 'GK', 'Belgium', 4);
INSERT INTO Player
VALUES (1059, 'Glen Johnson', 30, 'DF', 'England', 4);
INSERT INTO Player
VALUES (1060, 'José Enrique', 29, 'DF', 'Spain', 4);
INSERT INTO Player
VALUES (1061, 'Kolo Touré', 33, 'DF', 'Ivory Coast', 4);
INSERT INTO Player
VALUES (1062, 'Dejan Lovren', 25, 'DF', 'Croatia', 4);
INSERT INTO Player
VALUES (1063, 'Mamadou Sakho', 25, 'DF', 'France', 4);
INSERT INTO Player
VALUES (1064, 'Martin Skrtel', 30, 'DF', 'Slovakia', 4);
INSERT INTO Player
VALUES (1065, 'Steven Gerrard', 34, 'MF', 'England', 4);
INSERT INTO Player
VALUES (1066, 'Philippe Coutinho', 22, 'MF', 'Brazil', 4);
INSERT INTO Player
VALUES (1067, 'Jordan Henderson', 24, 'MF', 'England', 4);
INSERT INTO Player
VALUES (1068, 'Adam Lallana', 26, 'MF', 'England', 4);
INSERT INTO Player
VALUES (1069, 'Emre Can', 21, 'MF', 'Germany', 4);
INSERT INTO Player
VALUES (1070, 'Joe Allen', 25, 'MF', 'Wales', 4);
INSERT INTO Player
VALUES (1071, 'Raheem Sterling', 20, 'MF', 'England', 4);
INSERT INTO Player
VALUES (1072, 'Jordon Ibe', 19, 'MF', 'England', 4);
INSERT INTO Player
VALUES (1073, 'Rickie Lambert', 33, 'FW', 'England', 4);
INSERT INTO Player
VALUES (1074, 'Daniel Sturridge', 25, 'FW', 'England', 4);
INSERT INTO Player
VALUES (1075, 'Fabio Borini', 23, 'FW', 'Italy', 4);
INSERT INTO Player
VALUES (1076, 'Mario Balotelli', 24, 'FW', 'Italy', 4);





JDBC代码:

      // check whether dataSource was injected by the server
      if ( dataSource == null )
         throw new SQLException( "Unable to obtain DataSource" );

      // obtain a connection from the connection pool
      Connection connection = dataSource.getConnection();

      // check whether connection was successful
      if ( connection == null )
         throw new SQLException( "Unable to connect to DataSource" );

      try
      {
         // create a PreparedStatement to insert a new address book entry
         PreparedStatement addEntry =
            connection.prepareStatement( "INSERT INTO FAN " +
               "(USER_ID,USER_NAME,RESIDENT,SELECTED_TEAM)" +
               "VALUES ( ?, ?, ?, ? )" );

         // specify the PreparedStatement's arguments
         addEntry.setInt( 1, getUserID() );
         addEntry.setString( 2, getUserName() );
         addEntry.setString( 3, getResident() );
         addEntry.setInt( 4, getSelectedTeam() );

         addEntry.executeUpdate(); // insert the entry

         addEntry = connection.prepareStatement( "UPDATE TEAM SET VOTES = VOTES + 1 "
                 + "WHERE TEAM_ID = " + getTeamID());
         addEntry.executeUpdate(); // insert the entry

         return "Prediction"; // go back to index.xhtml page
      } // end try
      finally
      {
         connection.close(); // return this connection to pool
      } // end finally
   } // end method save

    // return a ResultSet of entries
   public ResultSet getTeams() throws SQLException
   {
      // check whether dataSource was injected by the server
      if ( dataSource == null )
         throw new SQLException( "Unable to obtain DataSource" );

      // obtain a connection from the connection pool
      Connection connection = dataSource.getConnection();

      // check whether connection was successful
      if ( connection == null )
         throw new SQLException( "Unable to connect to DataSource" );

      try
      {
         // create a PreparedStatement to insert a new address book entry
         PreparedStatement getTeams = connection.prepareStatement(
            "SELECT TEAM_ID, TEAM_NAME, REGION, LEAGUE, MANAGER, VOTES " +
                 "FROM TEAM");

         CachedRowSet rowSet = new com.sun.rowset.CachedRowSetImpl();
         rowSet.populate( getTeams.executeQuery() );
         return rowSet;
      } // end try
      finally
      {
         connection.close(); // return this connection to pool
      } // end finally
   } // end method getAddresses

最佳答案

CREATE TRIGGER [TRIGGER_ALTER_COUNT] ON [dbo].[tblExample]
FOR INSERT, UPDATE
 AS
 BEGIN
  DECLARE @getSelectedTeamID  VARCHAR(36)
  SELECT @SelectedTeamID = getSelectedTeam FROM INSERTED
  UPDATE [dbo].[YourTeamsTblExample] SET VoteCount = VoteCount + 1

  WHERE SelectedTeamID = @getSelectedTeamID


结束

10-07 19:33