JDBC Common Interface Detailed

Source: Internet
Author: User

A detailed description of common interfaces in JDBC

DriverManager

First, the registration drive

The first way: Drivermanager.registerdriver (New Com.mysql.jdbc.Driver ());

First, look at the source code of driver can be seen, if in this way, will cause the driver to register two times, that is, in memory there will be two driver objects.

Second, the program relies on the MySQL API, out of the MySQL jar package, the program will not compile, the future program to switch the underlying database will be very troublesome.

The second way: Class.forName ("Com.mysql.jdbc.Driver");

Features: In this way does not cause the drive object to repeat in memory, and in this way, the program only needs a string, do not need to rely on the specific driver, so that the program flexibility is higher.

Second, get the connection to the database

First way: drivermanager.getconnection (URL, user, password)

Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/day12", "root", "sorry");

Second way: drivermanager.getconnection (URL)

Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/day12?user=root&password=sorry &useunicode=true&characterencoding=utf8 ");

Third Way: drivermanager.getconnection (URL, info) ( This method is most convenient when using a configuration file )

Properties Props = new properties ();

Props.setproperty ("User", "root");

Props.setproperty ("Password", "sorry");

Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/day12", props);

Where the URL is spelled:

Common database URL address notation:

Oracle wording: Jdbc:oracle:thin: @localhost: 1521:sid

SQL Server -jdbc:microsoft:sqlserver://localhost:1433; Databasename=sid

MYSQL -jdbc:mysql://localhost:3306/sid

Short form of MySQL URL address: jdbc:mysql:///sid

Common Properties: Useunicode=true&characterencoding=utf-8

-------------------------------------------------------------------------------------------------------

Connection

It is used to represent the link of the database, collection is the most important object in the database programming, the client and the database all interaction is done through the connection object, this object's Common method:

Createstatement (): Creates a statement object that sends SQL to the database.

Preparestatement (SQL): Creates a precompiled Sqlpreparesatement object to send 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

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.

Note: Parameter SQL can only be a DQL statement. Returns the ResultSet object that encapsulates the result of the query

Executeupdate (String sql): Used to send INSERT, UPDATE, or DELETE statements to the database

NOTE: The parameter SQL is a DML statement that returns the number of rows affected by the statement. You can also execute a DDL statement that does not return a result, and the return value does not represent the number of rows affected.

Execute (String SQL): Used to send arbitrary SQL statements to the database

NOTE: The parameter SQL can be any SQL statement. The returned result does not represent success or not. Returns true if the SQL statement executed has a result set. The other returns false.

Addbatch (String sql): puts multiple SQL statements into one batch.

ExecuteBatch (): Sends a batch of SQL statement execution to the database.

-------------------------------------------------------------------------------------------------------

Preperedstatement

Preperedstatement is a child of statement, and its instance object can be obtained by calling the Connection.preparedstatement () method, as opposed to the statement object:

1) Preperedstatement can avoid problems with SQL injection.

2) statement causes the database to compile SQL frequently, potentially causing a database buffer overflow. PreparedStatement can pre-compile SQL to improve the efficiency of database execution.

3) and preperedstatement for parameters in SQL, it allows the substitution of placeholders to simplify the writing of SQL statements.

-------------------------------------------------------------------------------------------------------

ResultSet

Encapsulates the query results. There is a cursor inside, which points to the front of the first row by default. Next () method: The cursor moves down and returns whether there is a record.

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

Get any type of data

GetObject (int index)

GetObject (String columnName)

Gets the data of the specified type (convenient for encapsulating data) such as:

getString (int index)

GetString (String columnName)

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 (): Moves to the last face of the resultset.

JDBC Common Interface Detailed

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.