Archive for the ‘clob’ Category
Oracle and Java Tutorial
I’m posting this because of a question raised against this older post on how to configure the %CLASSPATH%
to find the ojdbc6.jar
file. This is the lab file I use in my Database 1 class to expose students to the moving parts of writing Java programs against the Oracle database. That’s why I choose to use a CLOB
data type, which requires Oracle’s DBMS_LOB
package and wrapping stored procedures.
If you want the same content for MySQL, here’s the link. The full program in either blog entry is available by clicking on the fold/unfold Java Source Code Program widget at the bottom of the respective posts.
This demonstrates how to create an Java infrastructure for reading and writing large text files to an Oracle database. The example provides:
- A
FileIO.jar
library that lets you enter Oracle connection parameters through aJOptionPane
, and a customizedJFileChooser
to filter and read source files from the file system. - A
ojdbc6.jar
file, which is Oracle’s library for JDBC communication with the Oracle Databases.
The steps to compiling and testing this code are qualified below:
- Download and install the Java Software Development Kit (JSDK) for Java 6.
- Create a
C:\JavaTest
folder on Windows, or a/JavaTest
directory from some mount point of your choice. - Download and position the
ojdbc6.jar
andFileIO.jar
files in theJavaTest
directory. - Create a batch file to source your environment path (%PATH% on Windows and $PATH on Linux or Mac OS X) and the two Java Archive (JAR) files. A sample batch file is noted below:
set PATH=C:\Program Files\Java\jdk1.6.0_07\bin;%PATH% set CLASSPATH=C:\JavaDev\Java6\ojdbc6.jar;C:\JavaDev\Java6\FileIO.jar;. |
You can run this file by simply typing the files first name. On Linux or Mac OS X, you first need to grant it privileges with the chmod
command as 755
.
- Copy the
WriteReadCLOB.java
code from the bottom of this posting and also put it into theJavaTest
directory. - Compile the
WriteReadCLOB.java
source code with thejavac
utility, as shown below:
javac WriteReadCLOB.java |
After you compile it, you should run it as follows:
java WriteReadCLOB |
- Before running the code, you’ll need to seed (
INSERT
) a row that meets the desired hard coded criteria. It requires anITEM_TITLE
value of'The Lord of the Rings - Fellowship of the Ring'
and anITEM_SUBTITLE
of'Widescreen Edition'
in theITEM
table. - When it runs, you’ll see the following tabbed
JOptionPane
.
You need to enter the following values before clicking the OK button:
- Host: The
hostname
of your machine. - Port: The
port
that the Oracle Listener is running on (the default value is1521
). - Database: The Oracle TNS Alias, which is
orcl
for the full database sample database orxe
for the Oracle Database 10g Express Edition. - UserID: The
user
(schema) name where you’ve created anITEM
table. - Password: The
password
for the user’s account.
In the JFileChooser
, select a file to upload to the database.
You should see what you uploaded displayed in a JFrame
.
Java Source Code Program ↓
The drop down unfolds the WriteReadCLOB.java
source code.
The following program has dependencies on the FileIO.jar file. You need to download it and put it in your $CLASSPATH
for Linux or Mac OS X or %CLASSPATH%
for Windows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | // -------------------------------------------------------------------- // WriteReadCLOB.java // by Michael McLaughlin // // This code demonstrates reading a large text file and displaying // the text stream in a JLabel in a JFrame. // // The UPDATE and SELECT statements have dependencies on the // create_store.sql script. // -------------------------------------------------------------------- // Java Application class imports. import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.io.Reader; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; // Generic JDBC imports. import java.sql.CallableStatement; import java.sql.Clob; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; // Oracle JDBC import. import oracle.jdbc.driver.OracleDriver; import oracle.jdbc.pool.OracleDataSource; // Include book libraries (available at publisher website). import plsql.jdbc.DataConnectionPane; import plsql.fileio.FileIO; // -------------------------------------------------------------------/ public class WriteReadCLOB extends JFrame { // Define database connections. private String host; private String port; private String dbname; private String userid; private String passwd; // Define data connection pane. private DataConnectionPane message = new DataConnectionPane(); // Construct the class. public WriteReadCLOB (String s) { super(s); // Get database connection values or exit. if (JOptionPane.showConfirmDialog(this,message ,"Set Oracle Connection String Values" ,JOptionPane.OK_CANCEL_OPTION) == 0) { // Set class connection variables. host = message.getHost(); port = message.getPort(); dbname = message.getDatabase(); userid = message.getUserID(); passwd = message.getPassword(); // Print connection to console (debugging tool). message.getConnection(); // Create a JPanel for data display. ManageCLOB panel = new ManageCLOB(); // Configure the JPanel. panel.setOpaque(true); setContentPane(panel); // Configure the JFrame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocation(100,100); pack(); setVisible(true); } else System.exit(1); } // -------------------------------------------------------------------/ private class ManageCLOB extends JPanel { // Define display variables. private String clobText; private JScrollPane scrollPane; private JTextArea textArea; // -----------------------------------------------------------------/ public ManageCLOB () { // Set layout manager. super(new GridLayout(1,0)); // Assign file read to String. clobText = FileIO.openFile(FileIO.findFile(this)); // Insert record before querying it. if (clobText.length() > 0) { if (insertClob(host,port,dbname,userid,passwd,clobText)) clobText = getQuery(host,port,dbname,userid,passwd); else clobText = null; } else System.exit(2); // Construct text area and format it. textArea = new JTextArea(clobText); textArea.setEditable(false); textArea.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,14)); textArea.setLineWrap(true); textArea.setRows(10); textArea.setSize(400,100); textArea.setWrapStyleWord(true); // Put the image in container, and add label to panel. scrollPane = new JScrollPane(textArea); add(scrollPane); } // ---------------------------------------------------------------/ private Boolean insertClob(String host,String port,String dbname ,String user,String pswd,String fileString) { try { // Set the Pooled Connection Source OracleDataSource ods = new OracleDataSource(); String url = "jdbc:oracle:thin:@//"+host+":"+port+"/"+dbname; ods.setURL(url); ods.setUser(userid); ods.setPassword(passwd); // Define connection. Connection conn = ods.getConnection(); // Create statement. CallableStatement stmt = conn.prepareCall("UPDATE item "+ "SET item_desc = ? "+ "WHERE item_title = "+ "'The Lord of the Rings - Fellowship of the Ring'"+ "AND item_subtitle = 'Widescreen Edition'"); // Set string into statement. stmt.setString(1,fileString); // Execute query. if (stmt.execute()) conn.commit(); // Close resources. stmt.close(); conn.close(); // Return CLOB as a String data type. return true; } // End of connection try-block. catch (SQLException e) { if (e.getSQLState() == null) { System.out.println( new SQLException("Oracle Thin Client Net8 Connection Error.", "ORA-" + e.getErrorCode() + ": Incorrect Net8 thin client arguments:\n\n" + " host name [" + host + "]\n" + " port number [" + port + "]\n" + " database name [" + dbname + "]\n" , e.getErrorCode()).getSQLState()); // Return an empty String on error. return false; } else { System.out.println(e.getMessage()); // Return an empty String on error. return false; }}} // -----------------------------------------------------------------/ private String getQuery(String host,String port,String dbname ,String user,String pswd) { // Define method variables. char[] buffer; int count = 0; int length = 0; String data = null; String[] type; StringBuffer sb; try { // Set the Pooled Connection Source OracleDataSource ods = new OracleDataSource(); String url = "jdbc:oracle:thin:@//"+host+":"+port+"/"+dbname; ods.setURL(url); ods.setUser(userid); ods.setPassword(passwd); // Define connection. Connection conn = ods.getConnection(); // Define metadata object. DatabaseMetaData dmd = conn.getMetaData(); // Create statement. Statement stmt = conn.createStatement(); // Execute query. ResultSet rset = stmt.executeQuery( "SELECT item_desc " + "FROM item " + "WHERE item_title = " + "'The Lord of the Rings - Fellowship of the Ring'"+ "AND item_subtitle = 'Widescreen Edition'"); // Get the query metadata, size array and assign column values. ResultSetMetaData rsmd = rset.getMetaData(); type = new String[rsmd.getColumnCount()]; for (int col = 0;col < rsmd.getColumnCount();col++) type[col] = rsmd.getColumnTypeName(col + 1); // Read rows and only CLOB data type columns. while (rset.next()) { for (int col = 0;col < rsmd.getColumnCount();col++) { if (type[col] == "CLOB") { // Assign result set to CLOB variable. Clob clob = rset.getClob(col + 1); // Check that it is not null and read the character stream. if (clob != null) { Reader is = clob.getCharacterStream(); // Initialize local variables. sb = new StringBuffer(); length = (int) clob.length(); // Check CLOB is not empty. if (length > 0) { // Initialize control structures to read stream. buffer = new char[length]; count = 0; // Read stream and append to StringBuffer. try { while ((count = is.read(buffer)) != -1) sb.append(buffer); // Assign StringBuffer to String. data = new String(sb); } catch (Exception e) {} } else data = (String) null; } else data = (String) null; } else { data = (String) rset.getObject(col + 1); }}} // Close resources. rset.close(); stmt.close(); conn.close(); // Return CLOB as a String data type. return data; } catch (SQLException e) { if (e.getSQLState() == null) { System.out.println( new SQLException("Oracle Thin Client Net8 Connection Error.", "ORA-" + e.getErrorCode() + ": Incorrect Net8 thin client arguments:\n\n" + " host name [" + host + "]\n" + " port number [" + port + "]\n" + " database name [" + dbname + "]\n" , e.getErrorCode()).getSQLState()); // Return an empty String on error. return data; } else { System.out.println(e.getMessage()); return data; }} finally { if (data == null) System.exit(1); }}} // -----------------------------------------------------------------/ public static void main(String[] args) { // Define window. WriteReadCLOB frame = new WriteReadCLOB("Write & Read CLOB Text"); }} |
A couple DBMS_SQL limits
While developing a dynamic SQL example in Oracle 11g that builds a query based on available display columns, I found two interesting error messages. Now instead of noting it for the umpteenth time, I’m documenting it for everybody. The error messages are generated when this DBMS_SQL
package’s statement is a SELECT
statement, and is executed with either a BLOB
, BFILE
or CFILE
column in the list of returned columns.
26 | STATUS := dbms_sql.execute(stmt); |
BLOB
data type
You get the following error when a column in the query has a BLOB
data type. If you alter the query to exclude the column, no error occurs.
BEGIN test('DEMO'); END; * ERROR at line 1: ORA-00932: inconsistent datatypes: expected NUMBER got BLOB ORA-06512: at "SYS.DBMS_SQL", line 1575 ORA-06512: at "STUDENT.TEST", line 26 ORA-06512: at line 1 |
BFILE
or CFILE
data type
You get the following error when a column in the query has a BFILE
or CFILE
data type. If you alter the query to exclude the column, no error occurs.
BEGIN test('DEMO'); END; * ERROR at line 1: ORA-00932: inconsistent datatypes: expected NUMBER got FILE ORA-06512: at "SYS.DBMS_SQL", line 1575 ORA-06512: at "STUDENT.TEST", line 26 ORA-06512: at line 1 |
It’s never a joy to debug the DBMS_SQL
package, at least it’s never a joy for me. I hope this helps somebody sort out an issue more quickly.
Oracle LOB Storage Syntax
Somebody asked me to post some examples of Large Object syntax. I checked Chapter 9 of the Oracle Database SecureFiles and Large Object Developer’s Guide, and I agree an example or two would be helpful. I’ve provided CREATE
and ALTER
statements.
Here’s a sample CREATE TABLE
statement for a CLOB
, BLOB
, and BFILE
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | CREATE TABLE item ( item_id NUMBER CONSTRAINT pk_item PRIMARY KEY , item_barcode VARCHAR2(20) CONSTRAINT nn_item_1 NOT NULL , item_type NUMBER CONSTRAINT nn_item_2 NOT NULL , item_title VARCHAR2(60) CONSTRAINT nn_item_3 NOT NULL , item_subtitle VARCHAR2(60) , item_desc CLOB CONSTRAINT nn_item_4 NOT NULL , item_blob BLOB , item_photo BFILE , item_rating VARCHAR2(8) CONSTRAINT nn_item_5 NOT NULL , item_rating_agency VARCHAR2(4) CONSTRAINT nn_item_6 NOT NULL , item_release_date DATE CONSTRAINT nn_item_7 NOT NULL , created_by NUMBER CONSTRAINT nn_item_8 NOT NULL , creation_date DATE CONSTRAINT nn_item_9 NOT NULL , last_updated_by NUMBER CONSTRAINT nn_item_10 NOT NULL , last_update_date DATE CONSTRAINT nn_item_11 NOT NULL , CONSTRAINT fk_item_1 FOREIGN KEY(item_type) REFERENCES common_lookup(common_lookup_id) , CONSTRAINT fk_item_2 FOREIGN KEY(created_by) REFERENCES system_user(system_user_id) , CONSTRAINT fk_item_3 FOREIGN KEY(last_updated_by) REFERENCES system_user(system_user_id)) LOB (item_desc) STORE AS BASICFILE item_desc (TABLESPACE users ENABLE STORAGE IN ROW CHUNK 32768 PCTVERSION 10 NOCACHE LOGGING STORAGE (INITIAL 1048576 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645)) , LOB (item_blob) STORE AS item_blob (TABLESPACE users ENABLE STORAGE IN ROW CHUNK 32768 PCTVERSION 10 NOCACHE LOGGING STORAGE (INITIAL 1048576 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645)); |
If you attempt to make the CHUNK
size greater than 32,768, it will raise an ORA-22851: invalid CHUNK LOB storage option value
error.
Here’s a sample ALTER TABLE
statement that adds a new large object column to a table. You can’t modify an existing large object column. You raise an ORA-22859: invalid modification of columns
error when you try it. If you need to make modifications, check this post form last year on re-organizing LOB indexes.
1 2 3 4 5 6 7 8 | ALTER TABLE item ADD (another CLOB) LOB (another) STORE AS BASICFILE item_clob (TABLESPACE users ENABLE STORAGE IN ROW CHUNK 32768 PCTVERSION 10 NOCACHE LOGGING STORAGE (INITIAL 1048576 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645)); |
This all makes sense, but hold on there’s a potential problem when you add a large object column to a table. I’m not sure it’s a bug but I’ll probably open a TAR on it later in the week (arghhhh! I once worked in Oracle Support. :-)). Anyway, here’s what I found:
If you drop and recreate the table, you can encounter an ORA-01430
error. It appears that the large object is really hanging out in the catalog. As soon as you try to re-add it, you get the error.
If you describe the table after recreating it, you’ll see the following:
SQL> DESCRIBE item Name Null? Type ----------------------------------------- -------- --------------- ITEM_ID NOT NULL NUMBER ITEM_BARCODE NOT NULL VARCHAR2(20) ITEM_TYPE NOT NULL NUMBER ITEM_TITLE NOT NULL VARCHAR2(60) ITEM_SUBTITLE VARCHAR2(60) ITEM_DESC NOT NULL CLOB ITEM_BLOB BLOB ITEM_PHOTO BINARY FILE LOB ITEM_RATING NOT NULL VARCHAR2(8) ITEM_RATING_AGENCY NOT NULL VARCHAR2(4) ITEM_RELEASE_DATE NOT NULL DATE CREATED_BY NOT NULL NUMBER CREATION_DATE NOT NULL DATE LAST_UPDATED_BY NOT NULL NUMBER LAST_UPDATE_DATE NOT NULL DATE |
Also, if you run the following query with the DBMS_METADATA
package, the column doesn’t exist in the table defintion:
SELECT dbms_metadata.get_ddl('TABLE','ITEM') FROM dual; |
If try to run the ALTER
statement to add the column that doesn’t appear to exist, you’ll get the following message in Oracle 11.1.0.7.0:
ALTER TABLE item ADD (another CLOB) * ERROR at line 1: ORA-01430: COLUMN being added already EXISTS IN TABLE |
If you rerun the DBMS_METADATA.GET_DDL
query, it’ll be there in the table definition. Also, in its non-existence but existent state (ghost-state), there’s nothing in the DBA_LOB
, or DBA_SEGMENT
views. You can find a query to check large object segments and indexes here. My questions is where has it gone, why is it there, and did I do something wrong because this looks like basic functionality.
If you know the mystery or the existence of a bug, please post a comment.
Matching LOB Indexes and Segments
I enjoyed Tom Kyte’s example of how you find matching a LOB Index to Segment (Expert Oracle Database Architecture, pp. 542). I’ve noticed variations of it posted in various locations. While it works well for sample scheme that have only one LOB, the following works for any number of LOBs in any schema. This simplifies working with system- and user-defined segment names. The first CASE
statement ensures that joins between user-named segment names are possible. The second CASE
statement ensures two things: (a) Joins between system generated segment names don’t throw an error when matching unrelated system generated return values found in the DBA_SEGEMENTS
view; and (b) Joins between named segments are possible and don’t throw an error.
COL owner FORMAT A5 HEADING "Owner" COL TABLE_NAME FORMAT A5 HEADING "Table|Name" COL column_name FORMAT A10 HEADING "Column|Name" COL segment_name FORMAT A26 HEADING "Segment Name" COL segment_type FORMAT A10 HEADING "Segment|Type" COL bytes HEADING "Segment|Bytes" SELECT l.owner , l.table_name , l.column_name , s.segment_name , s.segment_type , s.bytes FROM dba_lobs l , dba_segments s WHERE REGEXP_SUBSTR(l.segment_name,'([[:alnum:]]|[[:punct:]])+' , CASE WHEN REGEXP_INSTR(s.segment_name,'[[:digit:]]',1) > 0 THEN REGEXP_INSTR(s.segment_name,'[[:digit:]]',1) ELSE 1 END) = REGEXP_SUBSTR(s.segment_name,'([[:alnum:]]|[[:punct:]])+' , CASE WHEN REGEXP_INSTR(s.segment_name,'[[:digit:]]',1) > 0 THEN REGEXP_INSTR(s.segment_name,'[[:digit:]]',1) ELSE 1 END) AND l.table_name = UPPER('&table_name') AND l.owner = UPPER('&owner') ORDER BY l.column_name, s.segment_name; |
It produces the following type of output:
Table Column Segment Segment Name Name Segment Name Type Bytes ------ ---------- ------------------------- ---------- --------- PLSQL ITEM_BLOB SYS_IL0000074435C00007$$ LOBINDEX 65,536 PLSQL ITEM_BLOB SYS_LOB0000074435C00007$$ LOBSEGMENT 2,097,152 PLSQL ITEM_DESC SYS_IL0000074435C00006$$ LOBINDEX 65,536 PLSQL ITEM_DESC SYS_LOB0000074435C00006$$ LOBSEGMENT 720,896 |
This should help you monitor growth of LOBs in your database. The maximum CHUNK
size is 32.768. If you elect a value greater than that you raise an ORA-22851
error.