The following 5 steps are the basic steps
involve in connecting a Java application with Database using JDBC:-
1.
Loading and registering the drivers.
2.
Creating a connection.
3.
Creating and executing the statements.
5.
Termination of database connection.
Loading and Registering
the Driver:-
Class.forName(); is used to load
the driver class.
Example to Register
with MySql Driver:
Class.forName("com.mysql.jdbc.driver");
As we are working with MySql, so we will use
MySql driver but we can have connections with more drivers:-
Name of the
Drivers:
•
MySql : com.mysql.jdbc.driver
•
Oracle : oracle.jdbc.driver.oracledriver
•
DB2 : com.ibm.db2.jdbc.net.db2driver
Creating
a Connection:-
getConnection() static method of DriverManager class is used to create a
connection and if I say programmatically getConnection() method returns
Connection interface type object.
Syntax:
getConnection(String url)
getConnection(String url, String
username, String password)
getConnection(String url, Properties
info)
Here url
parameter specify database connectivity mechanism or database server
identifier.
username
and password parameters
are the username and password of database connection.
Example
Establish Connection with MySql Driver:-
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/database_name","username","password");
Creating
and Executing SQL Statement:-
For
Creating SQL Statement:
createStatement()
static method of Connection interface is used to
create a SQL statement which return Statement class type reference object.
Example
to create a SQL Statement:
Statement s=con.createStatement();
For
Executing SQL Statement:
We have three types of statements:
1.
execute() - It returns a Boolean value if a result set object can be retrieved
. This method is generally used for DDL (Data Definition Language) statements.
Basically, if we want a Boolean value to be returned from executing a statement
(query) then we will use this method.
2. executeUpdate()
- This method will return the number of rows affected by the execution of
the SQL statements. Such as: insert, update or delete statements. Parameter for
this method will be the query you will write for insert, update or delete for
table.
3. executeQuery()
- It returns a ResultSet object and generally used with the select SQL statements
.
Now, this execution step is performed by
our next step.
Execution
of ResultSet:
Example
to Execute a SQL Statement:
ResultSet rs=s.executeQuery("SELECT
* FROM employee");
while(rs.next())
{
System.out.println(rs.getString(“user_name”)+" "+rs.getString(“user_designation”));
}
Closing
the Connection:
After executing SQL statement you need to
close the connection and release the session. The close(); method of Connection
interface is used to close the connection.
Example
of Closing a Connection:-
con.close();
NOTE:
We can also close our statement before closing of
the connection such as:-
s.close();
Please Share with your friends to make them understand
and feel us confident to write more for you.
Thank you.
0 comments
If you have any questions or suggestions, you are free to ask, i will appreciate that and, i will try my best...