Skip to content

Commit f72c9b0

Browse files
authored
Add files via upload
1 parent 4d3c5e2 commit f72c9b0

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed

java/ojvm/TrimLob-Readme.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
TrimLob Readme
2+
==============
3+
4+
The goal of this code sample is to contrast the performance of the same java code
5+
running as a stand-alone JDBC code running outside the database, and used as Java stored
6+
procedure running directly in your database session, using OJVM.
7+
8+
1/ Create an empty table with a Varchar2, BLOB, and CLOB columns, using the
9+
TrimLob.sql script (from a SQL*Plus session)
10+
11+
2/ Edit and compile the Java class and execute using it as stand-alone JDBC code:
12+
javac TrimLob.java
13+
java classpath %CLASSPATH% TrimLob
14+
15+
16+
3/ Execute TrimLobSP.sql (from a SQL*Plus session), this creates a Java source directly
17+
in the database, compiles, publishes/exposes to SQL, executes it in the database, and displays the results.

java/ojvm/TrimLob.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
/------------------------------------------------------------------------------
3+
/ Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
4+
/
5+
/ Portions Copyright 2006-2015, Kuassi Mensah. All rights reserved.
6+
/
7+
/
8+
/------------------------------------------------------------------------------
9+
/ DESCRIPTION
10+
/ This sample shows basic BLOB/CLOB operations. It drops, creates, and populates a basic_lob_table
11+
/ with blob, clob data types columns then fetches the rows and trim both LOB and CLOB
12+
*/
13+
14+
// Need to import the java.sql package to use JDBC
15+
import java.sql.*;
16+
17+
/*
18+
* Need to import the oracle.sql package to use
19+
* oracle.sql.BLOB
20+
*/
21+
import oracle.sql.*;
22+
23+
public class TrimLob
24+
{
25+
public static void main (String args []) throws SQLException {
26+
Connection conn;
27+
/*
28+
* Where is your code running: in the database or outside?
29+
*/
30+
if (System.getProperty("oracle.jserver.version") != null)
31+
{
32+
/*
33+
* You are in the database, already connected, use the default
34+
* connection
35+
*/
36+
conn = DriverManager.getConnection("jdbc:default:connection:");
37+
}
38+
else
39+
{
40+
/*
41+
* You are not in the database, you need to connect to
42+
* the database
43+
*/
44+
45+
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
46+
conn =
47+
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522/orcl", "scott",
48+
"tiger");
49+
}
50+
long t0,t1;
51+
/*
52+
* auto commit is off (not suported)by default in OracleJVM
53+
* It's faster with JDBC when auto commit is off
54+
*/
55+
conn.setAutoCommit (false);
56+
t0=System.currentTimeMillis();
57+
// Create a Statement
58+
Statement stmt = conn.createStatement ();
59+
// Make sure the table is empty
60+
stmt.execute("delete from basic_lob_table");
61+
stmt.execute("commit");
62+
63+
// Populate the table
64+
stmt.execute ("insert into basic_lob_table values ('first', " +
65+
"'010101010101010101010101010101', " +
66+
"'one.two.three.four.five.six.seven')");
67+
stmt.execute ("insert into basic_lob_table values ('second', " +
68+
"'0202020202020202020202020202020202020202', " +
69+
"'two.three.four.five.six.seven.eight.nine.ten')");
70+
71+
/*
72+
* Retive Lobs and modify contents; this can be done by doing
73+
* "select ... for update", but "autocommit" is turned off and
74+
* the previous "create table" statement already locks the table
75+
*/
76+
ResultSet rset = stmt.executeQuery
77+
("select * from basic_lob_table for update");
78+
while (rset.next ())
79+
{
80+
// Get the lobs
81+
BLOB blob = (BLOB) rset.getObject (2);
82+
CLOB clob = (CLOB) rset.getObject (3);
83+
84+
// Show the original lob length
85+
System.out.println ("Show the original lob length");
86+
System.out.println ("blob.length()="+blob.length());
87+
System.out.println ("clob.length()="+clob.length());
88+
89+
// Trim the lobs
90+
System.out.println ("Trim the lob to legnth = 6");
91+
blob.truncate (6);
92+
clob.truncate (6);
93+
94+
// Show the lob length after trim()
95+
System.out.println ("Show the lob length after trim()");
96+
System.out.println ("blob.length()="+blob.length());
97+
System.out.println ("clob.length()="+clob.length());
98+
}
99+
100+
// Close the ResultSet and Commit changes
101+
rset.close ();
102+
stmt.execute("commit");
103+
104+
// Close the Statement
105+
stmt.close ();
106+
107+
t1=System.currentTimeMillis();
108+
System.out.println ("====> Duration: "+(int)(t1-t0)+ "Milliseconds");
109+
// Close the connection
110+
conn.close ();
111+
}
112+
}

java/ojvm/TrimLob.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
REM replace scott/tiger with you database username/password
2+
REM
3+
connect scott/tiger;
4+
drop table basic_lob_table;
5+
create table basic_lob_table (x varchar2 (30), b blob, c clob);

java/ojvm/TrimLobSp.sql

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
create or replace java source named TrimLob as
2+
/*
3+
* This SQL script generated a Java code performing BLOB/CLOB operations.
4+
* The goal is to show Java in the database and contrat its performance with the same code
5+
* running as a client-side or stand-alone JDBC code.
6+
*/
7+
8+
// You need to import the java.sql package to use JDBC
9+
import java.sql.*;
10+
11+
/*
12+
* You need to import the oracle.sql package to use
13+
* oracle.sql.BLOB
14+
*/
15+
import oracle.sql.*;
16+
17+
public class TrimLob
18+
{
19+
public static void main (String args []) throws SQLException {
20+
Connection conn;
21+
/*
22+
* Where is your code running: in the database or outside?
23+
*/
24+
if (System.getProperty("oracle.jserver.version") != null)
25+
{
26+
/*
27+
* You are in the database, already connected, use the default
28+
* connection
29+
*/
30+
conn = DriverManager.getConnection("jdbc:default:connection:");
31+
}
32+
else
33+
{
34+
/*
35+
* You are not in the database, you need to connect to
36+
* the database
37+
*/
38+
39+
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
40+
conn =
41+
DriverManager.getConnection("jdbc:oracle:thin:", "scott",
42+
"tiger");
43+
}
44+
long t0,t1;
45+
/*
46+
* auto commit is off by default in OracleJVM (not supported)
47+
* It's faster with JDBC when auto commit is off
48+
*/
49+
conn.setAutoCommit (false);
50+
t0=System.currentTimeMillis();
51+
// Create a Statement
52+
Statement stmt = conn.createStatement ();
53+
54+
// clean up
55+
try
56+
{
57+
stmt.execute ("drop table basic_lob_table");
58+
}
59+
catch (SQLException e)
60+
{
61+
// An exception could be raised here if the
62+
// table did not exist already.
63+
}
64+
65+
// Create a table containing a BLOB and a CLOB
66+
stmt.execute ("create table basic_lob_table (x varchar2 (30), " +
67+
"b blob, c clob)");
68+
// Populate the table
69+
stmt.execute ("insert into basic_lob_table values ('first', " +
70+
"'010101010101010101010101010101', " +
71+
"'one.two.three.four.five.six.seven')");
72+
stmt.execute ("insert into basic_lob_table values ('second', " +
73+
"'0202020202020202020202020202020202020202', " +
74+
"'two.three.four.five.six.seven.eight.nine.ten')");
75+
76+
/*
77+
* Retive Lobs and modify contents; this can be done by doing
78+
* "select ... for update", but "autocommit" is turned off and
79+
* the previous "create table" statement already locks the table
80+
*/
81+
ResultSet rset = stmt.executeQuery
82+
("select * from basic_lob_table");
83+
while (rset.next ())
84+
{
85+
// Get the lobs
86+
BLOB blob = (BLOB) rset.getObject (2);
87+
CLOB clob = (CLOB) rset.getObject (3);
88+
89+
// Show the original lob length
90+
System.out.println ("Show the original lob length");
91+
System.out.println ("blob.length()="+blob.length());
92+
System.out.println ("clob.length()="+clob.length());
93+
94+
// Trim the lobs
95+
System.out.println ("Trim the lob to legnth = 6");
96+
blob.trim (6);
97+
clob.trim (6);
98+
99+
// Show the lob length after trim()
100+
System.out.println ("Show the lob length after trim()");
101+
System.out.println ("blob.length()="+blob.length());
102+
System.out.println ("clob.length()="+clob.length());
103+
}
104+
105+
// Close the ResultSet
106+
rset.close ();
107+
108+
// Close the Statement
109+
stmt.close ();
110+
111+
t1=System.currentTimeMillis();
112+
System.out.println ("====> Duration: "+(int)(t1-t0)+ "Milliseconds");
113+
// Close the connection
114+
conn.close ();
115+
}
116+
}
117+
/
118+
119+
show errors;
120+
121+
alter java source TrimLob compile;
122+
123+
show errors;
124+
125+
create or replace procedure TrimLobSp as
126+
language java name 'TrimLob.main(java.lang.String[])';
127+
/
128+
129+
show errors;
130+
set serveroutput on
131+
call dbms_java.set_output(50000);
132+
133+
call TrimLobSp();
134+

0 commit comments

Comments
 (0)