JDBC设计理念浅析 JDBC简介(一)
2019-01-15 07:03:14来源:博客园 阅读 ()
概念


JDBC功能核心
数据库查询
- 连接数据库
- 执行SQL
- cmd打印结果
- 连接数据库
- 执行SQL
- 处理返回结果
JDBC架构设计
- 提供了一套纯粹的JAVA API给应用程序开发者
- 提供了一套低级别的JDBC driver API给数据库驱动开发者
JDBC API
官方相关介绍
What the
java.sql Package Contains
java.sql package contains API for the following:
- Making a connection with a database via the
DriverManagerfacility 通过驱动管理器工具与数据库建立连接 -
-
DriverManagerclass 类 -- makes a connection with a driver 与驱动程序建立连接 -
SQLPermissionclass 类-- provides permission when code running within a Security Manager, such as an applet, attempts to set up a logging stream through theDriverManager在安全管理器(如applet)中运行的代码试图通过驱动程序管理器设置日志流时,提供权限 -
Driverinterface 接口 -- provides the API for registering and connecting drivers based on JDBC technology ("JDBC drivers"); generally used only by theDriverManagerclass提供基于JDBC技术的驱动程序注册和连接API(“JDBC驱动程序”);通常只被DriverManager类使用 -
DriverPropertyInfoclass 类 -- provides properties for a JDBC driver; not used by the general user 为JDBC驱动程序提供属性;一般用户不使用
-
- Sending SQL statements to a database 向数据库发送SQL语句
-
Statement-- used to send basic SQL statements 执行对象,用于发送基本的SQL语句 -
PreparedStatement-- used to send prepared statements or basic SQL statements (derived fromStatement) 用于发送准备好的语句或基本SQL语句(从Statement派生) -
CallableStatement-- used to call database stored procedures (derived fromPreparedStatement) 用于调用数据库存储过程(从PreparedStatement 派生) -
Connectioninterface 接口 -- provides methods for creating statements and managing connections and their properties 提供用于创建语句和管理连接及其属性的方法 -
Savepoint-- provides savepoints in a transaction 在事务中提供Savepoint保存点
-
- Retrieving and updating the results of a query 检索和更新查询的结果
-
ResultSetinterface 接口
-
- Standard mappings for SQL types to classes and interfaces in the Java programming language SQL类型到Java编程语言中的类和接口的标准映射
-
Arrayinterface 接口-- mapping for SQLARRAY SQL ARRAY映射 -
Blobinterface 接口-- mapping for SQLBLOB SQL BLOB映射 -
Clobinterface接口 -- mapping for SQLCLOB SQL CLOB 映射 -
Dateclass 类-- mapping for SQLDATE -
NClobinterface 接口 -- mapping for SQLNCLOB -
Refinterface 接口-- mapping for SQLREF -
RowIdinterface 接口-- mapping for SQLROWID -
Structinterface 接口-- mapping for SQLSTRUCT -
SQLXMLinterface 接口-- mapping for SQLXML -
Timeclass 类-- mapping for SQLTIME -
Timestampclass 类-- mapping for SQLTIMESTAMP -
Typesclass 类-- provides constants for SQL types 为SQL类型提供常量
-
- Custom mapping an SQL user-defined type (UDT) to a class in the Java programming language 自定义将SQL用户定义类型(UDT)映射到Java编程语言中的类
-
SQLDatainterface -- specifies the mapping of a UDT to an instance of this class 指定UDT到该类实例的映射 -
SQLInputinterface -- provides methods for reading UDT attributes from a stream 提供从流中读取UDT属性的方法 -
SQLOutputinterface -- provides methods for writing UDT attributes back to a stream 提供将UDT属性写回流的方法
-
- Metadata 元数据
-
DatabaseMetaDatainterface -- provides information about the database 提供有关数据库的信息 -
ResultSetMetaDatainterface -- provides information about the columns of aResultSetobject 提供有关ResultSet对象的列的信息 -
ParameterMetaDatainterface -- provides information about the parameters toPreparedStatementcommands 为PreparedStatement命令提供有关参数的信息
-
- Exceptions 异常
-
SQLException-- thrown by most methods when there is a problem accessing data and by some methods for other reasons 当访问数据存在问题时大多数方法都会抛出这个异常,还有一些方法是其他原因抛出这个异常。 -
SQLWarning-- thrown to indicate a warning 抛出以表示警告 -
DataTruncation-- thrown to indicate that data may have been truncated 抛出以指示数据可能已被截断 -
BatchUpdateException-- thrown to indicate that not all commands in a batch update executed successfully 抛出以指示批处理更新中并非所有命令都已成功执行
-
核心
一般流程
第一个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(); } }
结果

总结
原文链接:https://www.cnblogs.com/noteless/p/10265236.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- JSP+Structs+JDBC+mysql实现的诚欣电子商城 2020-06-08
- Spring11_JdbcTemplate 2020-06-07
- 数据的存储结构浅析LSM-Tree和B-tree 2020-06-04
- JSP+Servlet+JDBC+mysql实现的学生成绩管理系统 2020-05-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
