Java JDBC Tutorial
Java JDBC Tutorial
Java JDBC Tutorial
JDBC Introduction
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 API
for executing SQL statements and supports basic SQL functionality. It provides RDBMS
access by allowing you 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 will 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.
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.
Before you can create a java jdbc connection to the database, you must first
import the java.sql package. “import java.sql.*;”. The star ( * ) indicates that all of the
classes in the package java.sql are to be imported.
try{
Connection
dbConnection=DriverManager.getConnection(url,"loginName","Password")
}
catch( SQLException x ){
System.out.println( "Couldn't get connection!" );
}
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,
the method to use is executeQuery . For statements that create or modify tables, the
method to 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 that is written as String object.
*****