JDBC Resultset 结果集范例

jdbc resultset 结果集范例

jdbc resultset 通过移动光标的方法浏览结果集。

 

1. resultset 移动光标的方法

编号 方法 描述
1 public void beforefirst() throws sqlexception 将光标移动到第一行之前
2 public void afterlast() throws sqlexception 将光标移动到最后一行之后。
3 public boolean first() throws sqlexception 将光标移动到第一行。
4 public void last() throws sqlexception 将光标移动到最后一行。
5 public boolean absolute(int row) throws sqlexception 将光标移动到指定的行。
6 public boolean relative(int row) throws sqlexception 从当前指向的位置,将光标向前或向后移动给定行数。
7 public boolean previous() throws sqlexception 将光标移动到上一行。 如果上一行关闭结果集,此方法返回false。
8 public boolean next() throws sqlexception 将光标移动到下一行。 如果结果集中没有更多行,则此方法返回false。
9 public int getrow() throws sqlexception 返回光标指向的行号。
10 public void movetoinsertrow() throws sqlexception 将光标移动到结果集中的特殊行,该行可用于将新行插入数据库。当前光标位置被记住。
11 public void movetocurrentrow() throws sqlexception 如果光标当前位于插入行,则将光标移回当前行; 否则,此方法什么也不做

 

2. resultset 浏览结果集范例

//step 1. import required packages
import java.sql.*;

public class navigatingresultset {
    // jdbc driver name and database url
    static final string jdbc_driver = "com.mysql.jdbc.driver";  
    static final string db_url = "jdbc:mysql://localhost/emp";

    //  database credentials
    static final string user = "root";
    static final string pass = "123456";

    public static void main(string[] args) {
        connection conn = null;
        statement stmt = null;
        try{
            //step 2: register jdbc driver
            class.forname("com.mysql.jdbc.driver");

            //step 3: open a connection
            system.out.println("connecting to database...");
            conn = drivermanager.getconnection(db_url,user,pass);

            //step 4: execute a query to create statment with
            // required arguments for rs example.
            system.out.println("creating statement...");
            stmt = conn.createstatement(
                                resultset.type_scroll_insensitive,
                                resultset.concur_read_only);
            string sql;
            sql = "select id, first, last, age from employees";
            resultset rs = stmt.executequery(sql);

            // move cursor to the last row.
            system.out.println("moving cursor to the last...");
            rs.last();

            //step 5: extract data from result set
            system.out.println("displaying record...");
            //retrieve by column name
            int id  = rs.getint("id");
            int age = rs.getint("age");
            string first = rs.getstring("first");
            string last = rs.getstring("last");

            //display values
            system.out.print("id: " + id);
            system.out.print(", age: " + age);
            system.out.print(", first: " + first);
            system.out.println(", last: " + last);

            // move cursor to the first row.
            system.out.println("moving cursor to the first row...");
            rs.first();

            //step 6: extract data from result set
            system.out.println("displaying record...");
            //retrieve by column name
            id  = rs.getint("id");
            age = rs.getint("age");
            first = rs.getstring("first");
            last = rs.getstring("last");

            //display values
            system.out.print("id: " + id);
            system.out.print(", age: " + age);
            system.out.print(", first: " + first);
            system.out.println(", last: " + last);
            // move cursor to the first row.

            system.out.println("moving cursor to the next row...");
            rs.next();

            //step 7: extract data from result set
            system.out.println("displaying record...");
            id  = rs.getint("id");
            age = rs.getint("age");
            first = rs.getstring("first");
            last = rs.getstring("last");

            //display values
            system.out.print("id: " + id);
            system.out.print(", age: " + age);
            system.out.print(", first: " + first);
            system.out.println(", last: " + last);

            //step 8: clean-up environment
            rs.close();
            stmt.close();
            conn.close();
        }catch(sqlexception se){
            //handle errors for jdbc
            se.printstacktrace();
        }catch(exception e){
            //handle errors for class.forname
            e.printstacktrace();
        }finally{
            //finally block used to close resources
            try{
                if(stmt!=null)
                stmt.close();
            }catch(sqlexception se2){
            }// nothing we can do
            try{
                if(conn!=null)
                conn.close();
            }catch(sqlexception se){
                se.printstacktrace();
            }//end finally try
        }//end try
        system.out.println("goodbye!");
    }//end main
}//end jdbcexample

 

3. 编译运行

f:\worksp\jdbc>javac -djava.ext.dirs=f:\worksp\jdbc\libs navigatingresultset.java

f:\worksp\jdbc>java -djava.ext.dirs=f:\worksp\jdbc\libs navigatingresultset

运行上面代码,得到以下结果:

相关文章