JDBC 2.0 introduces many new objects corresponding to SQL_99, these new objects have BLOB, Clob, Array, Ref, Structure Type, Distinct Type, and Locator.jdbc 3.0 Added Boolean and DataLink objects inserting these advanced data types into the database The primary means is to use the PreparedStatement object, read the main RESULTSET object. Here is how to read and write advanced data types in the database.
1: Blob and Clob Blob: Binary Large Object, is a byte sequence (a metaphor that an MP3 file can be stored as a blob) Clob: a string for VARCHAR or a similar column. From The database's BLOB and Clob data can be operated via java.sql.blob and java.sql.clob object. ResultSet and preparedStatement objects provide the process of these two data as follows: PreparedStatement Blob getBlob (int) void setblob (int, BLOB) // The first parameter is the index of the placeholder in preparedStatement, the following BLOB Getblob (String) Void SetClob (int, clob) Clob getClob (int) Clob getClob (String) Using PreparedStatement.setblob (int, blob) We can use BLOB data to set the placeholders in the preparation statement, and can write these data into another table by performing the SQL statement, such as: string sql = "SELECT BLOB_COL from blob_table where id =?" // blob_colum , ID is a list of BLOB_TABLE, PREPAREDSTATEMENT (SQL); ps.setint (1, 1); ResultSet Rset = ps.executeQuery (); blob blob = null; if (rset.next ()) {Blob = RSET.GetBlob (1);} BLOB in the last section is only a reference to these binary data in the database. Do not hold actual binary data, then the code can use this same reference to write these binary data To another table: SQL = "INSERT INTO BLOB_TABLE_2 VALUES"; ps = connection.preparestatement (SQL); ps.setblob (1, blob); ps.executeUpdate ();
The BLOB and CLOB excuses in JDBC 2.0 provide a means of obtaining data or writing data from the database to the database, which is a stream (input or output) object from the database. And from the stream Or write. Example: OutputStream out = null; bufferedinputstream in = null; file file = new file ("****"); reslutSet Rset = Statement.executeQuery (SQL); // Get a result set from the query statement IF (rset.next ()) {blob blob = rset.getblob (1); OUT = ((Oracle.sql.blob) blob) .GetBinaryoutputStream (); // JDBC 2.0 does not support write data to blob, so we use Oracle extended int bufferSize == ((oracle.sql.Blob) blob) .getBufferSize (); in = new BufferedInputStream (new fileInputStream (file), bufferSize); byte [] b = new byte [bufferSize]; int count = In.read (B, 0, Buffersize); // Start Storage Data to the database while (cout! = - 1) {Out.write (B, O, Count); cout = in.read (B, O, Buffersize );} // data is written out.close (); in.close (); connection.commit (); // Submit change ........} Similar, we can get an input from the blob Stream, write BLOB data into the file to INPUTSTREAM IN = blob.getBinaryStream (); int buffersize = ((Oracle.Sql.blob) blob) .getBuffersize (); 2: Structured Data Type Structured Data Type Similar to A Java object. As follows, we define a people type Create or Replace Type people as object (Name var); // Oracle database now, you can now use the PEOPLE Samples such as Create Table Samples now. SA_ID NUMBER, Curson People, now we can use setObject () and getObject () to manipulate these data types RESULTSET: PreparedStatement Object GetObject (int) void getobject (int, object) Object getObject (string) ....... ... Example: Public Class People Implements Sqldata, Serializable {...} Map Map = Connection.gettypeMap (); Map.Put ("people", people.class); // people This class must be in front Created, t, which must contain each column in the table String SQL = "INSERT INTO SAMPLE (SA_ID, CURSON) VALUES (?,?)"; People people = new people (); ps = connections.preparestatement SQL); ps.setint (1, 1); ps.setobject (2, people); int result = ps.executeUpdate (); we can also get this people object from the database String SQL = "
Select * from sample where sa_id = 1 "; ps = connection.preparestatement (SQL); ResultSet Rset = ps.executeQuery (); if (Rset.Next ()) {people people = (people) RSET.GETObject (2); } 3: Distinct Type Distinct Type is like an alias of a built-in type, we can define this type of Create Type BirthDate as date Since this new type is just a alias pointing to an existing built-in type, we can use getDate () And setdate () method; 4: Construct Type Array and Ref Object (Reference Object) Resultset PrepareStateArray GetArray (int) ARRAY GetArray (String) Ref getRef (int) Void SetRef (int, ref) Ref GetRef (String) Array method can deliver row values and column values by making row indexes and columns. The Array object can also be returned by an array as a resultset. (Resultset is just to access those columns in a row Meanship) We define a people custom type and sample table. If a query that returns a PEOPLE column, you can use getRef () method, we will get a reference to the prople object in this column 5: A new feature of DatalinkJDBC 3.0, Datalink object describes the data Datalink value on the database to the database Datalink value to process the ResultSet PrepareStatementURL GetURL (int) Void SetURL (INT, URL) URL GetURL (String)