JDBC学习笔记
来源:互联网 发布:mac百度云盘怎么卸载 编辑:程序博客网 时间:2024/06/11 17:08
数据持久化
- 持久化(persistence):把数据保存到可掉电式存储设备中以供之后使用。大多数情况下,特别是企业级应用,数据持久化意味着将内存中的数据保存到硬盘上加以”固化”,而持久化的实现过程大多通过各种关系数据库来完成。
- 持久化的主要应用是将内存中的数据存储在关系型数据库中,当然也可以存储在磁盘文件、XML数据文件中。
在Java中,数据库存取技术可分为如下几类:
JDBC直接访问数据库
JDO技术
第三方O/R工具,如Hibernate, ibatis 等
JDBC是java访问数据库的基石,JDO, Hibernate等只是更好的封装了JDBC。
•JDBC(Java Database Connectivity)是一个独立于特定数据库管理系统、通用的SQL数据库存取和操作的公共接口(一组API),定义了用来访问数据库的标准Java类库,使用这个类库可以以一种标准的方法、方便地访问数据库资源
•JDBC为访问不同的数据库提供了一种统一的途径,为开发者屏蔽了一些细节问题。
•JDBC的目标是使Java程序员使用JDBC可以连接任何提供了JDBC驱动程序的数据库系统,这样就使得程序员无需对特定的数据库系统的特点有过多的了解,从而大大简化和加快了开发过程。
JDBC接口(API)包括两个层次:面向应用的API:Java API,抽象接口,供应用程序开发人员使用(连接数据库,执行SQL语句,获得结果)。
面向数据库的API:Java Driver API,供开发商开发数据库驱动程序用。
JDBC驱动程序:各个数据库厂商根据JDBC的规范制作的 JDBC 实现类的类库
JDBC驱动程序总共有四种类型:
第一类:JDBC-ODBC桥。
第二类:部分本地API部分Java的驱动程序。
第三类:JDBC网络纯Java驱动程序。
第四类:本地协议的纯 Java 驱动程序。
第三、四两类都是纯Java的驱动程序,因此,对于Java开发者来说,它们在性能、可移植性、功能等方面都有优势。
JDBC API 是一系列的接口,它使得应用程序能够进行数据库联接,执行SQL语句,并且得到返回结果。
Java.sql.Driver 接口是所有 JDBC 驱动程序需要实现的接口。这个接口是提供给数据库厂商使用的,不同数据库厂商提供不同的实现
在程序中不需要直接去访问实现了 Driver 接口的类,而是由驱动程序管理器类(java.sql.DriverManager)去调用这些Driver实现
@Testpublic void testDriver() throws SQLException { Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://localhost:3306/test"; Properties info = new Properties(); info.put("user", "root"); info.put("password", "lgh123"); info.put("characterEncoding", "UTF-8"); info.put("useSSL", "true"); info.put("useUnicode", "true"); Connection connection = driver.connect(url, info); //DriverManager.getConnection(url, info); System.out.println(connection); connection.close();}更通用的一个实现:
将配置信息写入到一个配置文件中。
@Testpublic void testProperties() throws IOException { Properties properties = new Properties(); InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(in); properties.list(System.out); in.close();}
public Connection getConnection() { Properties properties = new Properties(); InputStream in = null; in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); if (in == null) { return null; } try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } //properties.list(System.out); String driverClassName = properties.getProperty("jdbc.driverClassName"); String jdbcUrl = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); Driver driver = null; try { driver = (Driver) Class.forName(driverClassName).newInstance(); } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) { e.printStackTrace(); } Connection connection = null; Properties info = new Properties(); info.put("user", user); info.put("password", password); try { connection = driver.connect(jdbcUrl, info); } catch (SQLException e) { e.printStackTrace(); } return connection;}@Testpublic void testGetConnection() { Connection connection = getConnection(); System.out.println(connection);}
当然也可以使用DriverManager的getConnection方法。
@Testpublic void testDriverManager() { Properties properties = new Properties(); InputStream in = null; in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); if (in == null) { return; } try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } String driverClassName = properties.getProperty("jdbc.driverClassName"); String jdbcUrl = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); Properties info = new Properties(); info.put("user", user); info.put("password", password); Connection connection = null; try { connection = DriverManager.getConnection(jdbcUrl, info); } catch (SQLException e) { e.printStackTrace(); } System.out.println(connection);}
0 0
- JDBC学习笔记(二)
- JDBC学习笔记(二)
- JDBC学习笔记(三)
- JDBC学习笔记(四)
- JDBC学习笔记(六)
- JDBC学习笔记
- JDBC学习笔记
- JDBC学习笔记1
- jdbc学习笔记2
- jdbc学习笔记
- JDBC学习笔记
- jdbc学习笔记(1)
- jdbc学习笔记(2)
- jdbc学习笔记(3)
- JDBC学习笔记
- J2EE学习笔记--JDBC
- JDBC学习笔记
- JDBC学习笔记
- 在做自动化测试之前你需要知道的
- NYOJ677谍战(最大流最小割问题)
- hbbtv
- linux服务器时间同步
- Android弹窗_PopupWindow详解
- JDBC学习笔记
- 链式前向星,前向星,vector模拟链表
- spark.mllib源码阅读-分类算法4-DecisionTree
- C语言编写FIR数字低通滤波器
- Navicat导出MySQL的数据字典
- 【精品】对同步的理解,进而理解lock与synchronized。
- SPOJ 18469 UOFTCG
- 第三方支付
- Delete Node in a Linked List