JDBC & Odbc
JDBC & Odbc
o
Advantages:
o easy to use.
o can be easily connected to any database.
Disadvantages:
o Performance degraded because JDBC method call is
converted into the ODBC function calls.
o The ODBC driver needs to be installed on the client
machine.
2) Native-API driver
The Native API driver uses the client-side libraries of the
database. The driver converts JDBC method calls into native
calls of the database API. It is not written entirely in java.
Advantage:
o performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
o The Native driver needs to be installed on each client
machine.
o The Vendor client library needs to be installed on client
machine.
4) Thin driver
The thin driver converts JDBC calls directly into the
vendor-specific database protocol. That is why it is known
as thin driver. It is fully written in Java language.
Advantage:
o Better performance than all other drivers.
o No software is required at client side or server side.
Disadvantage:
o Drivers depend on the Database.
ODBC JDBC
ODBC Stands for Open Database JDBC Stands for java
Connectivity. database connectivity.
Introduced by SUN Micro
Introduced by Microsoft in 1992. Systems in 1997.
We can use ODBC for any language We can use JDBC only for
like C,C++,Java etc. Java languages.
We can choose ODBC only windows We can Use JDBC in any
platform. platform.
Mostly ODBC Driver developed in native JDBC Stands for java
languages like C,C++. database connectivity.
For Java applications it is not For Java application it is
recommended to use ODBC because highly recommended to use
performance will be down due to internal JDBC because there are no
conversion and applications will become performance & platform
platform Dependent. dependent problem.
ODBC is procedural. JDBC is object oriented.
CRUD OPERATION
Creating, reading, updating, and deleting data in a database is a common
task in many applications, and JDBC (Java Database Connectivity) is a Java
API that allows you to connect to a database and perform these operations.
1. Connect to the database
The first step is to establish a connection to the database. You can do this by loading the JDBC
driver and creating a connection object.
try {
Class.forName("com.mysql.jdbc.Driver");
"jdbc:mysql://localhost:3306/mydb", "username",
"password");
System.out.println("Connection established.");
catch (Exception e) {
e.printStackTrace();
}
2. Create a new record
Once you have a connection to the database, you can use the connection
object to create a new record in the database. To do this, you will need to use
an SQL INSERT statement and execute it using the connection object.
try {
String sql = "INSERT INTO table_name (column1, column2, column3) VALUES
(?, ?, ?)";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, "value1");
statement.setString(2, "value2");
statement.setInt(3, 123);
statement.executeUpdate();
System.out.println("Record created.");
} catch (SQLException e) {
e.printStackTrace();
}
3. Read a record
To read a record from the database, you will need to use an SQL SELECT
statement and execute it using the connection object. The result of the query
will be a ResultSet object that you can use to access the data in the record.
try {
String sql = "SELECT column1, column2, column3 FROM table_name WHERE
id = ?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, 1);
ResultSet result = statement.executeQuery();
if (result.next()) {
String column1 = result.getString("column1");
String column2 = result.getString("column2");
int column3 = result.getInt("column3");
System.out.println("Column 1: " + column1);
System.out.println("Column 2: " + column2);
System.out.println("Column 3: " + column3);
}
} catch (SQLException e) {
e.printStackTrace();
}
4. Update a record
To update a record in the database, you will need to use an SQL UPDATE
statement and execute it using the connection object.
try {
String sql = "UPDATE table_name SET column1 = ?, column2 = ?, column3 = ?
WHERE id = ?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, "new_value1");
statement.setString(2, "new_value2");
statement.setInt(3, 456);
statement.setInt(4, 1);
statement.executeUpdate();
System.out.println("Record updated.");
} catch (SQLException e) {
e.printStackTrace();
}
5. Delete a record
To delete a record from the database, you will need to use an SQL DELETE
statement and execute it using the connection object.
try {
String sql = "DELETE FROM table_name WHERE id = ?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, 1);
statement.executeUpdate();
System.out.println("Record deleted.");
} catch (SQLException e) {
e.printStackTrace();
}
Prepared Statement
A PreparedStatement is a pre-compiled SQL statement. It is a subinterface
of Statement. Prepared Statement objects have some useful additional
features than Statement objects. Instead of hard coding queries,
PreparedStatement object provides a feature to execute a parameterized
query.
Advantages of PreparedStatement
myStmt.setInt(1,10);
myStmt.setString(2,"Java");
4. Execute the Query
ResultSet myRs= myStmt.executeQuery();
Statement
The statement interface is used to create SQL basic statements in Java it
provides methods to execute queries with the database. There are different
types of statements that are used in JDBC as follows:
• Create Statement
• Prepared Statement
• Callable Statement
1. Create a Statement: From the connection interface, you can create the
object for this interface. It is generally used for general–purpose access to
databases and is useful while using static SQL statements at runtime.
Syntax:
Statement statement = connection.createStatement();
Implementation: Once the Statement object is created, there are three ways to
execute it.
• boolean execute(String SQL): If the ResultSet object is retrieved,
then it returns true else false is returned. Is used to execute SQL
DDL statements or for dynamic SQL.
• int executeUpdate(String SQL): Returns number of rows that are
affected by the execution of the statement, used when you need a
number for INSERT, DELETE or UPDATE statements.
• ResultSet executeQuery(String SQL): Returns a ResultSet object.
Used similarly as SELECT is used in SQL.
// Class
class GFG {
ResultSet interface
The object of ResultSet maintains a cursor pointing to a row of a table. Initially, cursor
points to before the first row.
But we can make this object to move forward and backward direction by passing either
TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int)
method as well as we can make this object as updatable by:
1. Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
2. ResultSet.CONCUR_UPDATABLE);
2) public boolean previous(): is used to move the cursor to the one row previous
from the current position.
3) public boolean first(): is used to move the cursor to the first row in result
set object.
4) public boolean last(): is used to move the cursor to the last row in result
set object.
6) public boolean relative(int is used to move the cursor to the relative row
row): number in the ResultSet object, it may be positive
or negative.
ResultSetMetaData Interface
The metadata means data about data i.e., we can get further information from the data.
If you have to get metadata of a table like total number of columns, column name,
column type etc., ResultSetMetaData interface is useful because it provides methods to
get metadata from the ResultSet object.
Method Description
public String getColumnTypeName(int it returns the column type name for the
index)throws SQLException specified index.
public String getTableName(int index)throws it returns the table name for the specified
SQLException column index.