Jdbc原生数据库连接

太久没用过原生数据库连接方式,都快忘记了,这篇文章留做笔记。

后面使用一些高级框架去操作数据库,比如dbutil+C3P0,hibernate,jdbcTemplate等等,高级框架也是以底层为基础搭建的,
所以底层操作数据库也很重要。

总的来说jdbc原生方式分为以下7个步骤:

1、导入mysql包
2、加载驱动【反射】
3、取得数据库连接对象Connection
4、创建sql对象
5、执行sql命令,并返回结果集
6、处理结果集
7、关闭结果集,关闭会话,关闭连接

注:我的demo是用maven构建的,因此第一步导JAR包在代码中并没有注释。

废话不多说,直接上代码:

package com.zone.db;

import java.sql.*;

/**
 * @ClassName JdbcUtils1
 * @Author ZONE
 * @Date 2019/7/8 11:37
 * @Version 1.0
 **/
public class JdbcUtils1 {
    //这里使用静态代码块 (所谓静态代码块:随着类的加载而执行,且只执行一次)
    static {//2、加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){//3、建立连接
        Connection connection=null;
        try {
            connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydata?characterEncoding=utf-8","root","root");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){//关闭资源
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

下面是测试,注释也写的很清楚了:

import com.zone.db.JdbcUtils1;
import com.zone.db.JdbcUtils2;
import org.junit.Test;

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

/**
 * @ClassName JdbcUtils1Test
 * @Author ZONE
 * @Date 2019/7/8 12:01
 * @Version 1.0
 **/
public class JdbcUtils1Test {
    @Test
    public void test(){
        String sql="select * from jingdongbook WHERE bookId<=?";
        //这里的条件只是为了演示下面的这句话:ps.setString(1,"1356605");
        //要是没条件则不需要设置参数   第一个空为位置,第二个空为条件参数
        Connection connection= JdbcUtils1.getConnection();//执行到这里的时候实际上驱动加载已经完成
        //即这里包括前面说的两个步骤:2、驱动加载,3、获取连接对象
        PreparedStatement ps=null;
        ResultSet resultSet=null;

        try {
            ps=connection.prepareStatement(sql);//4、创建sql对象
            ps.setString(1,"1356605");
            resultSet=ps.executeQuery();//5、执行sql命令,并返回结果集
            while (resultSet.next()){//6、处理结果集
                System.out.println("bookName:"+resultSet.getString("bookName")+";BookPrice"+resultSet.getString("bookPrice"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils1.close(connection,ps,resultSet);//关闭连接
    }
    @Test
    public void test2(){
        InputStream inputStream=JdbcUtils1Test.class.getClassLoader().getResourceAsStream("db.properties");
        System.out.println(inputStream);
    }
}

下面一点优化将参数提取到db.properties中去:

driver = com.mysql.jdbc.Driver
url = jdbc:mysql://127.0.0.1:3306/mydata?characterEncoding=utf-8
username = root
password = root
package com.zone.db;

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

/**
 * @ClassName JdbcUtils2
 * @Author ZONE
 * @Date 2019/7/813:44
 * @Version 1.0
 **/
public class JdbcUtils2 {
    private static Properties properties;
    static {
        properties=new Properties();
        //使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream inputStream=JdbcUtils2.class.getClassLoader().getResourceAsStream("db.properties");
        //使用properties对象加载输入流
        try {
            properties.load(inputStream);
            Class.forName(properties.getProperty("driver"));//获取对应的值
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection connection=null;
        try {
            connection= DriverManager.getConnection(properties.getProperty("url"),
                    properties.getProperty("username"),
                    properties.getProperty("password"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){//关闭资源
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

最后,测试截图:

原创:https://blog.csdn.net/qq_37094660/article/details/95059896

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值