在java 中执行触发器代码、创表语句

2016-02-19 21:40 143 1 收藏

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐在java 中执行触发器代码、创表语句,赶紧看过来吧!

【 tulaoshi.com - 编程语言 】

      由于程序的需要,在SQLServer 中创建触发器及建表,碰到了在java 代码中执行创建触发器及表。

      /**建立中间表*/

      public static final String

              createMiddleTableSQL =

              "CREATE TABLE [dbo].[AlarmsMiddleTbl] ( "

              + " [id] [int] NOT NULL  , "

              + " [DeviceID] [smallint] NULL , "

              + " [Aid] [char] (10) COLLATE Chinese_PRC_BIN NULL , "

              + " [BeginTime] [datetime] NULL , "

              + " [EndTime] [datetime] NULL , "

              + " [Severity] [char] (2) COLLATE Chinese_PRC_BIN NULL , "

              + " [CondType] [char] (25) COLLATE Chinese_PRC_BIN NULL , "

              + " [DetailID] [smallint] NULL , "

              +

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

              " [AckNotes] [char] (150) COLLATE Chinese_PRC_BIN NULL , "

              +

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

              " [Description] [char] (255) COLLATE Chinese_PRC_BIN NULL , "

              +

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

              " [Systemtimes] [char] (30) COLLATE Chinese_PRC_BIN NULL "

              + ") ON [PRIMARY] ";

   

   

      /**在告警表上创建触发器*/

      public static final String createMiddleTblTriggerSQL =

              "CREATE   TRIGGER t_alarms "

              + "ON [NTBW].[dbo].[Alarms] "

              + "FOR INSERT, UPDATE "

              + "AS "

              + "DECLARE  @rows int "

              + "SELECT @rows =  @@rowcount "

              + "IF @rows = 0 "

              + "  return "

              +

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

              "IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted) "

              + "BEGIN "

              + "  INSERT INTO [NTBW].[dbo].[AlarmsMiddleTbl] "

              + "  SELECT i.[id], i.[DeviceID], i.[Aid], i.[BeginTime], "

              +

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

              "  i.[EndTime], i.[Severity], i.[CondType], i.[DetailID], i.[AckNotes], "

              + "  i.[Description], getdate() from inserted i "

              + "END "

              +

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

              "IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted) AND "

              + " UPDATE(EndTime) "

              + "BEGIN "

              + "  INSERT INTO [NTBW].[dbo].[AlarmsMiddleTbl] "

              + "  SELECT i.[id], i.[DeviceID], i.[Aid], i.[BeginTime], "

              +

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

              "  i.[EndTime], i.[Severity], i.[CondType], i.[DetailID], i.[AckNotes], "

              + "  i.[Description], getdate() from inserted i "

              + "END "

              + "IF @@error 0 "

              + "BEGIN "

              + "  RAISERROR('ERROR',16,1) "

              + "  rollback transaction "

              + "  return "

              + "END ";

   

   

   

   

   

  JAVA中执行以上语句过程:

  view plaincopy to clipboardprint?
  /** 
   
   * 创建中间表或者触发器 
   
   * 
   
   * @param sql String 
   
   * @return boolean 返回语句执行结果,true 成功,false 失败 
   
   */ 
   
  private boolean createTableOrTrigger(String sql)  
   
  {  
   
      Connection con = null;  
   
      PreparedStatement st = null;  
   
      boolean result = false;  
   
      try 
   
      {  
   
          con = dbh.getConnection();  
   
          st = con.prepareStatement(sql);  
   
          st.execute();  
   
          result = true;  
   
          dbh.closeConnections(null, st, con);  
   
      }  
   
      catch (SQLException ex)  
   
      {  
   
          Log.error("Unable to create :" +  
   
                    sql + " ,ErrorCode :" + ex.getErrorCode() +  
   
                    ",Exception :" +  
   
                    ex.getLocalizedMessage());  
   
          dbh.closeConnections(null, st, con);  
   
      }  
   
   
   
      return result;  
   
  } 

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

来源:http://www.tulaoshi.com/n/20160219/1626826.html

延伸阅读
约定和编程风格 每次我想要演示实际代码时,我会对mysql客户端的屏幕就出现的代码进行调整,将字体改成Courier,使他们看起来与普通文本不一样(让大家区别程序代码和正文)。在这里举个例子: mysql DROP FUNCTION f; Query OK, 0 rows affected (0.00 sec) 如果实例比较大,则需要在某些行和段落间加注释,同时我会用将"--"符号放在页面的...
Conventions and Styles约定和编程风格 每次我想要演示实际代码时,我会对mysql客户端的屏幕就出现的代码进行调整,将字体改成Courier,使他们看起来与普通文本不一样(让大家区别程序代码和正文)。在这里举个例子: mysql DROP FUNCTION f; Query OK, 0 rows affected (0.00 sec) 如果实例比较大,则需要在某些行和段落间加注释,...
--建递增序列 CREATE SEQUENCE LZEAM_SP_LOOP_EQ_SEQ MINVALUE 1 INCREMENT BY 1 START WITH 1 测试: SELECT LZEAM_SP_LOOP_EQ_SEQ.NEXTVAL FROM DUAL --建触发器 CREATE OR REPLACE TRIGGER LZEAM.LZEAM_SP_LOOP_EQ_TRIGGER BEFORE INSERT ON SP_LOOP_EQ FOR EACH ROW BEGIN SELECT LZEAM.LZEAM_SP_LOOP_EQ_SEQ.NEXTVAL INTO :NEW.LOOP_EQ...
标签: ASP
  利用err对象: sql="insert into table(f1,f2) values('v1','v2')" conn.execute sql if err.number<0 then response.write "出错了:"& err.description err.clear else response.write "OK" end if     
Conventions and Styles约定和编程风格 每次我想要演示实际代码时,我会对mysql客户端的屏幕就出现的代码进行调整,将字体改成Courier,使他们看起来与普通文本不一样(让大家区别程序代码和正文)。在这里举个例子: mysql DROP FUNCTION f; Query OK, 0 rows affected (0.00 sec) 如果实例比较大,则需要在某些行和段落间加注...

经验教程

514

收藏

89
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部