Application of JDBC Advanced data type

Source: Internet
Author: User
Tags count insert sql query reference oracle database
Advanced | data | Data Type JDBC 2.0 introduces many new objects corresponding to sql_99, which have blob,clob,array,ref, structured types, distinct types, and locator.
JDBC 3.0 Adds a Boolean and Datalink object
The primary means of inserting these advanced data types into the database is to use the PreparedStatement object, which reads primarily the ResultSet object. Here's how to read and write advanced data types in a database

1:blob and Clob
BLOB: A binary Large object (Binary Large object) is a sequence of bytes (metaphorically speaking a MP3 file can be stored as a blob)
CLOB: A string that is too long for a varchar or similar column.
Blob and CLOB data from the database can be manipulated by Java.sql.Blob and Java.sql.clob objects.
The ResultSet and PreparedStatement objects provide the following methods for processing these two types of data
Resultset:preparedstatement
Blob getblob (int) void Setblob (int, Blob)//The first argument is the index of the placeholder in PreparedStatement, the following same
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 up placeholders in prepared statements, and you can write that data to another table by executing SQL statements
Such as:
String sql= "Select Blob_col from blob_table where id=?" Blob_colum, the column name for the table with ID blob_table
PreparedStatement ps=connection.preparestatement (SQL);
Ps.setint (1,1);
ResultSet Rset=ps.executequery ();
Blob Blob=null;
if (Rset.next ())
{
Blob=rset.getblob (1);
}
The blob in the above only holds a reference to these binary data in the database. The actual binary data is not held, and the code can then write the binary data to another table using the same reference:
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 fetching data from a database or writing data to a database by obtaining a stream (input or output) object from the database. and reading data or writing from the stream.
Cases:
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 writing data to BLOBs, so we use Oracle extensions
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 storing data in a database
while (Cout!=-1)
{
Out.write (B,o,count);
Cout=in.read (b,o,buffersize);
}
Data finished
Out.close ();
In.close ();
Connection.commit ()/Submit Change
........
}
Similarly, we can get an input stream from a blob and write BLOB data to a file
InputStream In=blob.getbinarystream ();
int buffersize = ((Oracle.sql.Blob) Blob). GetBufferSize ();
2: Structured data types
Structured data types are similar to a Java object. As follows, we use SQL to define a people type
CREATE OR REPLACE TYPE people as OBJECT (NAME varchar,age INT),//oracle database ...
You can now use people to use this data type anywhere
Such as
CREATE TABLE SAMPLES (sa_id number, Curson people,sample BLOB);
Now we can manipulate these data types using SetObject () and GetObject ()
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 created earlier, t it must contain a member variable for each column in the table
String sql= "INSERT into SAMPLE (Sa_id,curson) VALUES (?,?)";
People people=new people ();
Ps=connection.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
The distinct type is like an alias of an builtin type, and we can define this type
CREATE TYPE Birthdate as DATE
Since this new type is just an alias that points to an existing built-in type, we can use the GETDATE () and Setdate () method;
4: Construction Type
Array and ref object (reference objects)
ResultSet preparestatement
Array getarray (int) void SetArray (int, Array)
Array GetArray (String)
REF getRef (int) void Setref (INT,REF)
Ref GetRef (String)
The array method can access row and column values by making row and column indexes. The array object can also be returned as a resultset. (ResultSet only means to access those column values in a set of rows)
Above we define a people custom type and a sample table. If you execute a query that returns a People column, you can use the Getref () method, and we'll get a reference to the Prople object in that column.

5:datalink
A new feature of JDBC 3.0 that Datalink objects to describe data that is accessed externally to the database
The Datalink value is processed using SetUrl () and Geturl ().
ResultSet preparestatement
URL GetURL (int) void SetUrl (int, URL)
URL GetURL (String)





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.