0323.JDBC review

/*
JDBC: (Java Database Connectivity) The Java database connection
      program written using JDBC can achieve cross-database, cross-platform, and has excellent portability.
      It is a Java API that can execute SQL (Structured Query Language).
      When switching between databases is required, only the different implementation classes (ie database drivers) need to be changed.
      The JDBC provided by Sun can complete the following three basic functions: (through these three functions, the program can access and operate the database system)
            establish a connection with the database;
            execute SQL statements;
            obtain the execution results of SQL statements.
      There are usually four types of JDBC drivers: P567.
ODBC: (Open Database Connectivity) Open database connection. Microsoft's.
      JDBC is simpler, more intuitive, more secure, and easier to deploy than ODBC.
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --
JDBC 4.2 common interfaces and classes: P602
     
      DriverManager: Service class for managing JDBC drivers, the main function is to obtain Connection objects.
            Connection con=DriverManager.getConnection ( String url , String user , String password ) ;
                          URL is usually written: jdbc:subprotocol:other stuff             
                          MySQL URL: jdbc:mysql://hostname:port/databasename
                                     ("jdbc:mysql:// 127.0.0.1:3306/database_name","root","password")
                          Oracle's URL: jdbc:oracle:thin:@hostname:port:databasename
     
      Connection: represents a database connection object, and each Connection represents a physical connection session.
            Common method: Statement stmt = con.createStatement() throws SQLException;
                             PreparedStatement pstmt = con.prepareStatement(String sql) throws SQLException;
                             CallableStatement cstmt = con.prepareCall(String sql) throws SQLException;
                  The above three methods all return the Statement object used to execute the SQL statement, which can only be executed after the Statement object is obtained SQL statement.
                  PreparedStatement and CallableStatement are subclasses of Statement and
            there are several methods for controlling transactions: P602                                                                      
     
      Statement: A tool interface for executing SQL statements.
            Can be used to execute SQL query statements: mainly completed by the select keyword;
                                  DML (Data Manipulation Language, data manipulation language) statements: insert, update, delete;
                                  DDL (Data Definition Language) statements: create, alter, drop, truncate;
                                  DCL (Data Control Language) statements: grant, revoke.
            Statement common methods:
                  ResultSet executeQuery(String sql) throws SQLException:
                      can only be used to execute the query statement and return the ResultSet result set obtained from the query;
                  int executeUpdate(String sql) throws SQLException:
                      used for DML (returns the number of affected rows), DDL (returns 0);
                  boolean execute(String sql) throws SQLException:
                      Any SQL statement can be executed. Returns true if the first result after execution is a ResultSet object; returns false if the
                                         first result after execution is the number of affected rows or no results.
      PreparedStatement: A precompiled Statement object. Is a subinterface of Statement. P603
            has the following methods more than Statement:
                  void setXxx(int ​​parameterIndex , Xxx value): The incoming value is passed to the parameter at the specified position in the SQL statement according to the index.                                                                                                                                          
     
      ResultSet: The result set object.                                                                                                  
            This object contains methods to access query results, which can be obtained by column index or column name.
            Contains the following common methods to move the record pointer:
                  void close();
                  bollean absolute(int row);
                  void beforeFirst();
                  boolean first();
                  boolean previous();
                  boolean next();
                  boolean last();
                  void afterLast();
            After the record pointer moves to the specified row, the ResultSet can obtain the value of the current row and specified column through the following methods:
                  getXxx(int ​​columnIndex);
                  getXxx(String columnLabel);
                  < T> T getObject(int columnIndex,Class<T> type);
                  <T> T getObject(int columnLabel,Class<T> type);
------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------
JDBC programming steps:
      1. Load the database driver
            Class.forname("com.mysql .jdbc.Driver"); seems to be equivalent to com.mysql.jdbc.Driver driver=new com.mysql.jdbc.Driver();
                         ("oracle.jdbc.driver.OracleDriver"); It seems that after JDBC 4, it is not necessary to write the Class.forname method.
      2. Obtain the database connection through DiverManager
            Connection con=DriverManager.getConnection ( String url , String user , String password ) ;
      3. Create a Statement object through the Connection object.
            Statement stmt=con.createStatement();
      4. Use Statement to execute SQL statement
            Result rs=stmt.executeQuery("select * from tablename");
      5. Operation result set P605 has Figure
      6. Recycle database resources, including closing ResultSet, Resources such as Statement and Connection.
            2 3 4 can be written in try(), and the try statement automatically closes these resources at the end of the statement. Equivalent to including the implicit finally block (Java7)
            these resource implementation classes must implement the AutoCloseable or Closeable interface, and to implement these two interfaces must implement the close() method.
*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483710&siteId=291194637