Abstract: At present, the database applications are very common in the application development process, often related to access to the database. Java using JDBC technology, access to the database. The operation of the database query is the more frequently a kind of operation, the returned results can sometimes be a lot of records in the result set, users browse and processing inconvenient, often used in paging processing.
Keywords: JDBC database; driver; result set CLC number: TP311 Document: A Article ID :1009-3044 (2007) 06-11511-01
1 JDBC technology, JDBC (Java Database Connectivity) is the Sun provides a set of database programming interface API functions, from the Java language classes and interfaces. JDBC for database developers with a standard API, using the Java programming language and JDBC conjunction with the preparation of applications, without regard to the preparation of different platforms for different applications. Truly "Write Once, Run Everywhere!".
2 JDBC programming database access using JDBC technology, when, Java application through JDBC API and the JDBC driver manager to communicate between, JDBC driver manager and the final in two ways to communicate with the database: First, the use of JDBC - ODBC bridge driver, an indirect way, and the other is to use the JDBC driver direct way.
The following JDBC driver to direct way to illustrate how Java language and SQL Server2000 connected to the whole process of JDBC programming:
(1) load the driver Class.forName ( "com.microsoft.jdbc.sqlserver.SQLServerDriver");
(2) through DriverManager to get a database connection handle private static String url = "jdbc: microsoft: sqlserver: / / localhost: 1433; databasename = xxxx";
Connection con = DriverManager.getConnection (url, user, password);
(3) by connecting the handle to bind to execute the statement Statement stmt = con.createStatement ();
(4) to receive results of the implementation ResultSet rs = stmt.executeQuery (sql);
Or ResultSet rs = stmt.executeUpdate (sql);
(5) deal with the results called get XXX () method to obtain the result value.
(6) Close the database connection rs.close ();
3 JDBC programming page is displayed in the result set ResultSet class provides a set of data, paging processing.
3.1 Technical points (1) createStatement () method:
public Statement createStatement ()
Get the default connect statement, there is no pointer to operate public Statement createStatement (int resultSetType, int resultSetConcurrency)
resultSetType decision result set type of a rolling basis, and its value as: ResultSet.TYPE_FORWARD_ONLY, Result.TYPE_SCROLL_INSENSITIVE and ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency deciding whether to update the database with the result set. Its value is: Result.CONCUR_READ_ONLY, Result.CONCUR_UPDATETABLE
(2) ResultSetMetaData result set meta-object classes result set meta-data by using the getMetaData () to get the result set meta-object. ResultSetMetaData result set meta-object class methods:
getColumnName (int index)
getColumnTypeName (int index)
3.2 The establishment of a database in SQL Server 2000 creates a directory named book of the database, book database, established under an information table book bookInfo, the table structure: bookInfo (no, bookname, author, price, public)
3.3 Java source program in order to make a certain versatility and flexibility, will be displayed in number and page size (the number of records per page) from the command line input. The command-line arguments arg  on behalf of pageno, arguments arg  on behalf of pagesize. JDBCScrollDisplay.java source code is as follows:
import java.sql .*;
public class JDBCScrollDisplay (
private static String url = "jdbc: microsoft: sqlserver: / / localhost: 1433; databasename = book";
private static String username = "sa";
private static String password = "sa";
public Connection conn () (
Class.forName ( "com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection (url, username, password);
catch (SQLException e1) (
System.out.println ( "can't connection db:" + e1);
catch (Exception e2) (
System.out.println ( "Failed to load JDBC driver.");
public void query (Connection con, String sql, int pageNo, int pageSize) (
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 rowCount; / / Record the total number of int pageCount; / / total number of pages Statement stmt = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery (sql); / / execute the query ResultSetMetaData rmeta = rs.getMetaData ();// get the number of data fields int numColumns = rmeta.getColumnCount ();
rowCount = rs.getRow ();
pageCount = (rowCount + pageSize-1) / pageSize; / / calculating the total number of pages if (pageNo> pageCount) pageNo = pageCount; / / adjust the page to be displayed if (pageCount> 0) (
rs.absolute ((pageNo-1) * pageSize +1); / / pointer to locate the record to be displayed on the pages of the first record int i = 0;
while (i) (
for (int j = 0; j <numColumns; j + +)
String sTemp = rs.getString (j +1);
System.out.print (sTemp + "");
i + +;
catch (Exception e) (
System.out.println ( "query error:" + e);
public void demo (int pageNo, int pageSize) (
JDBCScrollDisplay jd = new JDBCScrollDisplay ();
Connection con = jd.conn ();
String sql = "select * from bookInfo order by no";
jd.query (con, sql, pageNo, pageSize);
catch (SQLException se) (System.out.println (se);)
catch (Exception e) (System.out.println (e);))
public static void main (String  arg) (
JDBCScrollDisplay jsd = new JDBCScrollDisplay ();
int pageno = Integer.parseInt (arg );
int pagesize = Integer.parseInt (arg );
jsd.demo (pageno, pagesize);))
4 Conclusion JDBC is Java, a standard SQL database access interface, which for database application developers, database front tool developers to provide a standard application programming interfaces. Developers simply use JDBC provides classes, call these class methods can access the database, without concern to what is used in the specific database system. In the actual use often when we try to provide scrollable result set, just before the use of paging data declaration is a movable SQL statements. The cases of any application can do any operation on the database, based on the re-paging display.
 DU, Guan Payson. Java Application Programming 100 cases of [M]. China Railway Publishing House ,2004,4.171-189.
 Yu Dong Jun, Yang Jingyu, Li Qian-mu, the Kingdom-wide. Java program design and application development [M]. Tsinghua University Press ,2005.3.216-231.
In this article are involved in graphics, comments, formulas etc. Please refer to the PDF format to read the original.