Monday, 24 October 2016

Create Statement object

Create Statement object

  • To create Statement object, we need to call createStatement() method.
  • createStatement() method is presents in java.sql.Connection interface.
Syntax
             public Statement createStatement()throws SQLException
Example
             Statement statement = connection.createStatement();
-------------------------------------------------------------------------------------
Program         :       Creating Statement object
Program name  :      CreateStmt1.java
Output             :         
 Driver is Registered successfully
 got Connection from DB success
 Statement object created successfully
import java.sql.*;
class CreateStmt1
{
public static void main(String args[])throws SQLException
{
//step-1:Register the JDBC driver

Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
System.out.println(“Registered succesfully”);

//step-2: get the connection from DB server

Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","nireekshan");
System.out.println("got Connection from DB"+con);

//step-3 create the Statement object

Statement statement = con.createStatement();
System.out.println("Statement object created successfully");
}

}

Note:
To compile above java program we have to set the class path to ojdbc14.jar, so, keep this jar in corresponding directory then set the class path.
Set class path:
  • C:\Nireekshan\JDBC>set classpath=ojdbc14.jar;.;
Compile      :          javac CreateStmt1.java
Run            :          java CreateStmt1
Output        :         
Driver is Registered successfully
got Connection from DB success
Statement object created successfully
-------------------------------------------------------------------------------------
Thanks for your time.
Nireekshan