Summary of JDBC usage

  • 2020-04-01 03:37:54
  • OfStack

This example summarizes the use of JDBC. Share with you for your reference. Specific analysis is as follows:

DriverManger: driver manager class
To manipulate the database, you must first create a connection to the database to get the connection object
Public static Connection getConnection(String url, String username,String password){}

The Connection interface:
The connection object is obtained through the getConnection method of the DriverManger class, and the SQL Statement must be executed with the help of a Statement object (Statement).
The Statement createStatement ();

Statement: Statement interface
After the Statement object is obtained through the createStatement method of the connected object, the Statement object can execute the SQL Statement. ExecuteUpdate and executeQuery statements are provided in the Statement to execute different SQL statements
Int executeUpdate(String SQL): a statement that performs an add, delete, or change operation, and returns the total number of rows for the operation
ResultSet executeQuery(String SQL) : executes the query statement and returns the value as a collection of query results

ResultSet: ResultSet interface
Boolean next() : this method moves the result set cursor downward, returning true if there are still records, or false if the traversal has ended

GetXXX (String columnName): this series of methods is used to return the value of a field by its name. There are a number of getXXX(String columnName) methods in the result set interface, such as getString, getInt, etc

GetXXX (int index) : this series of methods is used to return the value of a field based on its index value in the result set. There are a number of getXXX methods in the result set, similar to the above, to get the value of the field based on the index value. XXX is the data type of the field.

JDBC statement object:
Statement: as above STMT = conn. CreateStatement ();
PrepareStatement: a Statement object called precompiled, which is a child interface of a Statement, STMT = conn. PrepareStatement (String SQL), is different from a Statement

public class ConnectTest {
  public static void main(String[] args) {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:ZFY";
    String username = "briup";
    String password = "briup";     Connection conn = null;
    //The first and most common way to establish a database connection is
    try {
      //1. Instantiate the driver class
      //  Class.forName(driverName) ;
      //  OracleDriver driver = new OracleDriver();       //2. Register the driver and get the connection
      //  DriverManager.registerDriver(driver);
      conn = DriverManager.getConnection(url, username, password);
      System.out.println(conn);
     } catch (Exception e) {
        e.printStackTrace();
      }finally{
    //Close the connection
    try {
      if (conn!=null)
      conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
       }       }
   }
}

I hope this article has been helpful to your Java programming.


Related articles: