JDBC

Java Database Connectivity is a set of interface specifications. Java programs connect to the database through JDBC, and then execute SQL through it, and operate on the database.

DBC is the interface specification defined by Sun Company, the specific implementation is handed over to each database vendor to achieve, because each database has its particularity, these are Java specifications can not be determined

import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; public class JdbcExample { public static void main(String[] args) { JdbcExample example = new JdbcExample(); Role role = example.getRole(1L); System.out.printf("role_name => " + role.getRoleName()); } public Role getRole(Long id) { Connection connection = this.getConnection(); PreparedStatement ps = null; ResultSet rs = null; Try {// operation Connection, Open the Statement object ps = connection. PrepareStatement (" select id, role_name, note the from t_role where id =?" ); ps.setLong(1,id); Rs = ps.executeQuery(); rs = ps.executeQuery(); While (rs.next()){Long roleId = rs.getLong("id"); Long roleId = rs.getLong("id"); String roleName = rs.getString("role_name"); String note = rs.getString("note"); Role role = new Role(); role.setId(id); role.setRoleName(roleName); role.setNote(note); return role; } } catch (SQLException e) { Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e); } finally { this.close(rs,ps,connection); } return null; } private Connection getConnection(){private Connection getConnection(){Connection Connection = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/mybatis1? characterEncoding=utf8"; String user = "root"; String password = "root"; connection = DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException | SQLException e) { Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e); return null; } return connection; } private void close(ResultSet rs,Statement STMT,Connection Connection){// Close database related resources try {if (rs! = null && ! rs.isClosed()){ rs.close(); } } catch (SQLException e) { Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e); } try { if (stmt ! = null && ! stmt.isClosed()){ stmt.close(); } } catch (SQLException e) { Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e); } try { if (connection ! = null && ! connection.isClosed()){ connection.close(); } } catch (SQLException e) { Logger.getLogger(JdbcExample.class.getName()).log(Level.SEVERE,null,e); }}}Copy the code
  • Register database driver class, specify database address, including DB user name, password and other connection information;
  • Call the DriverManager. GetConnection () method to create Connection to connect to the database;
  • Call the createStatement() or prepareStatement() method in Connection to create the Statement object, which specifies the SQL (or SQL Statement template + SQL parameters).
  • You can execute SQL statements through the Statement object to obtain a ResultSet object, which is the query ResultSet.
  • Iterate through the ResultSet, read data from the ResultSet, and convert each row of database records into a JavaBean object;
  • Close the ResultSet ResultSet, Statement object, and database Connection to release underlying resources occupied by these objects.

ORM

ORM (Object Relational Mapping) framework to encapsulate 1 to 6 steps of repetitive code, realize the transformation between Object model and Relational model.

Common ORM framework Mybatis, Hibernate

JPA

JPA is a Java persistence specification (JSR 338) that was introduced after JDK 5.0. The JPA specification itself is to integrate existing ORM frameworks on the market, end the separation of ORM frameworks such as Hibernate, EclipseLink and JDO, and simplify Java persistence layer development.

Spring Data JPA

Spring Data JPA is a Repository layer implementation that complies with the JPA specification

Although most OF the ORM frameworks on the market implement the JPA specification, they have their own development and modification on the basis of JPA. As a result, when we use JPA, we still cannot seamlessly switch the underlying ORM framework implementation. With Spring Data JPA, we smoothen the differences between ORM frameworks so that our upper-layer business can seamlessly switch ORM implementation frameworks.

\