FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Java_Database_Connectivity

Java Database Connectivity

JDBC
DeveloperOracle Corporation
java.sql and UPDATE and java.sql.Statement – sent to the database server each and every time. In other words, the java.sql.Statement methods are executed using SQL statements to obtain a java.sql.ResultSet object containing the data.
  • javax.sql.

    JDBC connections are often managed via a connection pool rather than obtained directly from the driver, like javax.sql.DataSource.

    Examples

    edit

    When a Java application needs a database connection, one of the DriverManager.getConnection() methods is used to create a JDBC java.sql.Connection. The URL used is dependent upon the particular database and JDBC driver. It will always begin with the jdbc: protocol, but the rest is up to the particular vendor.

    importjava.sql.Statement; try(Statementstmt=conn.createStatement()){ stmt.executeUpdate("INSERT INTO MyTable(name) VALUES ('my name')"); }

    Note that java.sql.Connections, java.sql.Statements, and java.sql.ResultSets often tie up operating system resources such as sockets or file descriptors. In the case of java.sql.Connections to remote database servers, further resources are tied up on the server, e.g. cursors for currently open java.sql.ResultSets. It is vital to close() any JDBC object as soon as it has played its part; garbage collection should not be relied upon. The above try-with-resources construct is a code pattern that obviates this.

    Data is retrieved from the database using a database query mechanism. The example below shows creating a statement and executing a query.

    SQLException. There is typically very little one can do to recover from such an error, apart from logging it with as much detail as possible. It is recommended that the java.sql.SQLException be translated into an application domain exception (an unchecked one) that eventually results in a transaction rollback and a notification to the user.

    The following code is an example of a database transaction:

    1. 1 2 "Sun Ships JDK 1.1 -- Javabeans Included". www.sun.com. Sun Microsystems. February 19, 1997. Archived from the original on February 10, 2008. Retrieved February 15, 2010. February 19, 1997 - The JDK 1.1 [...] is now available [...]. This release of the JDK includes: [...] Robust new features including JDBC for database connectivity
    2. ↑ JDBC API Specification Version: 4.0.
    3. ↑ "The Java Community Process(SM) Program - communityprocess - mrel". jcp.org. Retrieved March 22, 2018.
    4. ↑ "java.sql (Java SE 9 & JDK 9)". docs.oracle.com. Retrieved March 22, 2018.
    5. 1 2 3 4 Bai 2022, p. 74.
    6. ↑ Bai 2022, pp. 122–124, §4.2.3.5 More About the Execution Methods.
    7. 1 2 3 Bai 2022, pp. 72–74, §3.2 JDBC Components and Architecture.
    8. ↑ Horstmann 2022, §5.5.3 SQL Escapes.
    9. 1 2 Bai 2022, pp. 122–124, §4.2.3.5 JDBC Components and Architecture.
    10. ↑ Bai 2022, p. 83, §3.5.1 JDBC DataSource.
    11. ↑ "Access External Databases from RPG with JDBCR4 Meat of the Matter". June 28, 2012. Retrieved April 12, 2016.
    12. ↑ Horstmann, Cay (April 15, 2022). Core Java. Oracle Press Java. ISBN 978-0-13-787107-0.
    13. External links

      edit
      Wikimedia Commons has media related to JDBC.
    Wikibooks has a book on the topic of: Java Programming/Database Programming