Tuesday, 17 January 2017

CONNECTIVITY WITH ORACLE

How to send SQL statement to database ?

JDBC provide statement objects in order to send SQL statements to database. JDBC provides 3 type of statement objects -

  1. Statement
  2. PreparedStatement
  3. CallableStatement

Statement : It is an interface implemented by JDBC driver developer. Statement is used for sending static SQL statements.


What is static SQL statement ?

An SQL statement with values, which is compiled every-time before execution.


How to create Statement object ?

Connection provide the following method which return Statement object.
Statement createStatement() ---- creates a Statement object for sending SQL statements to the database.
Example - 


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Test4 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
System.out.println(con);
Statement st = con.createStatement();
System.out.println(st);
con.close();
}
}

Output -
oracle.jdbc.driver.T4CConnection@4ccabbaa
oracle.jdbc.driver.OracleStatementWrapper@6bf256fa


Methods of Statement

1. int executeUpdate(String sql) - Executes the given SQL statement, which may be an INSERT, UPDATE or DELETE statement or an SQL statement that returns nothing, such as SQL DDL statement.

2. ResultSet executeQuery(String sql) - Executes the given SQL statement, which returns a single ResultSet object.

3. boolean execute(String sql) - Executes the given SQL statement, which may return multiple results.


No comments:

Post a Comment