Database connection JDBC Principle

Source: Internet
Author: User
Tags odbc null null stmt
Database connection 2007-07-25 20:02 Five elements of the database connection:

IP Address
The port number (the port number is basically fixed for a database service unless specifically set) means that the corresponding database driver
Select Database
Account number, password (is assigned to the database)
Emitting SQL statements (sent via statement object) JDBC principle

JDBC is the connection between Java and data. Because ODBC is written entirely in C language, and Java in the implementation of the C language Program communication is more difficult, so the Java language is produced by the Java Program and database connection technology.
JDBC Connection to database

JDBC is connected to a specific database by automatically loading the driver directly through classes in the JDBC API class library. This class library is typically in the java.sql package, which contains classes for implementing other functions connected to the database, including establishing connections with the database, routing queries, and accepting query results. Detailed descriptions of the following four classes:
Role:

Establishing a connection to the database
Send SQL statement
Processing results

DriverManager class This class is used to load the driver, all of its members are static members, so it is not necessary to instantiate it in the program and access it directly through the class name. The DriverManager class is a JDBC management layer that acts between users and drivers

Load Driver
Class.forName ("Company name. Database name. Driver name")
such as: Class.forName ("Sun.jdbc.odbc.jdbcOdbcDriver")

Establish a connection
After loading the driver class and registering with the DriverManager class, you can use it to establish a connection to the database. When the call to Driver.Manager.getConnection () makes a connection request, DriverManager checks each driver to see if it can establish a connection.

Method: Connection getconnection (String url,string user,string password)
Where user and password are users and passwords that log on to the database
The first parameter is the URL that points to the database, which is formatted as follows:
JDBC: (subprotocol):(subname)
Subprotocol: A child protocol that specifies which database to connect to or how to connect to the database
SubName: Establish a connection that can be a data source name or point to an online database

For example: The following are examples of commonly used drivers (JDBC-ODBC bridge drivers) and a student data source with anonymous logins:
Class.forName ("Sun.jdbc.odbc.jdbcOdbcDriver");//Load Driver
String url= "Jdbc:odbc:student";
Connection cn=drivermanager.getconnection (URL, "anonymous", "");
Getconnection (): Returns a Connection class object. If successful, this object points to a connection to this database; otherwise, this object will be null NULL
The connection class connection class is a connection that points to the same database. Role: Manage connections to the database, such as: queries that send queries to the database and query results from the receiving database are based on it, and the connection is closed after all tasks that have connected to the database have been completed.

Method:
Statement createstatment (): Creates a new Statement object that can send query information to the database
void Close (): Closes the connection to the database and frees the occupied JDBC resource
Boolean Isclose (): Determines whether the statement class function is still connected to the database: statement object is used to send SQL statements to the database. When you create a statement object that establishes a connection to a specific database, you can use the connection to send the SQL statement. The statement object is created using the connection method createstatement.
Connection cn=drivermanager.getconnection (Rul, "Sunny", "");
Statement stmt=cn.createstatement ();
In order to execute the statement object, the SQL statement sent to the database will be supplied as a parameter to the statement method
ResultSet rs=stmt.executequery ("Select A,b,c from table2");
Using the Statement Object Execution Statement statement interface provides three ways to execute SQL statements
ExecuteQuery (): statement used to produce a single result set, such as: SELECT statement

Executeupdate (): For INSERT, update, or delete, statement, etc.
The return value is an integer indicating the number of rows affected (that is, the update count)

Execute (): A statement statement completion statement that returns multiple result sets, multiple update counts, or combination of both is considered complete when execution has occurred and all results have returned. For the ExecuteQuery () method that returns a result set, after retrieving the ResultSet object
The statement completes when all rows are in the

For method Executeupdate (), the statement completes when it executes

In the case of a few calls to execute (), the statement completes only after retrieving all the result sets or the update count that it generates
Closing the Statement object statement object is automatically closed by the Java garbage collector. But we'd better show them off because the data management system resources will be released immediately, helping to avoid potential memory problems. Main methods
ResultSet executequery (String sql): Returns a static SQL query result

int executeupdate (String sql): Query the number of inserts, update, deletes, or return 0 in a row of SQL declarations

void Close (): Closes the connection to the database and the JDBC resources it occupies

ResultSet class (Recordset) function: Load query results, and can extract the results of the query through its different methods. ResultSet contains all the rows that conform to the conditions in the SQL statement, and it provides access to the data in those rows through a set of get methods that can access different columns in the current row.
Resultset.next (): Moves the record pointer to the next line in the ResultSet record set to make it the current row.
Note: A record set is a two-dimensional table with the column headings and corresponding values returned by the query.

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.