Java-data storage process (54), java Data Storage

Source: Internet
Author: User

Java-data storage process (54), java Data Storage

Stored procedure: A piece of executable program. This program runs in data.

Begin = {

End =}

If

Else

You can also receive parameters.

1 define a procedure:

CREATE PROCEDURE proc1()BEGIN  ――{  SELECT * FROM users;END // ―― }DELIMITER ;CALL proc1();

2. Define a receiving parameter:

DELIMITER $$CREATE PROCEDURE proc2(IN _id VARCHAR(32),IN _nm VARCHAR(30))BEGIN  INSERT INTO users(id,NAME) VALUES(_id,_nm);END $$CALL proc2('U003','Rose');

3. Define a response type parameter:

DELIMITER $$CREATE PROCEDURE proc3(IN _id VARCHAR(32),IN _nm VARCHAR(30),OUT _size INT)BEGIN  INSERT INTO users(id,NAME) VALUES(_id,_nm);  SELECT COUNT(1) INTO _size FROM users;  IF(_size=10) THEN     INSERT INTO users VALUES('U0000','ddd','ddddd');  END IF;END $$CALL proc3('U002882','Rose',@wj);SELECT @wj;

Call Procedure in Java code

PackageCn. hx. demo;

ImportJava. SQL. CallableStatement;

ImportJava. SQL. Connection;

ImportJava. SQL. ResultSet;

ImportJava. SQL. Types;

ImportOrg. junit. Test;

ImportCn. itcast. utils. performanceutils;

Public ClassProceDemo {

@ Test

Public VoidProc1 ()ThrowsException {

// Dbutils does not provide the ability to call stored procedures

Connection con = performanceutils.GetDatasSource(). GetConnection ();

// Obtain the object of the call Process

CallableStatement cs =

Con. prepareCall ("{call proc1 ()}");

// Execute

BooleanBoo = cs.exe cute (); // if true is returned, the select statement is executed in the last sentence.

If(Boo ){

ResultSet rs = cs. getResultSet ();

While(Rs. next ()){

System.Err. Println (rs. getString ("name "));

}

}

Con. close ();

}

@ Test

Public VoidProc2 ()ThrowsException {

Connection con = performanceutils.GetDatasSource(). GetConnection ();

// Obtain the object of the call Process

CallableStatement cs =

Con. prepareCall ("{call proc2 (?,?)} ");

Cs. setString (1, "UAAA ");

Cs. setString (2, "11 ");

BooleanBoo = cs.exe cute ();

System.Err. Println (boo );

Con. close ();

}

@ Test

Public VoidProc3 ()ThrowsException {

Connection con = performanceutils.GetDatasSource(). GetConnection ();

// Obtain the object of the call Process

CallableStatement cs =

Con. prepareCall ("{call proc5 (?,?,?)} ");

Cs. setString (1, "UBDDB ");

Cs. setString (2, "Zhang San ");

Cs. registerOutParameter (3, Types.INTEGER); // -- Int,

BooleanBoo = cs.exe cute ();

System.Err. Println (">>:" + boo); // true

// Obtain the returned value from the call.

IntSize = cs. getInt (3 );

System.Err. Println ("number of rows:" + size );

If(Boo ){

ResultSet rs = cs. getResultSet ();

Rs. next ();

IntSs = rs. getInt (1 );

System.Err. Println ("sss:" + ss );

}

Con. close ();

}

@ Test

Public VoidProc6 ()ThrowsException {

Connection con = performanceutils.GetDatasSource(). GetConnection ();

// Obtain the object of the call Process

CallableStatement cs =

Con. prepareCall ("{call proc6 (?,?,?,?)} ");

Cs. setString (1, "UBafadsB ");

Cs. setString (2, "Zhang San ");

Cs. registerOutParameter (3, Types.INTEGER); // -- Int,

Cs. registerOutParameter (4, Types.INTEGER);

BooleanBoo = cs.exe cute ();

System.Err. Println (">>:" + boo); // faluse

// Obtain the returned value from the call.

IntSize = cs. getInt (3 );

Int_ S = cs. getInt (4 );

System.Err. Println ("number of rows:" + size + "," + _ s );

Con. close ();

}

}

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.