JDBC---概述
/** * 【JDBC】 * what? * java database connectivity * * java.sql.DriverManager * The basic service for managing a set of JDBC drivers. * DriverManager 是 一个 管理 JDBC驱动 的服务; * * The {@link javax.sql.DataSource} interface, new in the JDBC 2.0 API, provides another way to connect to a data source. * The use of a DataSource object is the preferred means of connecting to a data source. * javax.sql.DataSource 接口 是 JDBC 2.0 新增的API,提供了 另一种方式 连接一个 数据源; * 推荐 使用 DataSource 连接 一个数据源; * * As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property. * This allows a user to customize the JDBC Drivers used by their applications. * DriverManager 的初始化块中,尝试加载 系统属性jdbc.drivers [System.getProperty("jdbc.drivers")] 引用的驱动; * DriverManager 允许 用户自定义 JDBC驱动 ; * * When the method getConnection is called,the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application. * 当 调用 DriverManager 的getConnection(), 将会 从初始化块 或 使用类加载器 找到合适的 驱动; * * * java.sql.Connection * A connection (session) with a specific database. * SQL statements are executed and results are returned within the context of a connection. * 指定数据库的 一个 connection; * 在 一个connection上下文 中,SQL语句 将被执行 且 结果将被返回; * * A Connection object's database is able to provide information describing its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on. * This information is obtained with the getMetaData method. * 一个Connection的数据库 应该提供表信息、SQL语法、存储过程... * 通过 Connection的 getMetaData() 可以获取 数据库的元数据; * * When configuring a Connection, JDBC applications should use the appropriate Connection method such as setAutoCommit or setTransactionIsolation. * Applications should not invoke SQL commands directly to change the connection's configuration when there is a JDBC method available. * By default a Connection object is in auto-commit mode, which means that it automatically commits changes after executing each statement. * If auto-commit mode has been disabled, the method commit must be called explicitly in order to commit changes; otherwise, database changes will not be saved. * 配置一个Connection时,JDBC应用程序 应该使用合适的Connection方法 (比如 setAutoCommit 、setTransactionIsolation); * 当 有一个可用的JDBC可用时,应用程序 不能 直接 调用SQL 直接改变 Connection的配置; * 默认情况下,一个 Connection 是自动提交事务; * 如果 自动提交 被禁用,必须显式 提交,否则数据库将不会保存; * * A new Connection object created using the JDBC 2.1 core API has an initially empty type map associated with it. * 使用 JDBC2.1 API 创建一个新的Connection 会关联一个 初始化的map类型; * * public class ConnectionImpl implements JdbcConnection, SessionEventListener, Serializable { * private NativeSession session = null; * } * * * java.lang.AutoCloseable * An object that may hold resources (such as file or socket handles) until it is closed. * The {@link #close()} method of an {@code AutoCloseable} object is called automatically when exiting a {@code try}-with-resources block for which the object has been declared in the resource specification header. * This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. * AutoCloseable 是一个 持有资源(比如 文件、Socket处理) 直到 资源被关闭; * 当 存在try{} AutoCloseable 的 close方法 将被调用 ; * 这种 结构 确保资源释放; * * java.sql.Statement * The object used for executing a static SQL statement and returning the results it produces. * Statement 被用来 执行静态SQL 且 返回执行结果; * * By default, only one <code>ResultSet</code> object per <code>Statement</code>object can be open at the same time. * All execution methods in the <code>Statement</code> interface implicitly close a current <code>ResultSet</code> object of the statement if an open one exists. * 默认情况,一个 Statement 同一时间 只能有一个 ResultSet; * Statement 执行的方法 必须显式 关闭ResultSet; * * * java.sql.PreparedStatement * An object that represents a precompiled SQL statement. * A SQL statement is precompiled and stored in a <code>PreparedStatement</code> object. * This object can then be used to efficiently execute this statement multiple times. * PreparedStatement 代表一个预编译的SQL statement; * 一个 SQL statement 被 预编译 且 存储在 PreparedStatement中; * PreparedStatement 能被高效率的执行; * * * 【PreparedStatement 与 Statement比较】 * 1、PreparedStatement 预编译SQL,性能更好; * 只需要处理参数,不需要每次重新传入SQL; * 2、PreparedStatement 不需要 拼接处理参数,易编写易读; * 3、PreparedStatement 防止SQL注入; * 将 传入的参数 整个字符串 作为 某个字段的值 处理,而不是作为 SQL的一部分; * * * java.sql.ResultSet * A table of data representing a database result set, which is usually generated by executing a statement that queries the database. * ResultSet 代表 数据库的结果集; * * A <code>ResultSet</code> object maintains a cursor pointing to its current row of data. * Initially the cursor is positioned before the first row. * The <code>next</code> method moves the cursor to the next row, and because it returns <code>false</code> when there are no more rows in the <code>ResultSet</code> object,it can be used in a <code>while</code> loop to iterate through the result set. * ResultSet 包含一个 游标 对象(指向 当前的数据行); * 游标 初始 指向 第一行; * ResultSet的 next方法 移动游标到下一行,当没有数据返回时 return false,因此 可以使用 while 迭代处理数据; * */