JDBC设计理念浅析 JDBC简介(一)
概念


JDBC功能核心
数据库查询
- 连接数据库
- 执行SQL
- cmd打印结果
- 连接数据库
- 执行SQL
- 处理返回结果
JDBC架构设计
- 提供了一套纯粹的JAVA API给应用程序开发者
- 提供了一套低级别的JDBC driver API给数据库驱动开发者
JDBC API
官方相关介绍
What the
java.sql Package Contains
The
java.sql package contains API for the following:
- Making a connection with a database via the
facility 通过驱动管理器工具与数据库建立连接DriverManager
class 类 -- makes a connection with a driver 与驱动程序建立连接
DriverManagerclass 类-- provides permission when code running within a Security Manager, such as an applet, attempts to set up a logging stream through the
SQLPermission
在安全管理器(如applet)中运行的代码试图通过驱动程序管理器设置日志流时,提供权限DriverManager
interface 接口 -- provides the API for registering and connecting drivers based on JDBC technology ("JDBC drivers"); generally used only by the
Driver
class提供基于JDBC技术的驱动程序注册和连接API(“JDBC驱动程序”);通常只被DriverManager类使用DriverManager
class 类 -- provides properties for a JDBC driver; not used by the general user 为JDBC驱动程序提供属性;一般用户不使用
DriverPropertyInfo
- Sending SQL statements to a database 向数据库发送SQL语句
-- used to send basic SQL statements 执行对象,用于发送基本的SQL语句
Statement-- used to send prepared statements or basic SQL statements (derived from
PreparedStatement
) 用于发送准备好的语句或基本SQL语句(从Statement派生)Statement
-- used to call database stored procedures (derived from
CallableStatement
) 用于调用数据库存储过程(从PreparedStatement 派生)PreparedStatement
interface 接口 -- provides methods for creating statements and managing connections and their properties 提供用于创建语句和管理连接及其属性的方法
Connection-- provides savepoints in a transaction 在事务中提供Savepoint保存点
Savepoint
- Retrieving and updating the results of a query 检索和更新查询的结果
interface 接口
ResultSet
- Standard mappings for SQL types to classes and interfaces in the Java programming language SQL类型到Java编程语言中的类和接口的标准映射
interface 接口-- mapping for SQLArray
ARRAY SQL ARRAY映射
interface 接口-- mapping for SQLBlob
BLOB SQL BLOB映射
interface接口 -- mapping for SQLClob
CLOB SQL CLOB 映射
class 类-- mapping for SQLDate
DATE
interface 接口 -- mapping for SQLNClob
NCLOB
interface 接口-- mapping for SQLRef
REF
interface 接口-- mapping for SQLRowId
ROWID
interface 接口-- mapping for SQLStruct
STRUCT
interface 接口-- mapping for SQLSQLXML
XML
class 类-- mapping for SQLTime
TIME
class 类-- mapping for SQLTimestamp
TIMESTAMP
class 类-- provides constants for SQL types 为SQL类型提供常量Types
- Custom mapping an SQL user-defined type (UDT) to a class in the Java programming language 自定义将SQL用户定义类型(UDT)映射到Java编程语言中的类
interface -- specifies the mapping of a UDT to an instance of this class 指定UDT到该类实例的映射SQLData
interface -- provides methods for reading UDT attributes from a stream 提供从流中读取UDT属性的方法SQLInput
interface -- provides methods for writing UDT attributes back to a stream 提供将UDT属性写回流的方法SQLOutput
- Metadata 元数据
interface -- provides information about the database 提供有关数据库的信息DatabaseMetaData
interface -- provides information about the columns of aResultSetMetaData
object 提供有关ResultSet对象的列的信息ResultSet
interface -- provides information about the parameters toParameterMetaData
commands 为PreparedStatement命令提供有关参数的信息PreparedStatement
- Exceptions 异常
-- thrown by most methods when there is a problem accessing data and by some methods for other reasons 当访问数据存在问题时大多数方法都会抛出这个异常,还有一些方法是其他原因抛出这个异常。
SQLException-- thrown to indicate a warning 抛出以表示警告
SQLWarning-- thrown to indicate that data may have been truncated 抛出以指示数据可能已被截断
DataTruncation-- thrown to indicate that not all commands in a batch update executed successfully 抛出以指示批处理更新中并非所有命令都已成功执行
BatchUpdateException
核心
一般流程
第一个JDBC示例
准备
导包
示例代码
package jdbc.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 第一个JDBC
* @author noteless
*/
public class FirstJDBC {
public static void main(String[] args) throws Exception {
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//数据库连接所需参数
String user = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/sampledb?useUnicode=true&characterEncoding=utf-8";
//2、获取连接对象
Connection conn = DriverManager.getConnection(url, user, password);
//设置sql语句
String sql = "select * from student";
//3、获得sql语句执行对象
Statement stmt = conn.createStatement();
//4、执行sql并保存结果集
ResultSet rs = stmt.executeQuery(sql);
//5、处理结果集
while (rs.next()) {
System.out.print("id:" + rs.getInt(1));
System.out.print(",姓名:" + rs.getString(2));
System.out.print(",年龄:" + rs.getInt(3));
System.out.println(",性别:" + rs.getString(4));
}
//6、资源关闭
rs.close();
stmt.close();
conn.close();
}
}
结果

总结
JDBC设计理念浅析 JDBC简介(一)的更多相关文章
- JDBC驱动程序注册 JDBC简介(二)
使用JDBC进行数据库操作的第一步就是驱动注册(当然你得先导入JAR). 驱动注册有多种方式,第一步必然是获得正确的驱动名称与URL格式 驱动名称与URL格式 RDBMS 驱动程序名称 ...
- Mybatis配置信息浅析 MyBatis简介(二)
官方文档入门篇中有明确说明 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的. SqlSessionFactory 的实例可以通过 SqlSessionF ...
- Mybatis sql映射文件浅析 Mybatis简介(三)
简介 除了配置相关之外,另一个核心就是SQL映射,MyBatis 的真正强大也在于它的映射语句. Mybatis创建了一套规则以XML为载体映射SQL 之前提到过,各项配置信息将Mybatis应用的整 ...
- Mybatis sql映射文件浅析 Mybatis简介(三) 简介
Mybatis sql映射文件浅析 Mybatis简介(三) 简介 除了配置相关之外,另一个核心就是SQL映射,MyBatis 的真正强大也在于它的映射语句. Mybatis创建了一套规则以XML ...
- 理解JNDI中 java:comp/env/jdbc/datasource 与 jdbc/datasource 的不同之处(转)
在描述JNDI,例如获得数据源时,JNDI地址有两种写法,例如同是 jdbc/testDS 数据源: A:java:comp/env/jdbc/testDS B:jdbc/testDS 这两种写 ...
- 【JDBC&Dbutils】JDBC&JDBC连接池&DBUtils使用方法(重要)
-----------------------JDBC---------- 0. db.properties文件 driver=com.mysql.jdbc.Driver url=jdbc: ...
- Unit01: JDBC原理 、 JDBC基础编程
Unit01: JDBC原理 . JDBC基础编程 这个文件里面有两块内容: 1.用比较麻烦的方式连接数据库,test1(),test4() 2.创建DBTool,测试DBTool连接数据库 ,tes ...
- 通明讲JDBC(一)–认识JDBC
本章记录了jdbc的简单使用方式! 0,jdbc的作用 1,jdbc入门准备工作 2,jdbc注册驱动 3,使用jdbc对数据库CRUD 0,jdbc的作用 与数据库建立连接.发送操作数据库的语句并处 ...
- oracle数据库连接问题org.springframework.jdbc.support.MetaDataAccessException: JDBC DatabaseMetaData method not implemented by JDBC driver - upgrade your driver...
org.springframework.jdbc.support.MetaDataAccessException: JDBC DatabaseMetaData method not implement ...
随机推荐
- C++ 用变量定义数组
较早的编译器是不同意这样做的,所以一些书籍比方以Tc解说的书本都说数组的下标不能是变量.在vc6.0下亦是如此. 只是在一些较新的编译器如dev c++已经支持了,例如以下代码不会报错 #includ ...
- 微信跳转ticket值怎么得到?浏览器跳到微信?哪里有微信跳转接口?跳转功能能用多久?
目前很多实用微信跳转技术的电商朋友,网站文章头部或者文章中部出现了点击关注微信关注的二维码,用户点击进去直接跳转到微信内打开指定的二维码,识别即可关注,方便省事,比以往的一键复制—粘贴微信号,转化效果 ...
- python中的矩阵、多维数组
2. 创建一般的多维数组 import numpy as np a = np.array([1,2,3], dtype=int) # 创建1*3维数组 array([1,2,3]) type(a ...
- 使用IDEA时跳转到.class的解决办法
项目背景:jdk1.8 软件环境:IDEA 问题: 1. 两个不同的项目,在A项目中写了一个实体类.B项目中引用.在B项目中CTRL+鼠标左键点击进入,正常情况下是进入了源码文件,也就是.JAVA文件 ...
- WinccFlexible 同一个项目创建多个connections
在一个WinccFlexible 项目中,可以创建多个通讯连接,以满足不同的接口要求. 但是需要在控制面板上 Set PG/PC Interface中添加新的连接,并绑定对应的网卡即可.
- Http_4个新的http状态码:428、429、431、511
1.428 Precondition Required (要求先决条件) 先决条件是客户端发送 HTTP 请求时,必须要满足的一些预设条件.一个好的例子就是 If-None-Match 头,经常用在 ...
- 转发 Learning Go — from zero to hero
原文:https://medium.freecodecamp.org/learning-go-from-zero-to-hero-d2a3223b3d86 Learning Go — from zer ...
- this全解js
转(掘金) this关键字是JavaScript中最复杂的机制之一,是一个特别的关键字,被自动定义在所有函数的作用域中,但是相信很多JavaScript开发者并不是非常清楚它究竟指向的是什么.听说你很 ...
- koa 写简单服务
这两天用koa写了点服务,这里面和express还是有部分区别的 1.静态服务: koa 中,是有中间件, koa-static, const static_f = require('koa-sta ...
- FFmpeg 学习(四):FFmpeg API 介绍与通用 API 分析
一.FFmpeg 相关术语 1. 容器/文件(Container/File):即特定格式的多媒体文件,比如MP4,flv,mov等. 2. 媒体流(Stream):表示在时间轴上的一段连续的数据,比如 ...