iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >JDBC连接Mysql的5种方式实例总结
  • 214
分享到

JDBC连接Mysql的5种方式实例总结

jdbc连接MySQL代码jdbc连接mysql的代码jdbc连接mysql数据库 2023-05-14 08:05:16 214人浏览 八月长安
摘要

目录测试环境说明第一种方式第二种方式第三种方式第四种方式第五种方式总结测试环境说明 Mysql数据库:jdbc:mysql://localhost:3306/test IDE:ide

测试环境说明

Mysql数据库:jdbc:mysql://localhost:3306/test

IDE:idea 2022

jdk:JDK8

mysql:mysql 5.7

JDBC:5.1.37

第一种方式

使用静态加载驱动方式,连接mysql

这种方式灵活性差,依赖性强

public void connection01() throws SQLException {
    // 注册驱动
    Driver driver = new Driver();
    // 创建Properties对象,用于保存mysql账号和密码键值对
    Properties properties = new Properties();
    properties.setProperty("user", "root");
    properties.setProperty("passWord", "123456");
    String url = "jdbc:mysql://localhost:3306/test";
    // 得到mysql的连接
    Connection connection = driver.connect(url, properties);
    // 得到可以与mysql语句进行交互的对象
    Statement statement = connection.createStatement();
    // 关闭与 mysql语句进行交互的对象
    statement.close();
    // 关闭与mysql的连接
    connection.close();

第二种方式

在第一种方式的基础上使用反射动态加载驱动,依赖性减小、灵活性提高

public void connection02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    // 使用反射动态加载mysql驱动件程序
    Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
    Driver driver = (Driver) aClass.newInstance();
    // 创建Properties对象,用于保存mysql账号和密码键值对
    Properties properties = new Properties();
    properties.setProperty("user", "root");
    properties.setProperty("password", "123456");
    String url = "jdbc:mysql://localhost:3306/test";
    // 得到mysql的连接
    Connection connection = driver.connect(url, properties);
    // 得到可以与mysql语句进行交互的对象
    Statement statement = connection.createStatement();
    // 关闭与 mysql语句进行交互的对象
    statement.close();
    // 关闭与 mysql语句进行交互的对象
    connection.close();
}

第三种方式

使用DriverManager统一进行管理

public void connection03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
	// 使用反射动态加载mysql驱动件程序
    Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
    Driver driver = (Driver) aClass.newInstance();
    String user = "root";
    String password = "123456";
    String url = "jdbc:mysql://localhost:3306/test";
    // 使用DriverManager加载Driver
    DriverManager.reGISterDriver(driver);
    // 得到mysql的连接
    Connection connection = DriverManager.getConnection(url, user, password);
    // 得到可以与mysql语句进行交互的对象
    Statement statement = connection.createStatement();
    // 关闭与 mysql语句进行交互的对象
    statement.close();
    // 关闭与 mysql语句进行交互的对象
    connection.close();
}

第四种方式

其实Class.forName(“com.mysql.jdbc.Driver”)在底层已经自动加载好了Driver实例

所以Driver driver = (Driver) aClass.newInstance();这句话可以省略

这种方式也是开发中使用最多的一种方式

public void connection04() throws ClassNotFoundException, SQLException {
    // 使用反射动态加载mysql驱动件程序
    Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
    String user = "root";
    String password = "123456";
    String url = "jdbc:mysql://localhost:3306/test";
    // 得到mysql的连接
    Connection connection = DriverManager.getConnection(url, user, password);
    // 得到可以与mysql语句进行交互的对象
    Statement statement = connection.createStatement();
    // 关闭与 mysql语句进行交互的对象
    statement.close();
    // 关闭与 mysql语句进行交互的对象
    connection.close();
}

第五种方式

mysql5.16后可以不用Class.forName(“com.mysql.jdbc.Driver”);来加载驱动了
从jdk1.5以后使用了jdbc4,不再需要显示调用class.forName()注册驱动而是自动调用驱动jar包下META-INF\services\java.sql.Driver文本中的类名称去注册
建议还是写上 CLass . forName(“com.mysql.jdbc.Driver”),更加明确,兼容性更好

这里同时使用properties配置文件实现动态信息动态读取,灵活性得到提升

推荐使用这种方式

src/com/mysql/mysql.properties配置文件内容如下

url=jdbc:mysql://localhost:3306/test
user=root
password=123456

连接mysql程序

public void connection05() throws SQLException, ClassNotFoundException, IOException {
    // 使用Properties读取配置文件下的内容
    Properties properties = new Properties();
    properties.load(new FileInputStream("src/com/mysql/mysql.properties"));
    String url = properties.getProperty("url");
    String user = properties.getProperty("user");
    String password = properties.getProperty("password");
    // 得到mysql的连接
    Connection connection = DriverManager.getConnection(url, user, password);
    // 得到可以与mysql语句进行交互的对象
    Statement statement = connection.createStatement();
    // 关闭与 mysql语句进行交互的对象
    statement.close();
    // 关闭与 mysql语句进行交互的对象
    connection.close();
}

总结

到此这篇关于JDBC连接Mysql的5种方式的文章就介绍到这了,更多相关JDBC连接Mysql内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

您可能感兴趣的文档:

--结束END--

本文标题: JDBC连接Mysql的5种方式实例总结

本文链接: https://www.lsjlt.com/news/202961.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作