java调用mysql的存储过程

java调用方法

private void createOrUpdateDBStore(String storeName,String tableSQLs,boolean newStore, String dbType) throws java.sql.SQLException
  {
    if(null == storeName || storeName.length()<=0)
    {
      throw new SQLException("数据库名为空");
    }
    if(null == tableSQLs || tableSQLs.length()<=0)
    {
      throw new SQLException("建表脚本为空");
    }
    SessionFactory.dispose();
    Session session = SessionFactory.getSession();
    try
    {
      java.sql.Connection conn = session.getConnection("WBO_Server_DB_System");
      java.sql.Statement ps = conn.createStatement();

      if(newStore)
      {
        String createStoreSQL = this.toCrateStoreSQL(storeName,dbType);
        ToolLog.getLog().debug(createStoreSQL);
        ps.execute(createStoreSQL);
      }

      StringBuffer sqlSB = new StringBuffer();
      sqlSB.append("USE " + storeName + "\r\n");
      ToolLog.getLog().debug(sqlSB.toString());
      ps.execute(sqlSB.toString());

      String[] lines = this.toSQLLines(tableSQLs);
      sqlSB = new StringBuffer();
      for(int i=0;i<lines.length;i++)
      {
        String s = lines[i];
        if("GO".equalsIgnoreCase(s))
        {
          String sql = sqlSB.toString();

          ToolLog.getLog().debug(sql);
          ps.execute(sql);

          sqlSB = new StringBuffer();
        }
        else 
        {
          sqlSB.append(s);
          sqlSB.append("\r\n");
        }
      }
    }
    finally
    {
      SessionFactory.close(session);
    }
  }

 sql脚本

drop procedure if exists sp_update_table_field;
GO
create procedure sp_update_table_field()
begin
if not exists(select 1 from information_schema.columns where table_name='WB_Sys_Role' and column_name='Is_Export') then  
    ALTER TABLE WB_Sys_Role ADD Is_Export TINYINT NOT  NULL default '0';
end if;
end 
GO
call sp_update_table_field();
GO
drop procedure if exists sp_update_table_field;
GO

 这样是可以执行的;

但是在mysql的客户端是不会执行的,客户端执行的sql语句是:

drop procedure if exists sp_update_table_field;
delimiter //
create procedure sp_update_table_field()
begin
if not exists(select 1 from information_schema.columns where table_name='WB_Sys_Role' and column_name='Is_Export') then  
    ALTER TABLE WB_Sys_Role ADD Is_Export TINYINT NOT  NULL default '0';  
end if;
end 
//
delimiter ;
call sp_update_table_field();
drop procedure if exists sp_update_table_field;
 delimiter 分节符命令,是不被java解析的!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值