To establish connection with the database getConnection() method of
DriverManager class is used.
Syntax :
1) public static Connection getConnection(String url)throws SQLException
. 2) public static Connection getConnection(String url,String name,
String password) throws SQLException
Example to establish connection with the Oracle
database
Connection con=DriverManager.getConnection
( "jdbc:oracle:thin:@localhost:1521:xe","system","password");
3) Create Statement object
To create statement object The createStatement() method of
Connection interface is used. The object of statement is mainly used to
execute queries with the database. Advanced java
training in bangalore
|
Syntax : public Statement createStatement()throws SQLException
Example to
create the statement object
. Statement stmt=con.createStatement();
4) Execute the query
To execute queries to the database executeQuery() method of
Statement interface is used. 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
Example to execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
5) Close the connection object
By closing connection object statement and ResultSet will be
closed automatically. The close() method of Connection interface is used
to close java
course in Bangalore the connection.
|
Syntax :
public void close()throws SQLException
Example to close connection
con.close();
Connection
with Oracle database :
To connect java application with the oracle database,
we need to follow 5 steps to perform database connectivity. Before that
some information is needed :
1. Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
2. Connection URL: The connection URL for the oracle database is jdbc:oracle:thin:@localhost:1521:xe where
jdbc is the API, oracle is the database, thin is the driver, localhost is
the server name on which oracle is running, , 1521 is the port number and
XE is the Oracle service name. Username: The
default username for the oracle database is system. Java course
in Bangalore
3. Password: Password is given by the user at the time of installing the oracle
database.
Steps :
step1 : load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
step2: create the connection object
Connection con=DriverManager.getConnection
( "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
step3: create the statement object
Statement stmt=con.createStatement();
step4: execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
step5: close the connection object
con.close();
JDBC Driver is a software component that enables java application
to interact with the database. There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
|
1) JDBC-ODBC
bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the
database. The JDBC-ODBC bridge driver converts JDBC method calls into the
ODBC function calls. This is now discouraged because of thin driver.
|
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.
|
3) Network Protocol driver
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.
|
with the real time
project .
Learn OOPs concept ,
packages , multithreading , JDBC connectivity , Annotations , hibernate
framework in Core &
Advanced java class .
|
No comments:
Post a Comment