Docsity

JDBC and Oracle: A Comprehensive Tutorial, Slides of Java Programming

Partial preview of the text

Download JDBC and Oracle: A Comprehensive Tutorial and more Slides Java Programming in PDF only on Docsity!

JDBC/Oracle tutorial

Topics covered

  • Resources
  • Setup Oracle environment on NJIT
  • Install Oracle JDBC driver
  • Introduction to Oracle 9
  • Oracle SQL*Plus Basics
  • Introduction to JDBC
  • JDBC Basics
  • A sample JDBC program
  • Summary
  • References

Testing JDBC

  • Using your Prophet username and password,

login to one of the afs nn computers to enter,

compile and execute the Java program on the

next four slides.

Test Program (1)

*****************Test.java*******************

import java.sql.;*

class Test { public static void main (String args []) throws SQLException { // Load the Oracle JDBC driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

Test Program (3)

// Connect to the database Connection conn = DriverManager.getConnection (url,"username","password");

// Create a Statement Statement stmt = conn.createStatement ();

// Select the SYSDATE column from the dual table ResultSet rset = stmt.executeQuery ("select SYSDATE from dual");

Test Program (4)

// Print the result while (rset.next ()) System.out.println (rset.getString (1));

// Close the ResultSet rset.close();

// Close the Statement stmt.close();

// Close the connection conn.close(); } }

Errors

  • If the program reports that your connection to

oracle was unsuccessful or you receive java

exceptions, it may be a problem specific to

your AFS account.

Introduction to Oracle

  • Oracle is an Object Relational Database Management System(ORDBMS).
  • It offers capabilities of both relational and object- oriented database systems.

Introduction to SQL

  • SQL was invented and developed by IBM in early 1970’s. SQL stands for Structured Query Language.
  • Oracle’s database Language is SQL, which is used for storing and retrieving information in Oracle.
  • A table is a primary database object of SQL that is used to store data.

Introduction to SQL(Cont’d)

In order to communicate with the database, SQL supports the following categories of commands:-

  • Data Definition Language- create, alter,drop commands.
  • Data Manipulation Language- insert, select, delete and update commands.
  • Transaction Control Language- commit, savepoint and rollback commands.
  • Data Control Language- grant and revoke commands.

SQL*Plus

SQLPlus is an Oracle specific program which accepts SQL commands and PL/SQL blocks and executes them. SQLPlus enables manipulation of SQL commands and PL/SQL blocks. It performs many additional tasks as well.

Oracle Basics

  • You must have an existing database instance before you can create an oracle relation (table).
  • If you use NJIT Oracle account, you are already given a database instance when DBA opens the account for you.
  • Note that the following discussion is generic, and not specific to NJIT’s Prophet account.

Data Definition Language

  • Create tables

SQL> CREATE TABLE <table_name> ( <column1_name> <data_type> <[not]null>, <column2_name> <data_type> <[not]null>,

.... CONSTRAINT pk_name PRIMARY KEY (column_name); CONSTRAINT fk_name FOREIGN KEY (column_name)); REFERENCE name1(name2) ON DELETE CASCADE);

  • Alter the existing table SQL> ALTER TABLE <table_name> MODIFY/ADD (column definition);

Data Definition Language(Cont’d)

  • When there is no further use of records in a table and the structure has to be retained, then the records alone can be deleted. SQL>TRUNCATE table <table_name>;
  • Drop a table

SQL>DROP <table_name>;