异常、事务回滚

1、抛出异常

import java.io.*;

public class Exception1 {

 

         publicstatic int check(String strage) throws Exception {

                   intage = Integer.parseInt(strage);

                   if(age < 0)

                            thrownew Exception("年龄不能为负数!");

                   returnage;

         }

 

         publicstatic void main(String[] args) {

                   try{

                            intmyage = check("233f");

                            System.out.println(myage);

                   }catch (NumberFormatException e) {

                            System.out.println("数据格式错误!");

                            System.out.println("原因:" +e.getMessage());

                   }catch (Exception e) {

                            System.out.println("数据逻辑错误!");

                            System.out.println("原因:" +e.getMessage());

                   }

         }

 

        

                   //对于上述代码,如果希望异常发生后,抛出异常并继续执行后面的代码就可以同时使用try与finally语句块。

                    public static void doFile() throwsIOException{                   //通过throws将异常向上抛出

                    File file=new File("D:/myfile.txt");

                    try{

                    FileOutputStream out=newFileOutputStream(file);

                    out.write("start".getBytes()); 

                    //向myfile.txt文件中写入数据

                    out.close();                                                            //关闭输出流

                    out.write("end".getBytes());                                      //抛出IOException异常

                    }finally{

                    System.out.println("上一行代码:out.write(null)");           //执行该行代码

                    }

                    }

}

 

2、事务回滚

import java.util.*;

import java.sql.*;

 

public class Runtime {

 

         publicstatic void main(String[] args) throws SQLException {

                                     Stringurl="jdbc:odbc:book";

                                     Stringquery="SELECT * FROM book1";

                                    

                                     try{

                                               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                                     }

                                     catch(java.lang.ClassNotFoundExceptione){

                                               System.err.print("ERROR:");

                                               System.err.print(e.getMessage());

                                     }

                                     Connectioncn = null;

                                     try{

                                               cn= DriverManager.getConnection(url);

                                         cn.setAutoCommit(false); //取消自动提交

                                         Statement stmt = cn.createStatement();

                                         stmt.executeUpdate("UPDATE book1 SETprice=3.5 WHERE id=1");

                                         cn.commit();//提交

                                        

                                         SQLException e=new SQLException();

                                         throw e;

                                     }catch(SQLExceptionex){

                                               cn.rollback();//回滚

                                               ex.printStackTrace();       

                                     }

          }

         }

 

 

3、可以把要执行的四个SQL语句写到同一个List中再调用此方法

你也可以自己写

主要注意

执行sql插入前要取消自动提交 con.setAutoCommit(false);

全部sql语句执行完成后再提交 con.commit();

执行过程抛出异常则回滚      con.rollback();

希望对你有帮助

 

public boolean exeupdate(List<String>sqls) throws SQLException {

 boolean flag = false;

 openPoolConnection();//创建连接

  try{

   //

  con.setAutoCommit(false);//取消自动提交

  

  for(int i=0;i<sqls.size();i++)

   {

  pstmt = con.prepareStatement(sqls.get(i));

  int rows = pstmt.executeUpdate();

  

   }

     flag = true;

     con.commit();//提交

  }catch (Exception e) {

  con.rollback();//回滚

  e.printStackTrace();

  }finally {

  this.closeAll();//关闭连接数据集 语句对象

  }

 return flag;

 }

你可能感兴趣的:(异常、事务回滚)