JDBC
JDBC
Topics Covered:-
• JDBC
• Why we should we use JDBC
• JDBC Drivers
• JDBC API
• Java Database Connectivity with MySQL
• Steps to Connect with Database
• Creating Connection with database (JDBC)
• Creating Table (CRUD)
• Inserting values into table (CRUD)
• Dynamic Values inserted
• Updating data using the dynamic values using BufferedReader
• Fetching details from database (CRUD)
• Data Access Object in JDBC - (DAO classes)
•
Uday Sharma
[email protected]
JDBC
• JDBC
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query
with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to
connect with the database.
There are four types of JDBC drivers:
• JDBC-ODBC Bridge Driver,
• Native Driver,
• Network Protocol Driver, and
• Thin Driver
• JDBC API:-
Advantage:-
o performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:-
o The Native driver needs to be installed on the each client machine.
o The Vendor client library needs to be installed on client machine.
3. Network Protocol driver:-
The Network Protocol driver uses middleware (application server) that converts JDBC calls
directly or indirectly into the vendor-specific database protocol. It is fully written in java.
Advantage:-
o No client side library is required because of application server that can perform many
tasks like auditing, load balancing, logging etc.
Disadvantages:-
o Network support is required on client machine.
o Requires database-specific coding to be done in the middle tier.
o Maintenance of Network Protocol driver becomes costly because it requires database-
specific coding to be done in the middle tier.
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.
• JDBC Connectivity Steps:-
There are 5 steps to connect any java application with the database using JDBC. These steps are
as follows:-
1. Import the package.
2. Load & register the driver.
3. Establish the connection .
4. Create the statement.
5. Execute the query.
6. Process result.
7. Close connection .
• Steps to Connect with Database,
explained in Detail
(Java Database Connectivity) JDBC:-
Steps to connect java program with database-
1) load the driver:-
Class.forName("com.mysql.jdbc.Driver")//(inside of try-catch )
Or
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
2)Create a Connection:-
Connection con=DriverManager.getConnection("url",”Username”,”Password”);
Connection
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dbname","root","root");
3)Create query, Statement , PreparedStatement , CallableStatement:-
String q="select * from students";
Statement stmt=con.createStatement(); // stored in the stmt
ResultSet set=stmt.executeQuery(q); // fire with the help of stmt
4)Process the data :-
while(set.next()) // its help to move into next row
{
int id=set.getInt("studentID"); // we can also pass the column name
String name=set.getString("studentName");
System.out.println(id);
System.out.println(name);
}
5) Close the connection:-
st.close();
con.close();
Remember Note:
“If you want to fetch the data from the database you have to use .executeQuery() method and if
want to update or perform the curd operation you have to use .executeUpdate() method.”
• MySQL-Connector Jar File:-
• For using the database you have to download the MySQL jar file for java EE version.
And built the Classpath of that jar file where you using the Java Project with MySQL.
• Following steps To build the path of jar File in JAVA EE.
• I name my java-project as JDBC:-
• Java Database Connectivity with MySQL:-
To connect Java application with the MySQL database, we need to follow 5 following steps.
In this example we are using MySql as the database. So we need to know following informations
for the mysql database:
1. Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
2. Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database,
localhost is the server name on which mysql is running, we may also use IP address,
3306 is the port number and sonoo is the database name. We may use any database, in
such case, we need to replace the sonoo with our database name.
3. Username: The default username for the mysql database is root.
4. Password: It is the password given by the user at the time of installing the mysql
database. In this example, we are going to use root as the password.
• Connection interface:-
• A Connection is a session between a Java application and a database. It helps to establish a
connection with the database.
• The Connection interface is a factory of Statement, PreparedStatement, and
DatabaseMetaData, i.e., an object of Connection can be used to get the object of Statement
and DatabaseMetaData. The Connection interface provide many methods for transaction
management like commit(), rollback(), setAutoCommit(), setTransactionIsolation(), etc.
By default, connection commits the changes after executing queries.
• Commonly used methods of Connection interface:-
1) public Statement createStatement(): creates a statement object that can be used to execute
SQL queries.
2) public Statement createStatement(int resultSetType,int resultSetConcurrency): Creates a
Statement object that will generate ResultSet objects with the given type and concurrency.
3) public void setAutoCommit(boolean status): is used to set the commit status. By default, it
is true.
4) public void commit(): saves the changes made since the previous commit/rollback is
permanent.
5) public void rollback(): Drops all changes made since the previous commit/rollback.
6) public void close(): closes the connection and Releases a JDBC resources immediately.
• Connection Interface Fields:-
There are some common Connection interface constant fields that are present in the Connect
interface. These fields specify the isolation level of a transaction.
• TRANSACTION_NONE: No transaction is supported, and it is indicated by this constant.
• TRANSACTION_READ_COMMITTED: It is a constant which shows that the dirty reads are not
allowed. However, phantom reads and non-repeatable reads can occur.
• TRANSACTION_READ_UNCOMMITTED: It is a constant which shows that dirty reads, non-
repeatable reads, and phantom reads can occur.
• TRANSACTION_REPEATABLE_READ: It is a constant which shows that the non-repeatable
reads and dirty reads are not allowed. However, phantom reads and can occur.
• TRANSACTION_SERIALIZABLE: It is a constant which shows that the non-repeatable reads,
dirty reads as well as the phantom reads are not allowed.
• Creating Connection with database (JDBC)
Console:-
Output:- on Console
Output:- On Console:-
Output:-
Before Update Query
Output:-
➢ Go check out my LinkedIn profile for more notes and other resources content
https://www.linkedin.com/in/uday-sharma-602b33267