Java 连接mysql数据库以及 jdbc工具类的使用

准备:在连接数据库之前我们需要导入一个jar包名为:mysql-connector-java-8.0.11.jar

原始方法获取数据库连接

  • DriverManager.getConnection(url,root,password)

  • DriverManager.getConnection(url,properties)

  • DriverManager.getConnection(url)
    三种方法仅仅参数不同,使用方法也不同,在连接本机的数据库时可以使用第一种和第二种方法,第三种方法用于连接异地数据库,今天就来使用第一种和第二种方法。

  • 第一种方式:传入三个参数url、root、password

 Class.forName("com.mysql.cj.jdbc.Driver");
 Connection con = DriverManager.getConnection("jdbc:mysql///db1?useSSL = false&serverTimezone=UTC&characterEncoding=utf8", "root", "123456");
  • 第二种方式:传入一个url和properties集合
     Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql///db1?useSSL = false&serverTimezone=UTC&characterEncoding=utf8";
            //properties集合
            Properties prop = new Properties();
            //赋值
            prop.setProperty("user","root");
            prop.setProperty("password","123456");
            //获取数据库连接对象
            Connection conn = DriverManager.getConnection(url, prop);

上面两种方式需要手动赋值,耦合度很高,如果参数发生改变,需要在源程序中对其进行修改,极其不便。所以往往会创建一个名为jdbc.properties的文档,写上如下内容:

url=jdbc:mysql:///db1?useSSL=false&serverTimezone=UTC
user=root
password=123456

那么在使用的时候我们就可以使用读取文件的方式来获取内容

 Class.forName("com.mysql.cj.jdbc.Driver");
 //类加载器读取文件
InputStream is = Test07.class.getClassLoader().getResourceAsStream("jdbc.properties");
//properties集合
Properties prop = new Properties();
//读取jdbc.properties文件
prop.load(is);
//获取数据库连接对象
Connection conn = DriverManager.getConnection(prop.getproperty("url"), prop);

当我们使用了上述方式获取到了数据库连接后,是否总是有种感觉,一直在写一些重复的代码,java讲究代码的复用性以此来提高效率,那么上面的步骤明显不符合这条规范。
于是,我们通常会封装一个名为JdbcUtil的工具类,这里面会有一些我们经常需要用到的方法,我们直接调用就可以,非常方便,如下:

//使用类加载器读取properties文件返回文件输出流
public class JdbcUtil {
private static Properties prop;
      //静态代码块,每当类加载时会自动加载
    static {
        try {
        //读取jdbc.properties中的数据到properties集合中
            InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            prop= new Properties();
            prop.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //调用getCon方法获取连接对象
    public static Connection getCon(){
        try {
            return DriverManager.getConnection(prop.getProperty("url"),prop);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //调用close方法关闭资源
    //参数为ResultSet:结果集、Statement:sql语句执行对象、Connection:数据库连接对象
    public static void close(ResultSet rs, Statement st,Connection con){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st!=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }if (con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

jdbc.properties文件内容如下:

url=jdbc:mysql:///db1?useSSL=false&serverTimezone=UTC
user=root
password=123456

常见的问题:由于mysql版本的不同,所以有些同学在连接数据库的时候可能会有一些异常发生,做一下总结

  • Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
    上述提醒会出现在mysql8.0版本中,因为Driver类的文件存放路径的改变,所以我们需要对其做一些小小的改动;
Class.forName(com.mysql.cj.jdbc.Driver);//加上一个cj
  • 时区的异常,编码格式的异常需对url进行增添;
url="jdbc:mysql:///db1?serverTimezone=UTC&useSSL=false&CharacterEncoding=utf8"

感谢观看!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值