S2/java/08-jdbc

Source: Internet
Author: User
Tags stmt

The JDBC API does 3 things: Establish a connection to the database, send SQL statements, and process the results.

DriverManager class: Depending on the database, the corresponding JDBC driver is managed .

Connection Interface: Responsible for connecting the database and serving the task of transmitting data.

Statement Interface: generated by Connection , responsible for executing SQL statements.

ResultSet Interface: Responsible for saving and processing The results of the query resulting from Statement execution.

PreparedStatement Interface:Statement Sub-interface, also Connection generated, is also responsible for executing SQL statements. It has the advantages of high security, high performance, high readability and high maintainability compared with the Statement interface.

the steps for JDBC to access the database.

(1) load the JDBC driver.

Use the class.forname () method to load a given JDBC driver class into a Java virtual machine. If a given class does not exist in the system, an exception is thrown and the exception type is classnotfoundexception. code example:

Class.forName (" name of the JDBC driver Class ");

(2) Establish a connection to the database

The DriverManager class is the management layer of JDBC, which acts between the user and the driver. The DriverManager class tracks the available drivers and establishes a connection between the database and the appropriate driver. When the getconnection () method is called, theDriverManager class first finds a driver from the list of loaded drivers that can receive the database URL . The driver is then asked to connect to the database using the associated URL, user name, and password, and the connection to the database isestablished, the connection object is created, and the reference is returned. code example:

Connection con=drivermanager.getconnection ( data connection string, database user name, password );

(3) Send The SQL statement and get the result returned.

Once the connection is established, the object of the Statement interface is created using the connection and the SQL statement is passed to the database to which he is connected. In the case of a query operation, a result set of type ResultSet is returned , which contains the result of executing the SQL query. If it is a different operation, the number of records affected by the Boolean value or operation will be returned depending on the calling method. code example:

Statement stmt=con.createstatement ();

ResultSet rs=stmt.executequery ("Select Id,name from Master");

(4) processing return results

It is mainly for the result set of the query operation, through the loop to take out each record in the result set and make corresponding processing. Sample code to process the result:

while (Re.next ()) {

int Id=rs.getint ("id");

String name=rs.getstring ("name");

System.out.println (id+ "" +name);

}

Two commonly used driving methods: The first is the Jdbc-odbc Bridge connection method, suitable for personal development and testing, he connects with the database through ODBC. The second is a pure Java Drive, he directly connected to the database, in the production of development, it is recommended to use a pure Java Drive mode.

Connect to the database using Jdbc-odbc Bridge, theJDBC driver class is "Sun.jdbc.odbc.JdbcOdbcDriver" and the database connection string will be "JDBC:ODBC" start, followed by the data source name. So, assuming that we have configured an ODBC data source called "Conn_epet" , the database connection string is "Jdbc:odbc:conn_epet", assuming the user name of the logged-on database system is " sa", the password is "sa". The specific implementation code is shown in Example 1 .

We use a pure Java drive to connect to the database, we first need to download the database vendor-supplied driver jar Package, and bring the jar package into the project. Next, you can program and establish a connection to the database. It is assumed that a database named "Epet" has been established in SQL Server2008 , the database user name is " sa", the password is "sa", and the driver package is Sqljdbc2008.jar.

After getting the Connection object, you can perform various database operations, and you need to create the Statement object using the Connection object . The Statement object is used to send SQL statements to the database, or to execute SQL statements. The Statement interface contains a number of basic database operations methods, the following 3 methods for executing SQL commands .

ResultSet executeQuery (String sql): You can execute a SQL query and get the ResultSet object.

int executeupdate (String sql): You can perform an INSERT, delete, update operation. The return value is the number of rows affected by performing the operation.

Boolean execute (String sql): you can execute any SQL statement that returns true if the result is a ResultSet object, or if it is an update count or there is no result. false.

S2/java/08-jdbc

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.