JDBC performs preprocessing, batching, LOB field processing, calling stored procedures

Source: Internet
Author: User

(1) JDBC performs preprocessing

Preparedstatment Prep Statement

Eg:string sql= "INSERT into User (Id,name,birthday,money) VALUES (5, ' Administrator ',?, ' 1000000 ')";

PreparedStatement stmt=conn.preparestatment (SQL);

Stmt.settimestamp (1,new Timestamp (System.currenttimemillis ()));

Stmt.execute ();

(2) calling a stored procedure

For scalar functions its invocation string is {fn functionname (?,?)} /{FN functionname ()}

The calling string for the stored procedure is {call proc (?,?)} /{call proc}/{call? =proc (?,?)}

CallableStatement

No parameter, no return value of stored procedure

String sql= "{Call test1 ()}";

Callablestatment stmt=conn.preparecall (SQL);

Stmt.executeupdate ();

Stored procedure with parameters and return values

String sql= "{call =test1 (?)}"; Mode 1

Callablestatment stmt=conn.preparecall (SQL);

Stmt.setstring (2, "test");

Stmt.registeroutparameter (1,type.interger);

Stmt.execute ();

int Result=stmt.getint (1);

String sql= "{call =test1 (?,?)}"; Mode 2

Callablestatment stmt=conn.preparecall (SQL);

Stmt.setstring (1, "Test");

Stmt.registeroutparameter (2,type.interger);

Stmt.execute ();

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.executebatch ();

(4) Lob field (BLOB,CLOB) processing

Handling 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 Rt=stmt.executequery ();

if (Rt.next ()) {

Blob Blob=rt.getblob (1);

Image Img=imageio.read (Blob.getinputstream ());

}

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

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 (a);

Stmt.setblob (2,BLOB);

Stmt.executeupdate ();

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

JDBC performs preprocessing, batching, LOB field processing, calling stored procedures

Related Article

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.