JDBC-数据量连接

JDBC

【步骤】
	1.加载驱动进行连接
	方式一:通过第三方数据库连接驱动[MySQL/Oracle]配置文件,读取相对路径Jar包,使用DriverManger类
	方式二:通过第三方数据库连接池[DBCP/C3P0/Druid] Jar 以及 Proerties配置文件,读取相对路径Jar包,使用DataSource类
	
	2.获取Connection连接对象
	
	3.操作静态Sql语句执行Statement对象,或预编译Sql语句的PreparedStatement对象

	4.获得结果集ResultSet 或 受影响行affectedRow

	5.释放Sql语句执行资源 Statement 或 PerparedStatement

	6.释放Connection连接资源,若使用了数据库连接池则对close()方法进行了加强,仅清除数据归还连接池,并不会断开当前连接。

>> Statement

>>> 1.resource资源路径下创建jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///test_05?useSSL=false&useServerPrep=true
jdbc.userName=root
jdbc.password=root
【警告】使用Connection Jar包版本8.0需要和MySQL8.0数据库版本符合,否则不兼容将无法加载驱动
>>> 2.获取Connection与Statement 对象
public class DriverDemo {
    public static void main(String[] args) throws Exception {
        /*
         *  jdbc.properties 配置文件(解决JAVA代码中配置硬编码问题):
         * ————————————————————————————————————————————————————————————————————————————————————
         * |  -- Lib<Liberal>文件夹下的手动引入Jar包位置
         * | jdbc.driver=com.mysql.jdbc.Driver
         * | -- url:<主协议://IP:port/path...?资源1=资源值1&资源2=资源值2>
         * | jdbc.url=jdbc:mysql:///test_05?useSSL=false&useServerPrep=true
         * | -- 数据库访问用户名
         * | jdbc.userName=root
         * | -- 数据库访问密码
         * | jdbc.password=root
         * ————————————————————————————————————————————————————————————————————————————————————
         * */

        // 通过类加载器读取解决硬编码问题的资源
        InputStream is = ClassLoader.getSystemResourceAsStream("jdbc.properties");

        // 获取配置文件专用Map容器
        Properties prop = new Properties();
        prop.load(is);
		
		// 读取后关闭资源读取流
		is.close();
		
        // 注册驱动:DriverMananger驱动管理器,获取连接Connection
        Connection connection = DriverManager.getConnection(prop.getProperty("jdbc.url"),
                pro.getProperty("jdbc.userName"),
                pro.getProperty("jdbc.password"));

        // 默认自动开启提交 : 手动关闭即可进行 try...catch 进行 commit() or rollback()
        connection.setAutoCommit(false);

        try {
            connection.commit();
        }catch (Exception e){
            connection.rollback();
        }

        // 获取静态SQL执行类:Statement 提供给三方数据库的抽象规则,具体实现不同数据库大同小异
        Statement statement = connection.createStatement();

        
		
		
		// 关闭 Statement and Connection
		statement.close();
		connection.close(); 
    }
}

>>> 获取单行row数据
		// DML(UPDATE INSERT DELETE) 与 DDL 语句 会返回所影响的行数
        int affectedRow = statement.executeUpdate("DMQ OR DDL SQL...");

        // DQL (Query)语句 会返回结果集
        ResultSet resultSet = statement.executeQuery("DQQ SQL...");
        // 通过数据库Colum名获取值 or 通过Colum索引
        // 如果不知道Colum类型可以使用getObject()
		int resultInt = resultSet.getInt("salary")int resultInt = resultSet.getInt(1);
>>> 获取多行rows数据
		// 准备收集对象的集合
		ArrayList<T> t = new ArrayList<>();
		// 执行next()指针,这个指针类似迭代器,返回boolean类型来判断是否有数据。如果由数据使用getObject()方法获取值
        while (resultSet.next()) {
            Account account = new Account(resultSet.getString("name"), Integer.valueOf(resultSet.getString("salary")));
            accounts.add(account);
        }

>> PreparedStatement

>>> 1.resource资源路径下创建dateSource.properties文件
# 第三方数据库驱动路径
driverClassName=com.mysql.jdbc.Driver
# 统一资源标识符Url
url=jdbc:mysql:///test_05?useSSL=false&useServerPrepStmts=true
# 账户以及密码
username=root
password=root
# 初始化连接大小
initialSize=5
# 最大活跃连接
maxActive=10
# 最大等待时间(超过最大活跃连接时,最大等待时间)
maxWait=3000
>>> 2.获取DataSource与PreparedStatement 对象
		// 获取数据库连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        // 获取连接
        Connection connection = dataSource.getConnection();

        // 定义预编译SQL语句:使用 ? 占位符
        String sql = "INSERT INTO tb_brand VALUES (?,?,?,?,?,?)";
        // 获取执行静态SQL预编译对象:
        PreparedStatement preparedStatement = connection.prepareStatement(sql);


        Class<? extends Brand> aClass = brand.getClass();
        Field[] fields = aClass.getDeclaredFields();

        // 暴力反射:获取所有字段Field对象
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            // 值替换占位符:通过占位符的索引赋值,索引1开始
            preparedStatement.setObject(i + 1, fields[i].get(brand));
        }

        // 受影响行数affected row
        int i = preparedStatement.executeUpdate();

        // 释放执行sql对象
        preparedStatement.close();

        // 释放连接对象
        connection.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值