[JAVA100 example]055, scrollable result set

Source: Internet
Author: User
Tags odbc odbc connection stmt create database

Import java.sql.*;


/**


* <P>TITLE:ODBC Connection Database </p>


* <p>description: This example demonstrates how to manipulate a database using the JDBC Bridge. </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:JDBCScrollConn.java</p>


* @version 1.0


*/


public class jdbcscrollconn{


private static String url= "";


private static String username= "";


private static String password= "";


/**


*<br> Method Description: Obtain data connection


*<br> input Parameters:


*<br> return Type: Connection Connection object


*/


public Connection Conn () {


try {


//load JDBC driver


class.forname ("Oracle.jdbc.driver.OracleDriver");


//CREATE DATABASE connection


Connection con = drivermanager.getconnection (URL, username, password);


return con;


}catch (SQLException sqle) {


System.out.println ("Can´t Connection db:" +sqle);


return null;


} catch (Exception e) {


System.out.println ("Failed to load Jdbc/odbc driver.");


return null;


   }


 }


/**


*<br> Method Description: Execute query SQL statement


*<br> input parameters: Connection Con database connection


*<br> input parameter: String SQL SQL statement to execute


*<br> return type:


*/


public void query (Connection con, String sql) {


try{


if (con==null) {


throw new Exception ("Database connection can´t use!");


   }


if (sql==null) throw new Exception ("Check your parameter:´sql´! Don´t input null! ");


//Declaration Statements


Statement stmt = Con.createstatement ();


Execute Query


ResultSet rs = stmt.executequery (SQL);


ResultSetMetaData Rmeta = Rs.getmetadata ();


//Get the number of data fields


int numcolumns = Rmeta.getcolumncount ();


while (Rs.next ())


 {


for (int i = 0;i< numcolumns;i++)


  {


String stemp = rs.getstring (i+1);


System.out.print (stemp+ "");


  }


System.out.println ("");


 }


}catch (Exception e) {


System.out.println ("Query error:" +e);


  }


 }


/**


*<br> Method Description: Execute query SQL statement


*<br> input parameters: Connection Con database connection


*<br> input parameter: String SQL SQL statement to execute


*<br> Input Parameters: PageNo page number


*<br> input parameter: PageSize Record bar number


*<br> return type:


*/


public void query (Connection con, String sql,int pageno,int pageSize) {


try{


if (con==null) {


throw new Exception ("Database connection can´t use!");


   }


if (sql==null) throw new Exception ("Check your parameter:´sql´! Don´t input null! ");


int introwcount;//Records Total


int intpagecount;//Total pages


//Declaration Statements


Statement stmt = con.createstatement (resultset.type_scroll_insensitive,resultset.concur_read_only);


//Execute Query


ResultSet rs = stmt.executequery (SQL);


ResultSetMetaData Rmeta = Rs.getmetadata ();


//Get the number of data fields


int numcolumns = Rmeta.getcolumncount ();


//Get the total number of records


Rs.last ();


Introwcount = Rs.getrow ();


//Count Total pages


Intpagecount = (introwcount+pagesize-1)/pageSize;


//Adjust the page number to be displayed


if (pageno>intpagecount) pageno = Intpagecount;


if (intpagecount>0) {


//Position the record pointer on the first record of the page to be displayed


Rs.absolute (pageNo-1) * pageSize + 1);


int i = 0;


while (i<pagesize &&!rs.isafterlast ())


 {


for (int j = 0;j< numcolumns;j++)


  {


String stemp = rs.getstring (j+1);


System.out.print (stemp+ "");


  }


i++;


Rs.next ();


System.out.println ("");


 }


}


}catch (Exception e) {


System.out.println ("Query error:" +e);


  }


 }


/**


*<br> Method Description: Perform inserts, updates, deletes, and so on without returning a result set of SQL statements


*<br> input parameters: Connection Con database connection


*<br> input parameter: String SQL SQL statement to execute


*<br> return type:


*/


public void execute (Connection con, String sql) {


try{


if (con==null) return;


Statement stmt = Con.createstatement ();


stmt.executeupdate (SQL);


}catch (Exception e) {


System.out.println ("Query error:" +e);


  }


 }


/**


*<br> Method Description:


*<br> input Parameters:


*<br> return type:


*/


public void Demo (int pageno,int pageSize) {


try{


jdbcscrollconn JC = new Jdbcscrollconn ();


Connection conn = Jc.conn ();


String sql = "SELECT * from Tbl_user order by id";


jc.query (conn,sql,pageno,pagesize);


Conn.close ();


}catch (SQLException se) {


System.out.println (SE);


}catch (Exception e) {


System.out.println (e);


  }


 


 }


/**


*<br> Method Description: Main method


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] arg) {


if (arg.length!=5) {


System.out.println ("Use:java jdbcscrollconn URL username password nopage PageSize");


return;


  }


Jdbcscrollconn JSC = new Jdbcscrollconn ();


jsc.url = arg[0];


Jsc.username=arg[1];


jsc.password=arg[2];


int nopage = Integer.parseint (arg[3]);


int pagesize = Integer.parseint (arg[4]);


Jsc.demo (nopage,pagesize);


 }


}

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.