存储过程--异常处理机制(Begin try Begin Catch)

一、存储过程使用 try…catch异常处理
1、TRY 块以 BEGIN TRY 语句开头,以 END TRY 语句结尾。在 BEGIN TRY 和 END TRY 语句之间可以指定一个或多个 Transact-SQL 语句。
2、CATCH 块必须紧跟 TRY 块。
3、在 Transact-SQL 中,每个 TRY 块仅与一个 CATCH 块相关联。

begin try
--SQL 
end try 
begin catch 
--sql (处理出错动作)
end catch
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Date; import java.util.Scanner; public class Student { public static final String DRIVER="sun.jdbc.odbc.JdbcOdbcDriver"; public static final String URL="jdbc:odbc:student"; public static final String USERNAME="root";//用户名 public static final String PASSWORD="root";//密码 private static String name;//当前登录管理员的名字 private static int userid;//当前登录管理员的主键 //获得 jdbc 链接 public Connection connection(){ try { Class.forName(DRIVER); Connection con=DriverManager.getConnection(URL, USERNAME, PASSWORD); return con; } catch (Exception e) { e.printStackTrace(); } return null; } //管理员登陆 public boolean login() throws Exception{ Scanner sc = new Scanner(System.in); System.out.print("输入用户名:"); String username = sc.next(); System.out.print("输入密码:"); String password = sc.next(); //查找该用户 String sql = "select username,id from muser where username='" + username + "' and password='" + password + "'"; Connection connection = connection(); Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql); if (rs.next()) { name = rs.getString(1); userid = rs.getInt(2); return true; } return false; } //添加学生 //添加学生 public void addstudent(){ System.out.print("请依次输入学号,姓名,年龄,地址(用逗号隔开):"); Scanner scanner = new Scanner(System.in); String[] str = scanner.next().split(","); /**********************begin 验证学号是否重复*************************/ String sql = "select * from student where 1=1 and stuno="+str[0]; try { Connection connection = connection(); Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql); if(rs.next()){ System.out.println("该学号已经存在,请更换"); return; } } catch (SQLException ex) { } /**********************end 验证学号是否重复*************************/ String sql1 = "insert into student(stuno,stuname,stuage,stuaddress) values(?,?,?,?)"; Connection co
((IDisposable)new A()).Dispose(); /// <summary> /// 利用传进来的sql语句查新数据库 /// </summary> /// <param name="sql">sql语句</param> /// <returns></returns> public string QuerySingle(string sql) { string single = null; using (OleDbConnection connection = new OleDbConnection(str)) { using (OleDbCommand command = new OleDbCommand(sql, connection)) { try { connection.Open(); object obj = command.ExecuteScalar(); if (obj != null) { single = command.ExecuteScalar().ToString(); } } catch { } } } return single; } /// <summary> /// 返回查询到的行数 /// </summary> /// <param name="sql"></param> /// <returns></returns> public int Execute(string sql) { int result = 0; using (OleDbConnection connection = new OleDbConnection(str)) { using (OleDbCommand command = new OleDbCommand(sql,connection)) { try { connection.Open(); result = command.ExecuteNonQuery(); } catch { } } } return result; } //读记事本 FileStream fs = File.OpenRead(@"C:\Users\Administrator\Desktop\1.txt"); StreamReader sr = new StreamReader(fs, Encoding.Default); string s = sr.ReadLine(); string upStr = File.ReadAllText(@"C:\Users\Administrator\Desktop\1.txt", Encoding.Default); MessageBox.Show(upStr); sr.Close(); fs.Close(); //写记事本 FileStream fs = File.Create(@"C:\Users\Administrator\Desktop\1.txt"); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("你的名字"); sw.Close(); fs.Close(); //messageBox控件 确定和取消 DialogResult dr=MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel); if (dr == DialogResult.OK) { //用户选择确认的操作 MessageBox.Show("您选择的是【确认】"); } else if (dr == DialogResult.Cancel) { //用户选择取消的操作 MessageBox.Show("您选择的是【取消】"); } DialogResult result=MessageBox.Show("确定重新登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { //确定编码 }else if(result == DialogResult.No) { //取消编码 } 首先,ArrayList集合和List都是非静态类,使用时都必须先实列化。 ArrayList类似一个数组,但比数组优越的是它的长度大小可变,并且可以存放不同类型的变量,包括值类型和引用类型。 常用方法如下: 1、Add():方法用于添加一个元素到当前列表的末尾 2、AddRange():方法用于添加一批元素(数组或者集合)到列表的末尾 3、Remove():方法用于删除一个元素,通过元素本身的引用来删除,填什么删什么 4、RemoveAt():方法用于删除一个元素,通过索引值来删除 5、RemoveRange():用于删除一批元素,通过指定开始的索引和删除的数量来删除 6、Insert(int index,value):用于添加一个元素到指定位置,列表后面的元素依次往后移 7、InsertRange(int index,values):用于从指定位置开始添加一批元素,列表后面的元素依次往后移动 8、Contains():用于查看集合中是否包含所给元素 VS2017专业版和企业版激活密钥 需要的请自取~ Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH 交通银行:622262 1210005830359 开户行:虹桥支行 开户人:王彬 邮寄地址:北京市大兴区西红门镇五连环工业园区鼎业路11号利亚德电视剧技术有限公司 收件人:王彬 电话:15022681976 select 单号 ,收入 = case when 收入>0 then 收入 else 0 end ,支出 = case when 收入>0 then 0 else 收入* -1 end from test select teamName,胜=sum( case when 胜利 end ),负 from TeamScore group by teamName select teamName,第1赛季得分 = case when seasonName = '第1赛季得分' then Score end ,第2赛季得分=case when seasonName = '第2赛季得分' then Score end ,第3赛季得分=case when seasonName = '第3赛季得分' then Score end from NBAScore group by teamName select studentld,语文=case when studentld = 001 then (sel ) end ,数学,英语 from table group by studentld select 商品编号,商品名称 ,王大销售数量=sum(case when 销售员 = 王大 then 销售数量 end) ,刘七销售数量=sum(case when 销售员 = 刘七 then 销售数量 end) ,张三销售数量=sum(case when 销售员 = 张三 then 销售数量 end) ,李四销售数量=sum(case when 销售员 = 张三 then 销售数量 end) ,张武销售数量=sum(case when 销售员 = 张三 then 销售数量 end) from MyOrderes group by 商品名称 -------分页 select top 7 * from table where ID not in (select top (7*(5-1)) from table order by ID asc) order by ID select TSName,TsAdderss from Tblstudent where tSid not in (select tSld from TbScroe) select t1.EmployeeID 员工编号,t1.FirstName 员工姓名,t2.FirstName 上司姓名 from Employee as t1 inner join Employee as t2 on t1.EmployeeID = t2.ReportsTo --------创建视图 create View 视图名称 as --要执行的sql语句 select TSName,TsAdderss from Tblstudent where tSid not in (select tSld from TbScroe) --执行视图 select * from 视图名称 --set和select的区别 大部分情况用set就可以,set不能将返回多个值获取到,相对严禁 select可以将获取返回的多个结果的最后一条,相对灵活 --事务 begin transaction //开启一个事务 declare @sum int = 0 //创建一个变量,记录sql语句报错次数 sql语句.................... set @sum = @sum + @@error //如果有一天sql语句报错@@error将会记录为1 sql语句.................... set @sum = @sum + @@error //如果有一天sql语句报错@@error将会记录为1 sql语句.................... set @sum = @sum + @@error //如果有一天sql语句报错@@error将会记录为1 if@sum<> 0 //如果不为0,说明有SQL语句执行错误 begin rollback //将事务回滚到执行之前 end else begin commit //SQL语句执行没有错误就提交事务 end --存储过程(无参无返回值) create procedure user //创建一个存储过程 as begin print 'Hellow world' end exec user //执行一个存储过程 --存储过程(带参数的) create procedure user //创建一个存储过程 @NAME nvarchar(50) //创建变量,数据类型及初始值 .................. //创建变量,数据类型及初始值 .................. //创建变量,数据类型及初始值 .................. //创建变量,数据类型及初始值 as begin select @NAME from table end exec user @NAME=王彬,@AGE=29 create procedure user @NAME nvarchar(20) @AGE int as begin select * from table where NAME = @NAME and AGE = @AGE end exec user @NAME = '男',@AGE = 15 select 单号 ,收入 = case when 收入>0 then 收入 else 0 end ,支出 = case when 收入>0 then 0 else 收入* -1 end from test select teamName,胜=sum( case when 胜利 end ),负 from TeamScore group by teamName select teamName,第1赛季得分 = case when seasonName = '第1赛季得分' then Score end ,第2赛季得分=case when seasonName = '第2赛季得分' then Score end ,第3赛季得分=case when seasonName = '第3赛季得分' then Score end from NBAScore group by teamName select studentld,语文=case when studentld = 001 then (sel ) end ,数学,英语 from table group by studentld select 商品编号,商品名称 ,王大销售数量=sum(case when 销售员 = 王大 then 销售数量 end) ,刘七销售数量=sum(case when 销售员 = 刘七 then 销售数量 end) ,张三销售数量=sum(case when 销售员 = 张三 then 销售数量 end) ,李四销售数量=sum(case when 销售员 = 张三 then 销售数量 end) ,张武销售数量=sum(case when 销售员 = 张三 then 销售数量 end) from MyOrderes group by 商品名称 --分页 select top 7 * from table where ID not in (select top (7*(5-1)) from table order by ID asc) order by ID select TSName,TsAdderss from Tblstudent where tSid not in (select tSld from TbScroe) select t1.EmployeeID 员工编号,t1.FirstName 员工姓名,t2.FirstName 上司姓名 from Employee as t1 inner join Employee as t2 on t1.EmployeeID = t2.ReportsTo --创建视图 create View 视图名称 as --要执行的sql语句 select TSName,TsAdderss from Tblstudent where tSid not in (select tSld from TbScroe) --执行视图 select * from 视图名称 --set和select的区别 大部分情况用set就可以,set不能将返回多个值获取到,相对严禁 select可以将获取返回的多个结果的最后一条,相对灵活 --事务 begin transaction //开启一个事务 declare @sum int = 0 //创建一个变量,记录sql语句报错次数 sql语句.................... set @sum = @sum + @@error //如果有一天sql语句报错@@error将会记录为1 sql语句.................... set @sum = @sum + @@error //如果有一天sql语句报错@@error将会记录为1 sql语句.................... set @sum = @sum + @@error //如果有一天sql语句报错@@error将会记录为1 if@sum<> 0 //如果不为0,说明有SQL语句执行错误 begin rollback //将事务回滚到执行之前 end else begin commit //SQL语句执行没有错误就提交事务 end --存储过程(无参无返回值) create procedure user //创建一个存储过程 as begin print 'Hellow world' end exec user //执行一个存储过程 --存储过程(带参数的) go //表示批处理的第一句话 create procedure user //创建一个存储过程 @NAME nvarchar(50) //创建变量,数据类型及初始值 .................. //创建变量,数据类型及初始值 .................. //创建变量,数据类型及初始值 .................. //创建变量,数据类型及初始值 as begin select @NAME from table end create procedure user @NAME nvarchar(20) @AGE int as begin select * from table where NAME = @NAME and AGE = @AGE end exec user @NAME = '男',@AGE = 15 ---分页存储过程 go create procedure fenye @countpize int //总页数 @everpize int //每页多少条数据 @sumpize //总页数 @pazeindex //当前页数 begin select * from table where betweem ((@pazeindex-1)*@everpize)+1 and @pazeindex*@everpize set @sumpize =(select cuont(*) from table) //总条数 set @countpize = @sumpize/@everpize //总页数 end ((IDisposable)new A()).Dispose(); /// <summary> /// 利用传进来的sql语句查新数据库 /// </summary> /// <param name="sql">sql语句</param> /// <returns></returns> public string QuerySingle(string sql) { string single = null; using (OleDbConnection connection = new OleDbConnection(str)) { using (OleDbCommand command = new OleDbCommand(sql, connection)) { try { connection.Open(); object obj = command.ExecuteScalar(); if (obj != null) { single = command.ExecuteScalar().ToString(); } } catch { } } } return single; } /// <summary> /// 返回查询到的行数 /// </summary> /// <param name="sql"></param> /// <returns></returns> public int Execute(string sql) { int result = 0; using (OleDbConnection connection = new OleDbConnection(str)) { using (OleDbCommand command = new OleDbCommand(sql,connection)) { try { connection.Open(); result = command.ExecuteNonQuery(); } catch { } } } return result; } //读记事本 FileStream fs = File.OpenRead(@"C:\Users\Administrator\Desktop\1.txt"); StreamReader sr = new StreamReader(fs, Encoding.Default); string s = sr.ReadLine(); string upStr = File.ReadAllText(@"C:\Users\Administrator\Desktop\1.txt", Encoding.Default); MessageBox.Show(upStr); sr.Close(); fs.Close(); //写记事本 FileStream fs = File.Create(@"C:\Users\Administrator\Desktop\1.txt"); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("你的名字"); sw.Close(); fs.Close(); //messageBox控件 确定和取消 DialogResult dr=MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel); if (dr == DialogResult.OK) { //用户选择确认的操作 MessageBox.Show("您选择的是【确认】"); } else if (dr == DialogResult.Cancel) { //用户选择取消的操作 MessageBox.Show("您选择的是【取消】"); } DialogResult result=MessageBox.Show("确定重新登录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (result == DialogResult.Yes) { //确定编码 }else if(result == DialogResult.No) { //取消编码 } 首先,ArrayList集合和List都是非静态类,使用时都必须先实列化。 ArrayList类似一个数组,但比数组优越的是它的长度大小可变,并且可以存放不同类型的变量,包括值类型和引用类型。 常用方法如下: 1、Add():方法用于添加一个元素到当前列表的末尾 2、AddRange():方法用于添加一批元素(数组或者集合)到列表的末尾 3、Remove():方法用于删除一个元素,通过元素本身的引用来删除,填什么删什么 4、RemoveAt():方法用于删除一个元素,通过索引值来删除 5、RemoveRange():用于删除一批元素,通过指定开始的索引和删除的数量来删除 6、Insert(int index,value):用于添加一个元素到指定位置,列表后面的元素依次往后移 7、InsertRange(int index,values):用于从指定位置开始添加一批元素,列表后面的元素依次往后移动 8、Contains():用于查看集合中是否包含所给元素 https://pan.baidu.com/s/1LCfl5sPCb-HfQmL0Chjvmg#list/path=/ syll VS2017专业版和企业版激活密钥 需要的请自取~ Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH 交通银行:622262 1210005830359 开户行:虹桥支行 开户人:王彬 邮寄地址:北京市大兴区西红门镇五连环工业园区鼎业路11号利亚德电视剧技术有限公司 收件人:王彬 电话:15022681976 /// <summary> /// 利用传进来的sql语句查新数据库 /// </summary> /// <param name="sql">sql语句</param> /// <returns></returns> public string QuerySingle(string sql) { string single = null; using (OleDbConnection connection = new OleDbConnection(str)) { using (OleDbCommand command = new OleDbCommand(sql, connection)) { try { connection.Open(); object obj = command.ExecuteScalar(); if (obj != null) { single = command.ExecuteScalar().ToString(); } } catch { } } } return single; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值