Oracle事物

来源:互联网 发布:vibe算法改进代码 编辑:程序博客网 时间:2024/05/22 00:44

DML(data manipulation language)是数据操纵语言:它们是SELECT、UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言。

DDL(data definition language)是数据定义语言:DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用。
DCL(DataControlLanguage)是数据库控制语言:是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。

package com.eduask.day19;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.swing.text.DefaultEditorKit.InsertBreakAction;/** *  * 建一张账户表 有账户的id 和余额=3000两个字段   * 先从A账户中取出1000块  然后再加入到另一个B账户中 * 打印出账户的余额 *  * @author benji * */public class JdbcTest5 {    public static Connection getConnection(){        try {            Class.forName("oracle.jdbc.driver.OracleDriver");            return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:ZG","SCOTT","123456");        } catch (ClassNotFoundException e) {            e.printStackTrace();            return null;        } catch (SQLException e) {            e.printStackTrace();            return null;        }    }    public static void close(ResultSet resultSet,PreparedStatement per,Connection conn){        if(resultSet!=null){            try {                resultSet.close();            } catch (SQLException e) {                e.printStackTrace();                resultSet=null;            }        }        if(per!=null){            try {                per.close();            } catch (SQLException e) {                e.printStackTrace();                per=null;            }        }        if(conn!=null){            try {                conn.close();            } catch (SQLException e) {                e.printStackTrace();                conn=null;            }        }    }    @SuppressWarnings("finally")    public static double getBlance(int a){        //加载驱动        //获得链接        Connection connection=getConnection();        PreparedStatement preparedStatement=null;        ResultSet reSet=null;        double blance=0.0;        //        try {            preparedStatement=connection.prepareStatement("select * from blace where no=?");            preparedStatement.setInt(1, a);            reSet=preparedStatement.executeQuery();            while (reSet.next()) {                blance=reSet.getDouble(2);            }        } catch (SQLException e) {            e.printStackTrace();        }finally{            close(reSet, preparedStatement, connection);            return blance;        }    }    /**     * 对账户进行修改     * @param b     */    public static void updateAccount(){        Connection connection=getConnection();        PreparedStatement preparedStatement1=null;        PreparedStatement preparedStatement2=null;        ResultSet reSet=null;        double blance=0.0;        //        try {            //关闭自动提交            connection.setAutoCommit(false);            preparedStatement1=connection.prepareStatement("update blace set blance=? where  no=1 ");            preparedStatement2=connection.prepareStatement("update blace set blance=? where  no=2 ");            preparedStatement1.setDouble(1, 500);            preparedStatement1.execute();            int i=100/0;            preparedStatement2.setDouble(1, 4500);            preparedStatement2.execute();            connection.commit();        } catch (SQLException e) {            e.printStackTrace();            try {                connection.rollback();            } catch (SQLException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }        }finally{//          close(reSet, preparedStatement, connection);//          return blance;        }    }    public static void main(String args[]){//      System.out.print(getBlance(1));        updateAccount();    }}
0 0
原创粉丝点击