Jdbc pre-processing, batch processing, LOB field processing, calling stored procedure, jdbclob

Source: Internet
Author: User

Jdbc pre-processing, batch processing, LOB field processing, calling stored procedure, jdbclob

(1) jdbc execution preprocessing

PreparedStatment preparation statement

Eg: String SQL = "insert into user (id, name, birthday, money) values (5, 'admin ',?, '123 ')";

PreparedStatement stmt = conn. prepareStatment (SQL );

Stmt. setTimestamp (1, new Timestamp (System. currentTimeMillis ()));

Stmt.exe cute ();

(2) Call the Stored Procedure

For a scalar function, the call String is {fn functionname (?,?)} /{Fn functionname ()}

For a stored procedure, the call String is {call proc (?,?)} /{Call proc}/{call? = Proc (?,?)}

CallableStatement

Stored Procedure with no parameters or returned values

String SQL = "{call test1 ()}";

CallableStatment stmt = conn. prepareCall (SQL );

Stmt.exe cuteUpdate ();

Stored Procedures with parameters and returned values

String SQL = "{call? = Test1 (?)} "; // Method 1

CallableStatment stmt = conn. prepareCall (SQL );

Stmt. setString (2, "test ");

Stmt. registerOutParameter (1, Type. Interger );

Stmt.exe cute ();

Int result = stmt. getInt (1 );

 

String SQL = "{call? = Test1 (?,?)} "; // Method 2

CallableStatment stmt = conn. prepareCall (SQL );

Stmt. setString (1, "test ");

Stmt. registerOutParameter (2, Type. Interger );

Stmt.exe cute ();

Int result = stmt. getInt (2 );

(3) jdbc data Batch Processing

String SQL = "insert into user (id, name, birthday, money) values (?,?,?,?) ";

PreparedStatement stmt = conn. prepareStatement (SQL );

For (int I = 1; I <1000; I ++ ){

Stmt. setInt (1, I );

Stmt. setString (2, "test ");

Stmt. setDate (3, new Date (System. currentTimeMillis ()));

Stmt. setFloat (4,1000 );

Stmt. addBatch (SQL );

}

Int [] result=stmt.exe cuteBatch ();

(4) LOB field (BLOB, CLOB) Processing

Common classes and Methods

Java. SQL. ResultSet

Blob getBlob (int columnIndex );

Blob getBlob (string columnLabel );

Clob getClob (int columnIndex );

Clob getClob (string columnLabel );

-----------------------------------------------------------------

Java. SQL. Blob

Long length ();

Byte [] getBytes (long startPosition, long length );

InputStream getBinaryStream ();

InputStream getBinaryStream (long startPosition, long length );

OutputStream setBinaryStream (long startPosition );

Java. SQL. Clob

Long length ();

Byte [] getSubString (long startPosition, long length );

Reader getCharacterStream ();

Reader getCharacterStream (long startPosition, long length );

Writer setCharacterStream (long startPosition );

-----------------------------------------------------------------

Java. SQL. Connection

Blob createBlob ();

Clob createClob ();

----------------------------------------------------------------

Eg:

PreparedStament stmt = conn. prepareStatement ("select pic from book where bokkid = '1 '");

ResultSet rt1_stmt.exe cuteQuery ();

If (rt. next ()){

Blob blob = rt. getBlob (1 );

Image img = ImageIO. read (blob. getInputStream ());

}

Bytes --------------------------------------------------------------------------------------------

Blob blob = conn. createBlob ();

Int offset = 0;

OutputStream out = blob. setBinaryStream (offset );

ImageIO. write (img, "PNG", out );

PreparedStament stmt = conn. prepareStatement ("insert into book values (?,?) ");

Stmt. setInt (1, 1 );

Stmt. setBlob (2, blob );

Stmt.exe cuteUpdate ();

Bytes --------------------------------------------------------------------------------------------

 

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.