Oracle paging Stored Procedure oracle Stored Procedure instance

Source: Internet
Author: User

Copy codeThe Code is as follows:
Import java. SQL. CallableStatement;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Public class TestPage {

Public TestPage (){
}
Public static void main (String [] args ){
String driver = "oracle. jdbc. driver. OracleDriver ";
String strUrl = "jdbc: oracle: thin: @ localhost: 1521: xmgl ";
String username = "xmgl ";
String password = "xmgl ";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
Try {
Class. forName (driver );
Conn = DriverManager. getConnection (strUrl, username, password );
System. out. println ("OK ");
CallableStatement cs = null;
// Create CallableStatement
Cs = conn. prepareCall ("{call page. fenye (?,?,?,?,?,?)} ");
Cs. setString (1, "mytest"); // table name
Cs. setInt (2, 3); // display several records
Cs. setInt (3, 1); // page number
// Register the total number of records and declare the type of output parameters
Cs. registerOutParameter (4, oracle. jdbc. OracleTypes. INTEGER );
// Total number of registered pages
Cs. registerOutParameter (5, oracle. jdbc. OracleTypes. INTEGER );
// Register the returned result set
Cs. registerOutParameter (6, oracle. jdbc. OracleTypes. CURSOR );
Cs.exe cute ();
// Retrieve the total number of records/Note that 4 in getInt (4) is determined by the position of this parameter.
Int rowNum = cs. getInt (4); // obtain the output parameter
Int pageCount = cs. getInt (5 );
Rs = (ResultSet) cs. getObject (6 );

System. out. println ("rowNum =" + rowNum );
System. out. println ("Total number of pages =" + pageCount );
While (rs. next ()){
System. out. println ("No.:" + rs. getInt (1) + "name:" + rs. getString (2) + "Password:" + rs. getString (3 ));
}
} Catch (SQLException ex2 ){
Ex2.printStackTrace ();
} Catch (Exception ex2 ){
Ex2.printStackTrace ();
} Finally {
Try {
If (rs! = Null ){
Rs. close ();
If (stmt! = Null ){
Stmt. close ();
}
If (conn! = Null ){
Conn. close ();
}
}
} Catch (SQLException ex1 ){
}
}
}
}

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.