Jdbc学习


参考b站韩顺平老师jdbc课程

一、连接数据库

方式一:
在这里插入图片描述
方式二
在这里插入图片描述
方式三
在这里插入图片描述
方式四
在这里插入图片描述
方式五
在这里插入图片描述

二、乱写

总结有哪些接口以及常用方法
在这里插入图片描述
在这里插入图片描述

事务处理

package com.aylxx.jdbc.lx;

import com.aylxx.jdbc.utils.JDBCUtils;
import org.junit.jupiter.api.Test;

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


public class Founctiontest {
    @Test
    public void test01()  {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        String sql1="update account set money=money-100 where name='安玉龙'";
        String sql2="update account set money=money+100 where name='富有徐'";

        try {
            connection= JDBCUtils.getConnection();
            connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql1);
            preparedStatement.executeUpdate();
            int i=1/0;
            preparedStatement=connection.prepareStatement(sql2);
            preparedStatement.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            System.out.println("发生异常撤销操作");
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            JDBCUtils.close(null,preparedStatement,connection);
        }

    }
}

jdbc工具包
防止一次次写连接数据库的代码
方便

package com.aylxx.jdbc.utils;

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

public class JDBCUtils {
    private  static  String user;
    private  static  String pwd;
    private  static  String url;
    private  static  String driver;

    static {

        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\jdbc.properties"));
            user=properties.getProperty("user");
            pwd=properties.getProperty("password");
            url=properties.getProperty("url");
            driver=properties.getProperty("driver");
        } catch (IOException e) {
            throw new RuntimeException(e);

        }
    }
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,user,pwd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void close(ResultSet resultSet, Statement statement,Connection connection){
        try {
            if(resultSet!=null)
            {
                resultSet.close();
            }
            if(statement!=null)
            {
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

批处理应用

传统处理时间

@Test
    public void test01(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        String sql="insert into ll values(?,?)";
        long start=System.currentTimeMillis();
        try {
            connection = JDBCUtils.getConnection();
            preparedStatement=connection.prepareStatement(sql);

            for (int i = 0; i < 5000; i++) {

            preparedStatement.setString(1,"ayl"+i);
            preparedStatement.setString(2,"666");
            preparedStatement.executeUpdate();

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(null,preparedStatement,connection);
        }
        long end=System.currentTimeMillis();
        System.out.println(end-start);
    }

插入5000条数据运行时间:8684ms

批处理:用到了preparedStatement中的addbatch()方法,添加sql语句,executebatch()执行sql语句,clearbatch()清空sql语句

 public void test01(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        String sql="insert into ll values(?,?)";
        long start=System.currentTimeMillis();
        try {
            connection = JDBCUtils.getConnection();
            preparedStatement=connection.prepareStatement(sql);

            for (int i = 0; i < 5000; i++) {

            preparedStatement.setString(1,"ayl"+i);
            preparedStatement.setString(2,"666");
            preparedStatement.addBatch();
            if((i+1)%1000==0)
            {
                preparedStatement.executeBatch();
                preparedStatement.clearBatch();
            }


            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(null,preparedStatement,connection);
        }
        long end=System.currentTimeMillis();
        System.out.println(end-start);
    }

运行时间:1170ms

可以看出在执行大量插入语句时运用批处理效率会提高很多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值