Java Database Connectivity
Java Database Connectivity
JDBC
The Java Database Connectivity (JDBC) API is the industry standard for database-independent
connectivity between the Java programming language and a wide range of databases – SQL
databases and other tabular data sources, such as spreadsheets or flat files. The JDBC API
provides a call-level API for SQL-based database access.
JDBC technology allows you to use the Java programming language to exploit "Write Once, Run
Anywhere" capabilities for applications that require access to enterprise data. With a JDBC
technology-enabled driver, you can connect all corporate data even in a heterogeneous
environment.
Functionality
JDBC allows multiple implementations to exist and be used by the same application. The API
provides a mechanism for dynamically loading the correct Java packages and registering them
with the JDBC Driver Manager. The Driver Manager is used as a connection factory for creating
JDBC connections.
JDBC connections support creating and executing statements. These may be update statements
such as SQL's CREATE, INSERT, UPDATE and DELETE, or they may be query statements
such as SELECT. Additionally, stored procedures may be invoked through a JDBC connection.
JDBC represents statements using one of the following classes:
• Statement – the statement is sent to the database server each and every time.
• PreparedStatement – the statement is cached and then the execution path is pre
determined on the database server allowing it to be executed multiple times in an efficient
manner.
• CallableStatement – used for executing stored procedures on the database.
Update statements such as INSERT, UPDATE and DELETE return an update count that
indicates how many rows were affected in the database. These statements do not return any other
information.
Query statements return a JDBC row result set. The row result set is used to walk over the result
set. Individual columns in a row are retrieved either by name or by column number. There may
be any number of rows in the result set. The row result set has metadata that describes the names
of the columns and their types.
Statement stmt=con.createStatment();
Class.forName():
The method Class.forName(String) is used to load the JDBC driver class. The line below
causes the JDBC driver from some jdbc vendor to be loaded into the application.
Eg> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
Set methods are used in JDBC for creating a new row or to update an existing row in a result set.
Set methods:
CLOB setClob()
BLOB setBlob()
RAW setBytes()
LONGRAW setBytes()
try {
ps = c.prepareStatement("INSERT INTO authors VALUES (?, ?, ?)");
ps.setInt(1, 495);
ps.setString(2, "Light-Williams");
ps.setString(3, "Corwin");
ps.executeUpdate();
}
catch (SQLException se)
{
System.out.println("We got an exception while preparing a statement:" +
"Probably bad SQL.");
System.exit(1);
}
setInt(1, 495);
GET Method
Get methods are used to retrieve values from the result set.
while(rs.next())
System.out.println(rs.getString("username"));
Step1: Create a Database with a table in Microsoft Access and save it.
Step3: Create a Java program and use the created datasource name in getConnection() function.
import java.io.*;
import java.sql.*;
Connection con=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:DataSourceName ");
Statement stmt=con.createStatment();
while(rs.next())
System.out.println(rs.getString("COLUMNNAME"));