The forName()
method of the Class class is used to register the driver class. This method is used to load the driver class dynamically. Consider the following example to register OracleDriver class.Class.forName("oracle.jdbc.driver.OracleDriver");
getConnection()
method of DriverManager class is used to establish the connection with the database. The syntax of the getConnection()
method is given below.public static Connection getConnection(String url)throws SQLException
public static Connection getConnection(String url,String name,String password)
throws SQLException ​
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");
createStatement()
method of Connection interface is used to create the Statement. The object of the Statement is responsible for executing queries with the database.public Statement createStatement()throws SQLException
Statement stmt=con.createStatement();
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.executeQuery()
method is given below :public ResultSet executeQuery(String sql)throws SQLException
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
} ​
executeUpdate()
method is used which returns the boolean value to indicate the successful completion of the operation.close()
method is given below :public void close()throws SQLException
con.close();