Java JDBC Tutorial__java

Source: Internet
Author: User
Tags odbc url example

The JDBC (Java Database Connectivity) API defines interfaces and classes for writing Database
Applications in Java by making database connections. Using JDBC You can send SQL, pl/sql
Statements to almost any relational database. JDBC is a Java APIs for executing SQL statements
and supports basic SQL functionality. IT provides RDBMS access by allowing your to embed SQL
Inside Java code. Because Java can run on a thin client, applets embedded in Web pages can
Contain downloadable JDBC code to enable remote database access. You'll learn how to
Create a table, insert values into it, query the table, retrieve results, and update the table with
The help of a JDBC program example.

Although JDBC is designed specifically to provide a Java interface to relational databases,
You'll find this need to write Java code to access non-relational databases as. JDBC Architecture

Java application calls the JDBC library. JDBC loads a driver which talks to the database.
We can change database engines without changing database code. JDBC Basics-java Database Connectivity Steps

Before can create a Java JDBC connection to the database, you must the
java.sql package.

Import java.sql.*; The Star (*) indicates the classes in the package java.sql are
To be imported.

1. Loading a database driver,

In the "step of" the JDBC connection process, we load the driver class by calling Class.forName ()
With the Driver class name as a argument. Once loaded, the Driver class creates an instance
of itself. A client can connect to Database Server through JDBC Driver. Since most of the
Database Servers Support ODBC driver therefore JDBC-ODBC Bridge driver is commonly used.
The return type of the Class.forName (String ClassName) method is "Class". Class is a class in
Java.lang package.


2. Creating a Oracle JDBC Connection

the JDBC DriverManager class Defines objects which can connect Java applications to a JDBC driver. DriverManager is considered the backbone of JDBC architecture. DriverManager class manages
The JDBC drivers that are installed on the system. Its getconnection () are used to establish
a connection to a database. It uses a username, password, and a JDBC URL to establish a connection
to the database and returns a Connection object. A JDBC Connection represents a session/connection
with a specific database. Within the context of a Connection, SQL, pl/sql statements are executed
and results are. An application can have one or more connections with a single database,
or it can have many connections with different Databases. A Connection object provides metadata i.e. information about the database, tables, and fields. It also contains methods to deal with transactions.

JDBC URL Example:: jdbc: <subprotocol>: <subname> Each driver has its own subprotocol
each subprotocol has its own syntax for the source. We are using the JDBC ODBC Subprotocol, so the DriverManager knows to use the sun.jdbc.odbc.JdbcOdbcDriver.



3. Creating a JDBC Statement object,

Once A connection are obtained we can interact with the database. Connection interface defines
Methods for interacting with the database via the established connection. To execute SQL
Statements, you need to instantiate a Statement object from your connection object by using the Createstatement () method.

Statement Statement = Dbconnection.createstatement ();

A Statement object is used to send and execute SQL statements to a database.

Three Kinds of statements

Statement: Execute simple SQL queries without parameters.
Statement createstatement ()
Creates an SQL Statement object.

Prepared Statement: Execute precompiled SQL queries with or without parameters.
PreparedStatement preparestatement (String sql)
Returns a new PreparedStatement object. PreparedStatement objects are precompiled
SQL statements.

callable Statement: Execute a call to a database stored procedure.
CallableStatement preparecall (String sql)
Returns a new CallableStatement object. CallableStatement objects are SQL stored procedure
Call statements.

4. Executing a SQL statement with the statement object, and returning a JDBC ResultSet.

Statement interface defines methods that are used to interact with database via the execution
of SQL statements. The Statement class has three methods for executing statements:
ExecuteQuery (), executeupdate (), and execute (). For a SELECT statement, which is executequery. For statements This create or modify tables, the method to the use is executeupdate. Note:statements that create a table, alter a table, or drop a table are all examples of DDL
Statements and are executed with the method executeupdate. Execute () executes an SQL
Statement is written as String object.

ResultSet provides access to a table of the data generated by executing a Statement. The table
Rows are retrieved in sequence. A ResultSet maintains a cursor pointing to it current
row of data. The next () method was used to successively step through the rows of the tabular results.

ResultSetMetaData Interface holds information on the types and properties of the columns in
A ResultSet. It is constructed from the Connection object.

Test JDBC Driver installation


Download JDBC Sample Code

Java jdbc Connection Example, jdbc Driver Example

Related Article

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.