forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrimLobSp.sql
More file actions
134 lines (114 loc) · 3.83 KB
/
Copy pathTrimLobSp.sql
File metadata and controls
134 lines (114 loc) · 3.83 KB
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
create or replace java source named TrimLob as
/*
* This SQL script generated a Java code performing BLOB/CLOB operations.
* The goal is to show Java in the database and contrat its performance with the same code
* running as a client-side or stand-alone JDBC code.
*/
// You need to import the java.sql package to use JDBC
import java.sql.*;
/*
* You need to import the oracle.sql package to use
* oracle.sql.BLOB
*/
import oracle.sql.*;
public class TrimLob
{
public static void main (String args []) throws SQLException {
Connection conn;
/*
* Where is your code running: in the database or outside?
*/
if (System.getProperty("oracle.jserver.version") != null)
{
/*
* You are in the database, already connected, use the default
* connection
*/
conn = DriverManager.getConnection("jdbc:default:connection:");
}
else
{
/*
* You are not in the database, you need to connect to
* the database
*/
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
conn =
DriverManager.getConnection("jdbc:oracle:thin:", "scott",
"tiger");
}
long t0,t1;
/*
* auto commit is off by default in OracleJVM (not supported)
* It's faster with JDBC when auto commit is off
*/
conn.setAutoCommit (false);
t0=System.currentTimeMillis();
// Create a Statement
Statement stmt = conn.createStatement ();
// clean up
try
{
stmt.execute ("drop table basic_lob_table");
}
catch (SQLException e)
{
// An exception could be raised here if the
// table did not exist already.
}
// Create a table containing a BLOB and a CLOB
stmt.execute ("create table basic_lob_table (x varchar2 (30), " +
"b blob, c clob)");
// Populate the table
stmt.execute ("insert into basic_lob_table values ('first', " +
"'010101010101010101010101010101', " +
"'one.two.three.four.five.six.seven')");
stmt.execute ("insert into basic_lob_table values ('second', " +
"'0202020202020202020202020202020202020202', " +
"'two.three.four.five.six.seven.eight.nine.ten')");
/*
* Retive Lobs and modify contents; this can be done by doing
* "select ... for update", but "autocommit" is turned off and
* the previous "create table" statement already locks the table
*/
ResultSet rset = stmt.executeQuery
("select * from basic_lob_table");
while (rset.next ())
{
// Get the lobs
BLOB blob = (BLOB) rset.getObject (2);
CLOB clob = (CLOB) rset.getObject (3);
// Show the original lob length
System.out.println ("Show the original lob length");
System.out.println ("blob.length()="+blob.length());
System.out.println ("clob.length()="+clob.length());
// Trim the lobs
System.out.println ("Trim the lob to legnth = 6");
blob.trim (6);
clob.trim (6);
// Show the lob length after trim()
System.out.println ("Show the lob length after trim()");
System.out.println ("blob.length()="+blob.length());
System.out.println ("clob.length()="+clob.length());
}
// Close the ResultSet
rset.close ();
// Close the Statement
stmt.close ();
t1=System.currentTimeMillis();
System.out.println ("====> Duration: "+(int)(t1-t0)+ "Milliseconds");
// Close the connection
conn.close ();
}
}
/
show errors;
alter java source TrimLob compile;
show errors;
create or replace procedure TrimLobSp as
language java name 'TrimLob.main(java.lang.String[])';
/
show errors;
set serveroutput on
call dbms_java.set_output(50000);
call TrimLobSp();