Showing posts with label Statement. Show all posts
Showing posts with label Statement. Show all posts

Saturday, July 26, 2014

JDBC Connectivity in Java

JDBC is used to establish connection with database through java program. Java Database connectivity is four step procedures. Let us see each step in detail for oracle database connectivity.

1. Loading database driver: first step is to load the driver class called “OracleDriver” into main memory.
Class.forName( "oracle.jdbc.driver.OracleDriver" );
Here “oracle.jdbc.driver” is package name and “OracleDriver” is a class name which contains driver methods.

2.Establish a Database Connection: second step is to create connection object by mentioning driver name, server address, user name and password.
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl”,”Username”,"Password” );
Here “jdbc” is database, “oracle” is sub database, “thin” is driver type, “127.0.0.1” is host name, “1521” is port number used for connection, “orcl” is name of service used (SID), “Username” is database username and “Password” is password for corresponding user.

3.Execute SQL Statements: Once connection with database is created, we need to execute SQL queries using above connection object.
Statement stmt = conn.createStatement();
stmt.executeUpdate(“QUERY”);
Here we have created statement stmt using connection object and “QUERY” is SQL statement that has to be executed using executeUpdate().

4.Close Database Connection: Once all the SQL transactions are done with database, we need to close the connection with database.

conn.close();