forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrimLob.java
More file actions
112 lines (100 loc) · 3.48 KB
/
TrimLob.java
File metadata and controls
112 lines (100 loc) · 3.48 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
/*
/------------------------------------------------------------------------------
/ Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
/
/ Portions Copyright 2006-2015, Kuassi Mensah. All rights reserved.
/
/
/------------------------------------------------------------------------------
/ DESCRIPTION
/ This sample shows basic BLOB/CLOB operations. It drops, creates, and populates a basic_lob_table
/ with blob, clob data types columns then fetches the rows and trim both LOB and CLOB
*/
// Need to import the java.sql package to use JDBC
import java.sql.*;
/*
* 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:@localhost:1522/orcl", "scott",
"tiger");
}
long t0,t1;
/*
* auto commit is off (not suported)by default in OracleJVM
* It's faster with JDBC when auto commit is off
*/
conn.setAutoCommit (false);
t0=System.currentTimeMillis();
// Create a Statement
Statement stmt = conn.createStatement ();
// Make sure the table is empty
stmt.execute("delete from basic_lob_table");
stmt.execute("commit");
// 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 for update");
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.truncate (6);
clob.truncate (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 and Commit changes
rset.close ();
stmt.execute("commit");
// Close the Statement
stmt.close ();
t1=System.currentTimeMillis();
System.out.println ("====> Duration: "+(int)(t1-t0)+ "Milliseconds");
// Close the connection
conn.close ();
}
}