5,JDBC

 

About JDBC
· · · Concept:
    · JDBC is a set of standards java database connection, you can use a unified API operation of various databases.
· · · Relations:
    · Java.sql. * Is defined set of interfaces, the major database vendors drive to implement this interface, so as to achieve compatibility.
 
1, using JDBC
··· Steps for usage:
    1, the registration drive. Jar package is loaded into the drive JVM, Class.forName ( "com.mysql.jdbc.Driver");
    2, create a connection. The driver creates a database connection, DriverManager.getConnection (url, name, pwd);
    3. Create a Statement execution of the statement. conn.createStatement ();
    4, the implementation of SQL, get results. statement.execute (sql);
    5, the processing result.
    6, close the resource.
··· Precautions:
    ·  From jdk5 you do not need to register after the driver, DriverManager will find themselves driven to the CLASSPATH.
    ·  MySQL5 drive is com.mysql.jdbc.Driver, MySQL6 and above are com.mysql.cj.jdbc.Driver.
    · MySQL6 and above in connection url to add time zones,? ServerTimezone = Asia / Shanghai, otherwise an error.
    ·  When the database connection object is no longer referenced, will be recovered GC, GC but recovered only connection object resources JVM memory, and system resources associated with the connection object can not be recycled GC, it is necessary to call close ( ) method to close the system resources. For mysql database, socket disconnected after the JVM exits, the server will close the connection.
 
2,Statement
· · · Role: connection created by the database used to execute SQL statements.
· · · Instance method:
    ·  ExecuteQuery (SQL): used to execute a query, select. Return result ResultSet type.
    ·  ExecuteUpdate (SQL): used to perform the update statement, insert, update and delete. And returns the number of rows affected.
    ·  The Execute (SQL): you can perform the above two types of statements, returns true if it is select. Not recommended.
··· PreparedStatement:
    ·  Precompiled Statement, can completely avoid SQL injection. Recommended Use.
    ·  Write your SQL statement and use? As a placeholder; after pre-compiler, then () provided by the setObject? The transmission parameter placeholder character.
 
3,ResultSet
· · · Effect: the result set type select statement returned.
··· Precautions:
    · GetObject (String name); is to obtain the data according to the alias select the column.
    ·  When you call the close ResultSet () method, can no longer call the next () method to access the data.
 
4, JDBC control transactions
··· 概念:MySQL默认自动提交事务,即每条语句就是一个事务。 
··· 使用步骤:
    1,将connection的自动提交设为false。conn.setAutoCommit(false);
    2,手动提交事务。conn.commit();
    3,如果异常就回滚。conn.rollback();
 
5,DBCP数据库连接池
··· 作用:解决建立数据库连接需要消耗很多资源和时间的问题,可以提高性能。
··· DBCP:
    · 准备:commons-pool.jar、commons-dbcp.jar;配置文件:dbcp.properties。
    · 使用步骤:
        1,加载配置文件:properties.load("dbcp.properties");
        2,初始化DataSource:BasicDataSourceFactory.createDataSource(properties);
        3,获得Connection:dataSource.getConnection();
··· 注意事项:
    · 自定义数据库连接池要实现javax.sql.DataSource接口。
 
6,DBUtils
··· 作用:DBUtils是数据库操作实用工具,封装了对JDBC的操作。类似于ORM框架。
··· 优点:
    · 对于数据表读操作,可以把结果转换为List,Array,Set等java集合,方便操作。
    · 对于写操作也变得简单,只需要写SQL。
    · 可以通过数据库连接池来优化性能(连接复用)。
··· 三个核心对象:QueryRunner类,ResultSetHandler接口,DBUtils类。
··· QueryRunner:
    · 作用:提供对sql语句操作的API。导入commons-dbutils.jar
    · 例子:QueryRunner queryRunner = new QueryRunner(dataSource);
    · query():用来执行select操作。
    · update():用来执行insert,update,delete操作。
    · batch():批操作。
··· ResultSetHandler:
    · 作用:用于select操作后,怎么封装结果集。
    · 例子:Cat cat = queryRunner.query(sql, new BeanListHandler<>(Cat.class));
··· DBUtils:
    · 作用:工具类,用来关闭资源,与事务的处理的方法。

 

Guess you like

Origin www.cnblogs.com/shendeng23/p/12466504.html
Recommended