Java EE JDBC Core API

Source: Internet
Author: User
Tags driver manager

API for the JDBC interface core

@author Ixenos

Java.sql.* and Javax.sql.*

|-Driver Interface: Represents the Java driver interface. All of the specific database vendors are going to implement this interface.

|-Connect (URL, properties): A way to connect to a database.

    • URL: The URL of the connection database
    • URL syntax: JDBC Protocol: Database Sub-protocol://HOST: Port/Database
    • User: Username for database
    • Password: Database user password

|-DriverManager class: Driver manager class for managing all registered drivers

|-registerdriver (Driver): Registering a Driver class object

|-connection getconnection (Url,user,password); Get Connection Object

|-Connection interface: Represents a Connection object for Java programs and databases.

|-Statement createstatement (): Create Statement Object

|-PreparedStatement preparestatement (String sql) Creating PreparedStatement objects

|-callablestatement preparecall (String sql) Creating CallableStatement objects

|-statement interface: For executing static SQL statements

|-int executeupdate (String sql): Execute a static update SQL statement (DDL,DML)

|-ResultSet executeQuery (String sql): Static query SQL statement executed (DQL)

|-preparedstatement interface: Used to execute precompiled SQL statements

|-int executeupdate (): Perform Precompiled update SQL statement (DDL,DML)

|-resultset executeQuery (): Execute a Precompiled query SQL statement (DQL)

|-callablestatement interface: SQL statement for execution of stored procedures (call XXX)

|-resultset ExecuteQuery (): Methods for calling stored procedures

|-resultset interface: Used to encapsulate the data of the query

|-Boolean next (): Move the cursor to the next line

|-GETXX (): Gets the value of the column

Connection

Connection in the JDBC program, which is used to represent the link of the database, collection is the most important object in database programming, the client and database all interaction is done through the connection object, the common method of this object:

    • Createstatement (): Creates a statement object that sends SQL to the database.
    • Preparestatement (SQL): Creates a Preparesatement object that sends precompiled SQL to the database.
    • Preparecall (SQL): Creates the CallableStatement object that executes the stored procedure.
    • Setautocommit (Boolean autocommit): Sets whether the transaction is automatically committed.
    • Commit (): commits the transaction on the link.
    • Rollback (): Rolls back the transaction on this link.

Statement (including sub-interface PreparedStatement, CallableStatement)

The statement object in the JDBC program is used to send SQL statements to the database, statement object common methods:

    • ExecuteQuery (String sql): Used to send query statements to data.
    • Executeupdate (String sql): Used to send INSERT, UPDATE, or DELETE statements to the database
    • Execute (String SQL): Used to send arbitrary SQL statements to the database
    • Addbatch (String sql): puts multiple SQL statements into one batch.
    • ExecuteBatch (): Sends a batch of SQL statement execution to the database.

ResultSet

The resultset in the JDBC program is used to represent the execution result of the SQL statement. A table-like approach is used when the resultset encapsulates the execution result. The ResultSet object maintains a cursor to the table data row, and initially, the cursor calls the Resultset.next () method before the first row, allowing the cursor to point to a specific row of data and invoke the method to fetch the row's data.

L resultset since it is used to encapsulate execution results, the object provides a GET method for getting the data:

    • Get any type of data

L getObject (int index)

L GetObject (String columnName)

    • Gets the data of the specified type, for example:

L getString (int index)

L getString (String columnName)

    • Question: The type of column in the database is varchar, what method does the data call for that column? What about the int type? What about the bigint type? Boolean type?
    • Note: The index of the column starts at 1.

L ResultSet also provides a way to scroll the result set:

    • Next (): Move to the next line
    • Previous (): Move to previous line
    • Absolute (int row): Move to the specified line
    • Beforefirst (): Moves the front of the resultset.

Afterlast (): Move to the last face of resultset

Attention

When the JDBC program finishes running, remember to release the objects that are created by the program to interact with the database during the run, typically resultset, statement, and connection objects.

L Especially Connection object, it is very rare resources, must be released immediately after use, if the connection can not be timely, correct shutdown, very easy to cause system downtime. Connection's use principle is to create as late as possible, releasing as early as possible. (Use principle: create as late as possible, release as early as possible )

To ensure that the resource release code can run, the resource release code must also be placed in the finally statement.

Java EE JDBC Core API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.