There are 5 steps to connect any java application
with the database in java using JDBC. They are as follows:
- Register the driver class
- Creating connection
- Creating statement
- Executing queries
- Closing connection
1) Register the driver class
The forName () method of
Class class is used to register the driver class. This
method is used to dynamically load the
driver class.
Syntax of forName () method
public static void forName(String className)throws ClassNotFoundException
Class.forName(“com.mysql.jdbc.Driver”);//For MySQL driver
The getConnection () method of DriverManager class
is used to establish connection with the database.
Syntax of getConnection () method
1) public static Connection getConnection(String url)throws SQLException
2) public static Connection getConnection(String url,String name,String password)
throws SQLException.
Connection
con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbname”,”username”,”password”); // For MySQL driver
The createStatement () method of Connection
interface is used to create statement. The object of statement is responsible
to execute queries with the database.
Syntax of createStatement ()
method
public Statement createStatement()throws SQLException
Statement stmt=con.createStatement ();
The executeQuery () method of
Statement interface is used to execute queries to the database. This method
returns the object of ResultSet that can be used to get all the records of a
table.
Syntax of executeQuery () method
public ResultSet executeQuery(String sql)throws SQLException
ResultSet rs=stmt.executeQuery("select
* from TableName");
while(rs.next()){
System.out.println(rs.getInt(1)+"
"+rs.getString(2));
By
closing connection object statment and ResultSet will be closed automatically.
The close () method of Connection interface is used to close the connection.
Syntax of close () method
public void close()throws SQLException
con.close();
DriverManager Class:-
The DriverManager class acts as an interface
between user and drivers. It keeps track of the drivers that are available and
handles establishing a connection between a database and the appropriate
driver. The DriverManager class
maintains a list of Driver classes that have registered themselves by calling
the method DriverManager.registerDriver ().
Connection interface:
|
A Connection is the session between java
application and database. The Connection interface is a factory of Statement,
PreparedStatement, and DatabaseMetaData i.e. 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() etc.
|
By default, connection commits the changes after executing queries.
|
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 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.
|
Statement interface
The important methods of Statement interface are as
follows:
|
1) public ResultSet
executeQuery(String sql): is used to execute SELECT
query. It returns the object of ResultSet.
|
|
2) public int executeUpdate
(String sql): is used to execute specified
query, it may be create, drop, insert, update, delete etc.
|
|
3) public boolean
execute(String sql): is used to execute queries
that may return multiple results.
|
|
4) public int[] executeBatch(): is
used to execute batch of commands.
|
ResultSet interface
The object of ResultSet maintains a cursor pointing
to a particular row of data. Initially, cursor points to before the first row.
By default, ResultSet object can be moved forward only and it is not
updatable.
|
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:
|
Statement stmt = con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
For Example:-
package com.javacoreconcept.jeetendra;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcSample {
public static void main(String...jdbc) throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernatesample","root","admin");
String query="Select * from UserDetails2";
Statement
ResultSet rs=stmt.executeQuery(query);
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();}
}
/**
* Moves the cursor a relative
number of rows, either positive or negative.
* Attempting to move beyond
the first/last row in the
* result set positions the
cursor before/after the
* the first/last row. Calling
<code>relative(0)</code> is valid, but does
* not change the cursor position.
PreparedStatement:
|
The PreparedStatement interface is a subinterface
of Statement. It is used to exeucte parameterized query.
Why use PreparedStatement?
|
|
|
The performance of the
application will be faster if you use PreparedStatement interface because
query is compiled only once.
|
|
|
The
prepareStatement() method of Connection interface is used to return the
object of PreparedStatement.Syntax:
|
public PreparedStatement prepareStatement(String query)throws SQLException{}




perfect explanation about java programming .its very useful.thanks for your valuable information.jbest java institute in chennai | java training in velachery
ReplyDelete