JDBC Lab

On my web site (clipper.ship.edu/~cdgira) under the lab and project link I have place a link to the file JDBCHelper.java. You will need this file in order to do the lab today and the projects attached to this lab. Before compiling the file you need to make a few changes. You need to change the loginName and password information sent to the constructor in public static main to match your loginName and password for the Oracle database.

Your loginName is the same as your login for clipper and the password is also the same as your login for clipper.

Example if your login for clipper is nn5555, then you would have the following: JDBCHelper test = new JDBCHelper("jdbc:oracle:oci8:@CSDB","nn5555","nn5555");

Additionally you need to add the following to your .profile on clipper: CLASSPATH=$CLASSPATH:/opt/oracle/jdbc/lib/ojdbc14.jar:. export CLASSPATH LD_LIBRARY_PATH=/opt/oracle/lib export LD_LIBRARY_PATH ORACLE_HOME=/opt/oracle export ORACLE_HOME

You should now be ready to compile the file. NOTE: You must compile the program on clipper. javac JDBCHelper.java

In order to run the program you must make Java run in 64 bit mode: java –d64 JDBCHelper

Failure to do so will generate the following error: Exception in thread "main" java.lang.UnsatisfiedLinkError: /opt/oracle/lib/libocijdbc9.so: ld.so.1: java: fatal: /opt/oracle/lib/libocijdbc9.so: wrong ELF class: ELFCLASS64 at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1586) at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1511) at java.lang.Runtime.loadLibrary0(Runtime.java:788) at java.lang.System.loadLibrary(System.java:834) at oracle.jdbc.oci8.OCIDBAccess.logon(OCIDBAccess.java:262) at oracle.jdbc.driver.OracleConnection.(OracleConnection.java:346) at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:468) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:314) at java.sql.DriverManager.getConnection(DriverManager.java:512) at java.sql.DriverManager.getConnection(DriverManager.java:171) at JDBCHelper.createConnection(JDBCHelper.java:139) at JDBCHelper.main(JDBCHelper.java:360) SQL Format Cheet Sheet [] = optional

SELECT column1 [, column2,…] FROM tablename [WHERE condition]

DELET FROM tablename WHERE column operator value [AND|OR column operator value …]

CREATE TABLE tablename (column1 data_type [PRIMARY KEY] [, column2 data_type, …]);

INSERT INTO tablename (first_column,...last_column) VALUES (first_value,...last_value);

UPDATE tablename SET columnname = newvalue [,nextcolumn = newvalue2...] WHERE columnname operator value [AND|OR column operator value…]

DROP TABLE tablename

Examples: SELECT Last_Name_VC,First_Name_VC,Mi_VC FROM Users_T WHERE ID_INT = 5

DELETE FROM employee WHERE firstname = 'Mike' or firstname = 'Eric'

CREATE TABLE employee (first varchar2(15) PRIMARY KEY, last varchar2(20), age number(3), address varchar2(30), city varchar2(20), state varchar2(20))

INSERT INTO employee (first, last, age, address, city, state) VALUES ('Luke', 'Duke', 45, '2130 Boars Nest', 'Hazard Co', 'Georgia')

UPDATE employee SET age = age+1 WHERE first_name='Mary' AND last_name='Williams'

DROP TABLE employee Oracle Datatypes

Character Strings

 CHAR (size) – A fixed-sized field of characters. The largest this particular datatype can become is 2000 bytes. In other words, it can only hold 2000 characters. If you don’t specify the length of the CHAR datatype, the default size is a single character (i.e. 1 byte).

 NCHAR (size) – A fixed-sized field of characters, where the character set is determined by its definition. So, the maximum size is 2000 bytes per row or 2000 characters. This handles multibyte character sets.

 VARCHAR2 (size) – A variable-sized field of characters. The largest this datatype can become is 4000 characters.

 NVARCHAR2 (size) – A variable-sized field of characters, where the character set is determined by its definition. The maximum size is 4000 bytes per row or 4000 characters. This handles multibyte character sets.

Note: The VARCHAR2 datatype is the successor of VARCHAR. So it is recommended that you use VARCHAR2 as a variable-sized array of characters.

 LONG – A variable-sized field of characters. The maximum size of this field is 2GB.

Number

 NUMBER (precision, scale) – A variable-sized number, where the precision is between 1 and 38 and size is between -84 and 127. A NUMBER datatype with only one parameter is NUMBER (precision), where the parameter specifies the precision of the number. A NUMBER datatype with no parameters is set to its maximum size.

Date and Time

 DATE – A fixed-sized 7 bit field that is used to store dates. One thing to note is that the time is stored as part of the date. The default format DD-MON-YY can be overridden by NLS_DATE_FORMAT.

 TIMESTAMP (precision) – A variable-sized value ranging from 7 to 11 bytes, that is used to represent a date/time value. It includes both date and time. The precision parameter determines how many numbers are in the fractional part of SECOND field. The precision of the SECOND field within the TIMESTAMP value may have a value ranging from 0 to 9 with a default precision of 6.

 TIMESTAMP (precision) WITH TIME ZONE – A fixed-sized value of 13 bytes, which represents a date/time value along with a time zone setting. There are two ways one can set the time zone. The first is by using the UTC offset, say ‘+10:0’, or secondly by the region name, say ‘Australia/Sydney’.

 TIMESTAMP (precision) WITH LOCAL TIME – A variable value ranging from 7 to 11 bytes. This particular datatype is similar to the TIMESTAMP WITH TIME ZONE datatype. The difference is that the data is normalised to the database time zone when stored. The entry is manipulated to concur with the client’s time zone when retrieved.

Intervals

 INTERVAL DAY (day_precision) TO SECOND (second_precision) – A fixed-sized 11 byte value that represents a period of time. It includes days, hours, minutes and seconds.

 INTERVAL YEAR (year_precision) TO MONTH - A fixed-sized 5 byte value that represents a period of time. It includes years and months.

Binaries

 RAW (size) – A variable-sized field of raw binary data. The maximum size for this datatype is 2000 bytes.

 LONG RAW - A variable-sized field of raw binary data. The maximum size for this datatype is 2 GB.

 BLOB – The Binary Large Object is a field that holds unstructured binary data. The maximum size for this datatype is 4 GB.

 CLOB – The Character Large Object is a field that holds single byte character data. The maximum size for this datatype is 4 GB.

 NCLOB – The National Character Large Object is a field that holds either single byte of multibyte character data dependent on the national character set. The maximum size for this datatype is 4 GB.

 BFILE – An external binary file. The maximum size for this file is 4 GB. The size is also limited by the operating system.

Alternatives for ANSI Standard Datatypes

Instead of using ANSI standard datatypes, you can use Oracle defined datatypes. View the table below to see the Oracle datatype alternative for ANSI standard datatypes.

ANSI Standard Oracle Datatype

CHARACTER and CHAR CHAR

CHARACTER VARYING and CHAR VARYING VARCHAR2

NUMERIC, DECIMAL, DEC, INTEGER, INT NUMBER and SMALLINT

FLOAT, REAL, DOUBLE PRECISION FLOAT