jdbc
jdbc
Data is the new fuel that runs the current IT industry, and the data processing
applications utilize this data to extract the necessary information. But, the real question
is, how do we establish a connection between the data storage unit and the data
processing application?
Now that we know the definition of JDBC, let us understand why and where we need it.
• For viewing and modify data records in the data storage units.
So, these were the places and purposes for which we needed a JDBC connection. We
will learn the data types supported in SQL and their compatibility with Java over JDBC.
Data Types in Java JDBC
The data types for Java and the data storage unit (here, the SQL) will differ. Hence we
need a translator. The JDBC driver takes the responsibility to communicate the data
types from Java to SQL to eliminate the ambiguity.
Most frequently used Datatypes are mentioned below:
java.lang.string VARCHAR
boolean BIT
java.math.BigDecimal NUMERIC
int INTEGER
float REAL
float FLOAT
double DOUBLE
Java JDBC Architecture
The Java JDBC architecture consists of the following primary segments. They are:
• JDBC Application
• JDBC API
• JDBC Manager
• JDBC Drivers
JDBC API
The JDBC API plays a significant role in the JDBC architecture. The JDBC API ensures
that a stable connection is established between the data storage unit and the JDBC
application.
JDBC Manager
The JDBC manager takes care of selecting the appropriate driver manager to ensure
that the driver software chosen supports the data storage unit's requirements to offer
an uninterrupted connection.
JDBC Drivers
The JDBC driver is the crucial unit in the JDBC architecture. JDBC driver is the first layer
of JDBC architecture that has direct contact with the data storage units.
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb",
"mydbuser", "mydbuser");
// mydb is database
// mydbuser is name of database
// mydbuser is password of database
Statement statement;
statement = connection.createStatement();
ResultSet resultSet;
resultSet = statement.executeQuery(
"select * from designation");
int code;
String title;
while (resultSet.next()) {
code = resultSet.getInt("code");
title = resultSet.getString("title").trim();
System.out.println("Code : " + code
+ " Title : " + title);
}
resultSet.close();
statement.close();
connection.close();
}
catch (Exception exception) {
System.out.println(exception);
}
} // function ends
} // class ends
Statement Interface
Statement Use this for general-purpose access to your database. Useful when you are
using static SQL statements at runtime. The Statement interface cannot accept
parameters.
Creating Statement Object Before you can use a Statement object to execute a SQL
statement, you need to create one using the Connection object's createStatement( )
method, as in the following example –
Once you've created a Statement object, you can then use it to execute an SQL
statement with one of its three execute methods.
1. import java.sql.*;
2. class FetchRecord{
3. public static void main(String args[])throws Exception{
4. Class.forName("oracle.jdbc.driver.OracleDriver");
5. Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:15
21:xe","system","oracle");
6. Statement stmt=con.createStatement();
7.
8. //stmt.executeUpdate("insert into emp765 values(33,'Irfan',50000)");
9. //int result=stmt.executeUpdate("update emp765 set name='Vimal',salary=100
00 where id=33");
10. int result=stmt.executeUpdate("delete from emp765 where id=33");
11. System.out.println(result+" records affected");
12. con.close();
13. }}
PreparedStatement Interface
All parameters in JDBC are represented by the ? symbol, which is known as the
parameter marker. You must supply values for every parameter before executing the
SQL statement.
The setType(position,value) methods bind values to the parameters, where XXX
represents the Java data type of the value you wish to bind to the input parameter. If
you forget to supply the values, you will receive an SQLException.
Each parameter marker is referred by its ordinal position. The first marker represents
position 1, the next position 2, and so forth. This method differs from that of Java array
indices, which starts at 0.
All of the Statement object's methods for interacting with the database (a) execute(), (b)
executeQuery(), and (c) executeUpdate() also work with the PreparedStatement object.
However, the methods are modified to use SQL statements that can input the
parameters
import java.sql.*;
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb",
"mydbuser", "mydbuser");
// Query which needs parameters
String query
= "Select * from students where age> ? and name = ?";
// Prepare Statement
PreparedStatement myStmt
= con.prepareStatement(query);
// Set Parameters
myStmt.setInt(1, 20);
myStmt.setString(2, 'Prateek');
System.out.println('Age Name');
ResultSet Interface
The SQL statements that read data from a database query, return the data in a result
set.
The SELECT statement is the standard way to select rows from a database and view
them in a result set. The java.sql.ResultSet interface represents the result set of a
database query.
A ResultSet object maintains a cursor that points to the current row in the result set.
The term "result set" refers to the row and column data contained in a ResultSet object.
A default ResultSet object is not updatable and has a cursor that moves forward only.
The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for
retrieving column values from the current row.
Values can be retrieved using either the index number of the column or the name of the
import java.sql.*;
column.
class ResultSetDemo{
public static void main(String args[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:15
21:xe","system","oracle");
// Prepare Statement
PreparedStatement myStmt = con.prepareStatement(query);
// Set Parameters
myStmt.setInt(1, 20);
System.out.println(“Age “);