Nwlapcug.com


Come utilizzare CLOB in Java

Come utilizzare CLOB in Java


È ora possibile completare numerose attività attraverso Java Database Connectivity (JDBC) API Application Programming Interface () utilizzando colonne di database CLOB. CLOB, che sta per "Character Large OBject", è uno strumento di dati utilizzato per archiviare e recuperare grandi quantità di dati di testo in formato carattere. Un CLOB è in grado di memorizzare fino a 128 terabyte di dati di tipo carattere nel database. La piena compatibilità con Java lo rende una scelta preferita da molti sviluppatori di software.

Istruzioni

1

Utilizzare il seguente frammento di codice per creare un oggetto CLOB, cui "rs" è un oggetto ResultSet:

CLOB clob = rs.getClob(1);

La variabile "clob" è ora un operatore funzionale sul valore CLOB memorizzato nella prima colonna del set di risultati "rs".

2

Inserire il codice seguente al database per l'esecuzione di due valori CLOB identici in una nuova tabella, che sono in grado di contenere 500 kilobyte di dati di testo ridondanti:

importazione java.sql.*;

public class PutGetClobs {

Public Shared Sub main (String [] args)

throws SQLException

{

// Register the native JDBC driver.

try {

Class.forName("com.ibm.db2.jdbc.app.DB2Driver");

} catch (Exception e) {

System.exit(1); // Setup error.

}

// Establish a Connection and Statement with which to work.

Connection c = DriverManager.getConnection("jdbc:db2:*local");

Statement s = c.createStatement();

// Clean up any previous run of this application.

try {

s.executeUpdate("DROP TABLE CUJOSQL.CLOBTABLE");

} catch (SQLException e) {

// Ignore it - assume the table did not exist.

}

// Create a table with a CLOB column. The default CLOB column

// size is 1 MB.

s.executeUpdate("CREATE TABLE CUJOSQL.CLOBTABLE (COL1 CLOB)");

// Create a PreparedStatement object that allow you to put

// a new Clob object into the database.

PreparedStatement ps = c.prepareStatement("INSERT INTO CUJOSQL.CLOBTABLE VALUES(?)");

// Create a big CLOB value...

StringBuffer buffer = new StringBuffer(500000);

while (buffer.length() < 500000) {

buffer.append("All work and no play makes Cujo a dull boy.");

}

String clobValue = buffer.toString();

// Set the PreparedStatement parameter. This is not

// portable to all JDBC drivers. JDBC drivers do not have

// to support setBytes for CLOB columns. This is done to

// allow you to generate new CLOBs. It also

// allows JDBC 1.0 drivers a way to work with columns containing

// Clob data.

ps.setString(1, clobValue);

// Process the statement, inserting the clob into the database.

ps.executeUpdate();

// Process a query and get the CLOB that was just inserted out of the

// database as a Clob object.

ResultSet rs = s.executeQuery("SELECT * FROM CUJOSQL.CLOBTABLE");

rs.next();

Clob clob = rs.getClob(1);

// Put that Clob back into the database through

// the PreparedStatement.

ps.setClob(1, clob);

ps.execute();

c.close(); // Connection close also closes stmt and rs.

}

}

3

Inserire il codice seguente al database per modificare il comportamento degli oggetti CLOB:

importazione java.sql.*;

public class UpdateClobs {

Public Shared Sub main (String [] args)

throws SQLException

{

// Register the native JDBC driver.

try {

Class.forName("com.ibm.db2.jdbc.app.DB2Driver");

} catch (Exception e) {

System.exit(1); // Setup error.

}

Connection c = DriverManager.getConnection("jdbc:db2:*local");

Statement s = c.createStatement();

ResultSet rs = s.executeQuery("SELECT * FROM CUJOSQL.CLOBTABLE");

rs.next();

Clob clob1 = rs.getClob(1);

rs.next();

Clob clob2 = rs.getClob(1);

// Truncate a CLOB.

clob1.truncate((long) 150000);

System.out.println("Clob1's new length is " + clob1.length());

// Update a portion of the CLOB with a new String value.

String value = "Some new data for once";

int charsWritten = clob2.setString(500L, value);

System.out.println("Characters written is " + charsWritten);

// The bytes can be found at position 500 in clob2

long startInClob2 = clob2.position(value, 1);

System.out.println("pattern found starting at position " + startInClob2);

c.close(); // Connection close also closes stmt and rs.

}

}

4

Applicare il codice seguente per eseguire CLOB nel vostro database. CLOB chiuderà da sè dopo aver eseguito le sue funzioni elencate di seguito.

importazione java.sql.*;

public class UseClobs {

Public Shared Sub main (String [] args)

throws SQLException

{

// Register the native JDBC driver.

try {

Class.forName("com.ibm.db2.jdbc.app.DB2Driver");

} catch (Exception e) {

System.exit(1); // Setup error.

}

Connection c = DriverManager.getConnection("jdbc:db2:*local");

Statement s = c.createStatement();

ResultSet rs = s.executeQuery("SELECT * FROM CUJOSQL.CLOBTABLE");

rs.next();

Clob clob1 = rs.getClob(1);

rs.next();

Clob clob2 = rs.getClob(1);

// Determine the length of a LOB.

long end = clob1.length();

System.out.println("Clob1 length is " + clob1.length());

// When working with LOBs, all indexing that is related to them

// is 1-based, and not 0-based like strings and arrays.

long startingPoint = 450;

long endingPoint = 50;

// Obtain part of the CLOB as a byte array.

String outString = clob1.getSubString(startingPoint, (int)endingPoint);

System.out.println("Clob substring is " + outString);

// Find where a sub-CLOB or string is first found within a

// CLOB. The setup for this program placed two identical copies of

// a repeating CLOB into the database. Thus, the start position of the

// string extracted from clob1 can be found in the starting

// position in clob2 if the search begins close to the position where

// the string starts.

long startInClob2 = clob2.position(outString, 440);

System.out.println("pattern found starting at position " + startInClob2);

c.close(); // Connection close also closes stmt and rs.

}

}

Consigli & Avvertenze

  • Tutorial gratuiti online sono disponibili per Java e CLOB. Imparare come molti come si può per acquisire familiarità con queste tecnologie Web efficiente.
  • Applicare il codice per apportare modifiche agli oggetti CLOB ed eseguire l'elemento CLOB solo dopo aver completato il programma di "PutGetClobs" sul vostro database per mantenere intatte tutte le tabelle di dati.