Part 17th _ Detailed JDBC

Source: Internet
Author: User

JDBC (Java DataBase Connectivity)

    • is a Java API for executing SQL statements
    • Consists of a set of classes and interfaces written in the Java programming language
    • Provides database developers with a standard API that enables them to write database applications using pure Java APIs

Four types of JDBC drivers (learn)

    • JDBC-ODBC Bridge +ODBC Driver: Provides JDBC access using the ODBC driver. Note that ODBC binaries (in many cases, including database client code) must be loaded on each client of the driver
    • Local API: Some drivers written in Java that convert JDBC calls on the client API to Oracle, Sybase, DB2, or other DBMS calls. Note that, like the bridge driver, this type of driver requires some binary code to be loaded on each client computer
    • JDBC Network pure Java driver (frequently used): This driver translates jdbc into a DBMS-independent network protocol, which is then transformed by a server into a DBMS protocol. This Web server middleware can connect its pure Java client to a variety of different databases
    • Native protocol Pure Java driver: This type of driver translates the JDBC call directly into the network protocol used by the DBMS

General Procedures for database connections (important)

    • Loading the JDBC driver (jar)
    • Define connection URL (specific database)
    • Establishing a connection (Connection)
    • Creating a Statement object (JDBC top-level interface)
    • Perform a query or update
    • Result processing
    • Close connection

Load Driver

    • The statements using the Jdbc-odbc Bridge driver are as follows: Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
    • The statements using the Oracle9i JDBC driver are as follows: Class.forName ("Oracle.jdbc.driver.OracleDriver");

Establish a JDBC Connection

    • Getconnection () method for the DriverManager class: public static Connection getconnection (string url, string user, string password) Throws SQLException
    • Oracle provides two types of JDBC drivers

Finally, do not forget to close the connection object and release the resource that is occupied: public void Close () throws SQLException

Send SQL statement

    • The statement object is used to send SQL statements to the database. There are three kinds of Statement objects: statement;preparedstatement (inherited from Statement); CallableStatement (inherited from PreparedStatement)
    • Created by Connection's Createstatement () method: Public Statement createstatement () throws SQLException

ID name1  32  John Doe ResultSet rs = st.executequery ("SELECT * from person"); Rs.next ();//often used to iterate through a table, as a pointer to the current data row, The default pointer to the last line of valid data row int id = res.getint (1); Get the value of the first column in the current row, below int id2 = Res.getint ("id"); String name = rs.getstring (2); String name2 = rs.getstring ("name"); PreparedStatement PS = con.preparestatement ("INSERT into person (ID, name) VALUES (?,?)"); Ps.setint (1, 5); The type of the function call is based on the type of question mark in the field, where 1 represents the first question mark, 5 is the value set Ps.setstring (2, "Wangwu");p S.execute (); Represents the actual execution of the SQL statement

  

Part 17th _ Detailed 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.