博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java连接Mysql数据库的5种方法
阅读量:2067 次
发布时间:2019-04-29

本文共 4162 字,大约阅读时间需要 13 分钟。

文章目录

参考资料:https://www.bilibili.com/video/av69222697?p=63

前言

本文讲述了Java连接Mysql数据库的5种方法,其中最后一种方式是需要掌握的。

1 使用第三方库Driver

@Test    public void connectionTest01() throws SQLException {
// 获取Driver实现类对象 Driver driver = new com.mysql.jdbc.Driver(); // jdbc:mysql :协议 // localhost: ip地址 // 3306 :mysql端口号 // test :数据库名 String url = "jdbc:mysql://10.211.55.3:3306/test"; // 将用户名和密码封装在Properties中 Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "root"); Connection conn = driver.connect(url, info); // 如果打印成功,说明连接成功 System.out.println(conn); }

第一种方式用到了第三方库com.mysql.jdbc.Driver,这个包是需要去mysql官网下载、导入的。

2 使用反射

使用反射实例化Driver,不在代码中体现第三方数据库的API,使得程序具有良好的移植性,体现了面向接口编程的思想。

@Test    public void connectionTest02() throws Exception{
// 1.获取Driver实现类对象:使用反射 Class clazz = Class.forName("com.mysql.jdbc.Driver"); Driver con = (Driver) clazz.newInstance(); // 2.mysql的地址,用户名和密码 String url = "jdbc:mysql://10.211.55.3:3306/test"; Properties info = new Properties(); info.setProperty("user","root"); info.setProperty("password","root"); Connection connect = con.connect(url, info); System.out.println(connect); }

3 使用DriverManager

使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素。

/**     * 创建数据库连接的第三中方式     * 使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素。     */    @Test    public void connectiontest03() throws Exception {
// 1.数据库连接四要素 String url = "jdbc:mysql://localhost:3307/test?autoReconnect=true&useSSL=false"; String username = "root"; String password = "root"; String driverName = "com.mysql.jdbc.Driver"; // 2.实例化Driver Class clazz = Class.forName(driverName); Driver o = (Driver) clazz.newInstance(); // 3.注册驱动 DriverManager.registerDriver(o); // 获取连接 Connection connection = DriverManager.getConnection(url, username, password); System.out.println(connection); }

4 自动注册

driver.class类中的静态代码块在类加载的时候被执行,实现了自动注册。相比方式3免去了自动注册

在这里插入图片描述

@Test    public void connectionTest04() throws Exception{
// 1.数据库连接四要素 String url = "jdbc:mysql://localhost:3307/test?useSSL=false"; String username = "root"; String password = "root"; String driverName = "com.mysql.jdbc.Driver"; // 将Driver类加载到内存中 Class.forName(driverName); // 2.实例化Driver// Class clazz = Class.forName(driverName);// Driver o = (Driver) clazz.newInstance(); // 3.注册驱动// DriverManager.registerDriver(o); //以上代码被注释的原因:在Driver.class中有如下代码, // 在类加载的时候静态代码块被执行 /** * static { * try { * DriverManager.registerDriver(new Driver()); * } catch (SQLException var1) { * throw new RuntimeException("Can't register driver!"); * } * } */ // 获取连接 Connection connection = DriverManager.getConnection(url, username, password); System.out.println(connection); }

5 使用配置文件

在方式四的基础上使用配置文件的方式保存配置信息(user,password,url,driverClass),在代码中加载配置文件。

在IDEA的相对路径下创建jdbc.properites
在这里插入图片描述

user=rootpassword=rooturl=jdbc:mysql://localhost:3307/test/?useSSL=falsedriverName=com.mysql.jdbc.Driver
@Test    public void connectionTest05() throws Exception {
// 1.加载配置文件 InputStream ras = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties props = new Properties(); props.load(ras); // 2.读取配置文件信息 String user= props.getProperty("user"); String password= props.getProperty("password"); String url= props.getProperty("url"); String driverClass= props.getProperty("driverClass"); // 3.加载驱动 Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection); }

使用配置文件的好处:

①实现了代码和数据的分离,如果需要修改配置信息,直接在配置文件中修改,不需要深入代码

②如果修改了配置信息,省去重新编译的过程。

最后一种方式需要掌握,前四种方式了解即可。

转载地址:http://dabmf.baihongyu.com/

你可能感兴趣的文章
UDP连接和TCP连接的异同
查看>>
hibernate 时间段查询
查看>>
java操作cookie 实现两周内自动登录
查看>>
Tomcat 7优化前及优化后的性能对比
查看>>
Java Guava中的函数式编程讲解
查看>>
Eclipse Memory Analyzer 使用技巧
查看>>
tomcat连接超时
查看>>
谈谈编程思想
查看>>
iOS MapKit导航及地理转码辅助类
查看>>
检测iOS的网络可用性并打开网络设置
查看>>
简单封装FMDB操作sqlite的模板
查看>>
iOS开发中Instruments的用法
查看>>
iOS常用宏定义
查看>>
被废弃的dispatch_get_current_queue
查看>>
什么是ActiveRecord
查看>>
有道词典for mac在Mac OS X 10.9不能取词
查看>>
关于“团队建设”的反思
查看>>
利用jekyll在github中搭建博客
查看>>
Windows7中IIS简单安装与配置(详细图解)
查看>>
linux基本命令
查看>>