Download Overview of JDBC and Pro* C

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Entity–attribute–value model wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft Access wikipedia , lookup

Tandem Computers wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

Open Database Connectivity wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Overview of JDBC,Pro*C and Oracle
connectivity on Omega
CSE 5330 – Database Systems
Overview of JDBC and Pro*C
1
Options for doing the project
• Windows application using Java
– JDBC for database connectivity
– Java AWT / Swing etc. for the GUI
• Menu driven program using Pro*C
– If you know C, but not Java
Overview of JDBC and Pro*C
2
Java Database Connectivity (JDBC)
• JDBC provides a standardized library for accessing relational
databases
– Connecting to the database
– Initiating queries
– Creating stored (parameterized) queries
– Data structure for the query result
• JDBC is not embedded SQL
• JDBC is located in java.sql package
Overview of JDBC and Pro*C
3
JDBC driver
Java application
JDBC API
JDBC driver manager
JDBC driver API
Client
Oracle JDBC driver
(classes12.zip)
Server (omega)
Overview of JDBC and Pro*C
Oracle
database
4
JDBC driver (continued)
• JDBC driver for Oracle needs to be on the client machine
• Available on omega at
– /opt/oracle/product/9.2.0.4.0/jdbc/lib/classes12.zip
• The JDBC driver (classes12.zip) needs to be included in the runtime
classpath
Overview of JDBC and Pro*C
5
Six steps in using JDBC
1. Load the driver
2. Establish a connection
3. Create a statement object
4. Execute a query
5. Process the results
6. Close the connection
Overview of JDBC and Pro*C
6
JDBC usage steps - details
•
Step 1 : Load the driver
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
}
catch(Exception e)
{
System.out.println("JDBC ORACLE driver failed to load");
}
Overview of JDBC and Pro*C
7
JDBC usage steps - details
• Step 2 : Establish a connection
•
try
•
{
•
con =
DriverManager.getConnection("jdbc:oracle:thin:scott/[email protected]
ta.edu:1521:CSE1");
•
}
•
catch(Exception e)
•
{
•
•
System.out.println("Failed to connect to the database");
}
Overview of JDBC and Pro*C
8
JDBC usage steps - details
• Step 3 : Create a statement
– Statement statement = con.createStatement();
• Step 4 : Execute a query
String query = “SELECT FNAME, SALARY FROM EMPLOYEE”;
ResultSet rs = Statement.executeQuery(query);
• (For SELECT)
String query = “UPDATE EMPLOYEE SET SALARY = 50000 WHERE FNAME =
“John”;
Statement.executeUpdate(query);
• (For UPDATE, INSERT, DELETE)
Overview of JDBC and Pro*C
9
JDBC usage steps - details
• Step 5 : Process the result
While (rs.next())
{
System.out.println(rs.getString(1) + “ “ + rs.getInt(2));
}
• First column has index 1, not 0
• ResultSet provides various getXXX methods that take a column index,
and return the data
Overview of JDBC and Pro*C
10
JDBC usage steps - details
• Step 6 : Close the connection
– con.close();
• As opening a connection is expensive, postpone this step if additional
database operations are expected
Overview of JDBC and Pro*C
11
Online resources
• JDBC tutorial
– http://java.sun.com/docs/books/tutorial/jdbc/
• JDBC API reference
– http://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.html
Overview of JDBC and Pro*C
12
Pro*C
• Embedded SQL
– Allows you to embed SQL in C code
Pre-compiler
C compiler
Pro*C program
C code
Embedded SQL statements
SQL statements replaced
By library calls
Executable
SQLLIB
Oracle runtime library
Overview of JDBC and Pro*C
13
Pro*C syntax
• SQL statements can be placed anywhere between a C block
• All SQL statements need to start with EXEC SQL and be terminated by
a semicolon
{
int a;
/* ... */
EXEC SQL SELECT salary INTO :a
FROM Employee
WHERE SSN=876543210;
/* ... */
printf("The salary is %d\n", a);
/* ... */
}
• Be sure to include sqlca.h
Overview of JDBC and Pro*C
14
Connecting to the database
• Legal way
char *username = "SCOTT";
char *password = "TIGER";
…
EXEC SQL CONNECT :username IDENTIFIED BY :password;
• Illegal way
– EXEC SQL CONNECT SCOTT IDENTIFIED BY TIGER;
– EXEC SQL CONNECT 'SCOTT' IDENTIFIED BY 'TIGER';
Overview of JDBC and Pro*C
15
Dynamic sql
• Used to dynamically create entire SQL statements
– char *s = "INSERT INTO emp VALUES(1234, 'jon', 3)";
– EXEC SQL PREPARE q FROM :s;
– EXEC SQL EXECUTE q;
• Alternately,
– char *s = "INSERT INTO emp VALUES(1234, 'jon', 3)";
– EXEC SQL EXECUTE IMMEDIATE :s;
Overview of JDBC and Pro*C
16
Host variables
• Key to the communication between the host program and the database
• They should be declared in a Declare section
– EXEC SQL BEGIN DECLARE SECTION;
– /* Declare all host variables inside this section:
– */ char *uid = "scott/tiger";
–
...
– EXEC SQL END DECLARE SECTION;
Overview of JDBC and Pro*C
17
Transactions
• Sequence of SQL statements that are treated as a single unit of work
• Transaction ends when you issue
– EXEC SQL COMMIT;
Overview of JDBC and Pro*C
18
Cursors
• Used for fetching multiple rows of data
• First, declare it.
– EXEC SQL DECLARE emp_cursor CURSOR FOR
–
SELECT ename, empno, sal
–
FROM emp
–
WHERE deptno = :dept_number;
• Then, open it.
– EXEC SQL OPEN emp_cursor;
• Fetch the records.
– for (;;)
– {
–
EXEC SQL FETCH emp_cursor INTO :emp_name1, :salary1;
– }
Overview of JDBC and Pro*C
19
Cursors (continued)
• Finally, close it.
– EXEC SQL CLOSE emp_cursor;
• Error handling
– Every executable SQL statement returns a status code to the SQLCA variable
sqlcode
– A zero status code means that Oracle executed the statement without detecting an
error or exception
– A positive status code means that Oracle executed the statement but detected an
exception
– A negative status code means that Oracle did not execute the SQL statement
because of an error.
Overview of JDBC and Pro*C
20
Online reference
•Pro*C/C++ Precompiler Programmer's Guide
•http://omega.uta.edu/oracle/appdev.920/a97269/pc_01int.htm
Overview of JDBC and Pro*C
21
Oracle connectivity on Omega
• To connect with Oracle the SSH tool may be used
• It is available for download from
http://www.uta.edu/oit/downloads.html
Once downloaded , omega can be accessed as follows:-
Overview of JDBC and Pro*C
22
Oracle connectivity on Omega
Once you submit this box, you will be asked to enter your password as
follows :-
Overview of JDBC and Pro*C
23
Oracle connectivity on Omega
• Incase you reside at an off-campus location, you will not be able to
directly access http://omega.uta.edu using SSH
• You will need to download and connect using a VPN client and then
connect through SSH to omega.
• The VPN client can be downloaded from
http://www.uta.edu/oit/clientservices/network/vpn/pc/vpn_pc.html
Overview of JDBC and Pro*C
24