小白学java日记day24--jdbc

mysql 相关知识:
1.多表查询的分类
合并结果集     
    union        去重
    unionall    不去重
连接查询
    内连接        inner join   on
    外连接      左外   left (outer) join  on    右外 (outer)  right  join on  全外(mysql不支持)
    自然连接    natural
子查询    嵌套查询
2.合并结果集时对表的要求
字段顺序相同,类型,个数相同
A  B   C
A union B  union  C
3.解释笛卡尔积
两个表连接查询,表中的每一条记录都会互相连接在,最后形成一个大表,过程是笛卡尔积 

jbdc:

1.jdbc的实现原理
    注册驱动
    建立连接
    获取Statement对象绑定sql语句
    执行查询后得到ResultSet对象
    卸货
    关闭资源
2.连接池的原理
事先创建一个池子,放入指定数量的连接对象,当我们想使用连接时,直接从池子中取出使用,使用完成后再放回池子.
作用:节省资源,不用频繁的创建销毁连接对象
    提高性能
    方便对连接对象进行管理
3.什么是事务
事务是一种保证用户数据库操作的安全机制,通常一个事务中默认只执行一条sql语句,事务保持原子性,内部的sql语句要么全成功,要么全不成功.
4.事务的特性(ACID)
原子性
一致性
隔离性
持久性
5.事务的常用方法
commit
rollback
start transaction
6.隔离级别
未提交  1    不可重复读,脏读,幻读都有可能出现
提交 2       可以防止脏读               orcle
可重复读  4   可以防止脏读,不可重复读     mysql
序列化  8     都可以防止

案例:

张三和李四是好朋友,张三借给李四1000元,前提是张三的账户中有足够的钱,否则告知李四,暂时钱不够,不好意思

mysql-connector-java-5.0.8-bin.jar jdbc连接包导入工程

import util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/*
张三和李四是好朋友,张三借给李四1000元,前提是张三的账户中有足够的钱,否则告知李四,暂时钱不够,不好意思.

分析:
过程:
1.张三先查自己的账户钱是否足够
足够
    在继续借钱给李四
        张三的账户-1000
        李四的账户+1000
不够
    直接回复:李四,暂时钱不够,不好意思

技术:
jdbc
数据库

 */
public class test{
    public static void main(String[] args) {
        double borrowmoney=1000;
        //1.判断张三的钱够不够
        boolean v= isEnough(borrowmoney);
        if (v){//钱足够,可以借
            //2.借钱
            jie(borrowmoney);
        }else {//钱不够,提醒,不能借
            System.out.println("李四,暂时钱不够,不好意思");
        }
    }
    //1.判断张三的钱够不够
    public static boolean isEnough(double money){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet set = null;
        try {
            connection = DBUtils.getConnection();
            statement = connection.prepareStatement("select money from account where name =?");
            statement.setString(1,"zhangsan");

            set = statement.executeQuery();
            if (set.next()){
                double temp =set.getDouble("money");
                if(temp>money){
                    return true;
                }else {
                    return false;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
    //2.借钱
    public static void jie(double money){
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection = DBUtils.getConnection();
            //设置隔离级别--最高级别
            //注意:要在开启事务之前设置隔离级别
            connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
            //手动开启事务
            connection.setAutoCommit(false);
            //张三向外借钱
            statement = connection.prepareStatement("update account set money=money-? where name =?");
            statement.setDouble(1,money);
            statement.setString(2,"zhangsan");
            int v1=statement.executeUpdate();
            if(v1>0){
                System.out.println("张三取钱成功!");
            }else {
                System.out.println("张三取钱失败!");
            }
            //停电了
            int a=4/0;

            //李四收钱
            statement =connection.prepareStatement("update account set money=money+? where name =?");
            statement.setDouble(1,money);
            statement.setString(2,"lisi");
            int v2=statement.executeUpdate();
            if(v2>0){
                System.out.println("李四存钱成功!");
            }else {
                System.out.println("李四存钱失败!");
            }

            //提交事务
            connection.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }catch (ArithmeticException e){
            //执行事务回滚
            try {
                connection.rollback();
                //结束事务
                //回到事务开始之前的状态,清内存
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }finally {
            DBUtils.closeAll(connection,statement,null);
        }
    }
}

 创建DBUtils工具类文件

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    static String driver = null;
    static String url = null;
    static String user = null;
    static String pwd = null;
    //静态代码块
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileReader("src\\DBConfig.properties"));
            driver = properties.getProperty("myDriver");
            url = properties.getProperty("myUrl");
            user = properties.getProperty("myUser");
            pwd = properties.getProperty("myPwd");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //利用反射
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public static Connection getConnection() throws SQLException {
        //2.获取连接--通过驱动获取
        return DriverManager.getConnection(url,user,pwd);
    }

    //关闭资源
    public static void closeAll(Connection connection, Statement statement, ResultSet set){
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (set != null){
            try {
                set.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

编写DBConfig.properties配置文件

myDriver = com.mysql.jdbc.Driver
myUrl = jdbc:mysql://localhost:3306/mydb2
myUser = root
myPwd = 12345

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值