Ipwt 3
Ipwt 3
There are 5 steps to connect any java application with the database using JDBC. These steps are as follows:
o Register the Driver class
o Create connection
o Create statement
o Execute queries
o Close 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
Note: Since JDBC 4.0, explicitly registering the driver is optional. We just need to put vender's Jar in the classpath, and
then JDBC driver manager can detect and load the driver automatically.
Example to register the OracleDriver class
Here, Java program is loading oracle driver to esteblish database connection.
Class.forName("oracle.jdbc.driver.OracleDriver");
2) Create the connection object
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
Example to establish connection with the Oracle database
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
3) Create the Statement object
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
Example to create the statement object
Statement stmt=con.createStatement();
4) Execute the query
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
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 the connection.
Syntax of close() method
public void close()throws SQLException
Example to close connection
con.close();
ALTER: Used to modify the structure of existing database objects. For example, ALTER TABLE, ALTER INDEX.
DROP: Used to delete existing database objects. For example, DROP TABLE, DROP INDEX.
TRUNCATE: Used to remove all records from a table, but keep the table structure intact.
RENAME: Used to rename an existing database object. For example, RENAME TABLE.
Example:
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT );
DML (Data Manipulation Language):
DML statements are used to manipulate the data stored in database objects such as tables.
Common DML statements include:
SELECT: Used to retrieve data from one or more tables. It is not strictly a DML statement but is commonly
categorized as such.
INSERT: Used to add new records to a table. For example, INSERT INTO.
UPDATE: Used to modify existing records in a table. For example, UPDATE.
DELETE: Used to remove records from a table. For example, DELETE FROM.
Example:
INSERT INTO Employees (EmpID, FirstName, LastName, Age)
VALUES (1, 'John', 'Doe', 30);
27)Differentiate between Statement, PreparedStatement, and CallableStatement.