JDBC连接方式的最终形态
发布日期:2022-02-17 02:39:52 浏览次数:37 分类:技术文章

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

JDBC核心技术

持久化:

1.把数据保存到可掉电式存储设备中以供之后使用。大多数情况下,特别是企业级应用,数据持久化意味着将内存中的数据保存到硬盘是加以固话,而持久化的实现过程大多通过各种关系数据库来完成
2.持久化的主要应用是将内存中的数据存储在关系型数据库中,当然也可以存储在磁盘文件中、XML数据文件中

java中的数据存储技术

在java中,数据库存储技术可分为以下几类

JDBC直接访问数据库
JDO技术
第三方O/R工具,如Hibernate、Myblats
Jdbc是java访问数据库的基石,JDO、Hibernate、MyBalts等只是封装好的JDBC

JDBC介绍

JDBC是一种独立于特定数据库管理系统,通用的SQL数据存储和操作的公共接口,定义了用来访问数据库的标准java类库,使用这些类库可以以一种标准的方法,方便地访问数据库资源。

Jdbc为访问不同的数据库提供了一种统一的途径,为开发者屏蔽了一些细节问题。
JDBC的目标是使java程序员使用JDBC可以连接任何提供了JDBC驱动程序的数据库系统,这样就使得程序员无需对特定的数据库系统的特点有过多的了解,从而大大简化和加快了开发进程。在这里插入图片描述在这里插入图片描述在这里插入图片描述
url
jdbc:mysql:协议
localhost:ip地址
3306:默认mysql的端口号
test:test数据库

连接方式

import java.sql.SQLException;import java.util.Properties;public class ConnectionTest {      @Test    public void testConnection1() throws SQLException{        Driver driver = new com.mysql.jdbc.Driver(); 驱动        String url = "jdbc:mysql://localhost:3306/test";        Properties info = new Properties();        info.setProperty("user","root");        info.setProperty("password","root");        Connection conn =  driver.connect(url, info);        System.out.println(conn);    }}

这种移植性不好

所以要使用反射

@Test    public void testConnection2() throws Exception {      Class clazz  =    Class.forName("com.mysql.jdbc.Driver");      Driver driver=(Driver)clazz.newInstance();      String url = "jdbc:mysql://localhost:3306/test";      Properties info = new Properties();      info.setProperty("user","root");      info.setProperty("password","root");      Connection conn =  driver.connect(url, info);      System.out.println(conn);    }}
@Test    public void testConnection3() throws  Exception{        Class clazz  =    Class.forName("com.mysql.jdbc.Driver");        Driver driver=(Driver)clazz.newInstance();        String url = "jdbc:mysql://localhost:3306/test";        String password="root";        String user="root";        DriverManager.registerDriver(driver);        Connection conn = DriverManager.getConnection(url, user, password);        System.out.println(conn);    }}

使用drivemanager替换driver

@Test    public void testConnection4() throws  Exception{          //获取三个连接的基本信息        String url = "jdbc:mysql://localhost:3306/test";        String password="root";        String user="root";                //加载Driver        Class.forName("com.mysql.jdbc.Driver");        //注册驱动//        Driver driver=(Driver)clazz.newInstance();//        DriverManager.registerDriver(driver);        //获取连接        Connection conn = DriverManager.getConnection(url, user, password);        System.out.println(conn);    }}

相较于方式3,省略了注册驱动,因为在mysql的Driver类中声明了

static {        try {            DriverManager.registerDriver(new Driver());        } catch (SQLException var1) {            throw new RuntimeException("Can't register driver!");        }    }

还有改进 将基本信息写入配置文件,用输入流读出来

@Test    public void testConnection5() throws  Exception{//        1.读取配置文件中的4个基本信息        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");        Properties pros = new Properties();        pros.load(is);        String user = pros.getProperty("user");        String password = pros.getProperty("password");        String url = pros.getProperty("url");        String driverClass = pros.getProperty("driverClass");//        2.加载驱动        Class.forName(driverClass);        //3.获取连接        Connection conn =DriverManager.getConnection(url,user,password);        System.out.println(conn);    }}

将数据存储在src目录下的file中

实现了代码数据分离,解耦。

转载地址:https://blog.csdn.net/qq_39814938/article/details/106500407 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:在IDEA使用了@test以后无法在控制台输出
下一篇:servlet改进jsp想要实现的功能

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月08日 04时56分07秒