Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » UltraLiteJ » 使用 UltraLiteJ » 开发 UltraLiteJ 应用程序 » 代码示例

 

示例:读取表

在本示例中,从 Connection 中获取 PreparedStatement 对象,从 PreparedStatement 中获取 ResultSet 对象。每次可获取后继行时,ResultSet 的 next 方法都会返回 true。当前行中的列值可随后从 ResultSet 对象中获取。

注意:
package ianywhere.ultralitej.demo;
import ianywhere.ultralitej.*;
/**
 * ReadSeq -- sample program to demonstrate reading a Database table
 * sequentially.
 */
public class ReadSeq
{
    /**
     * mainline for program.
     *
     * @param args command-line arguments
     *
     */
    public static void main
        ( String[] args )
    {
        try {
            Configuration config = DatabaseManager.createConfigurationFile( "Demo1.ulj" );
            Connection conn = DatabaseManager.connect( config );
            PreparedStatement stmt = conn.prepareStatement( "SELECT * FROM Employee ORDER BY number" );
            ResultSet cursor = stmt.executeQuery();
            for( ; cursor.next(); ) {
                /* Can't access columns by name because no meta data */
                int emp_no = cursor.getInt( 1 /* "number" */ );
                String last_name = cursor.getString( 2 /* "last_name" */ );
                String first_name = cursor.getString( 3 /* "first_name" */ );
                int age = cursor.getInt( 4 /* "age" */ );
                Demo.display( first_name + ' ' + last_name );
                Demo.display( "  empl. no = "
                            , Integer.toString( emp_no )
                            , "  age = "
                            , Integer.toString( age ) );
            }
            cursor.close();
            stmt.close();
            conn.release();
        } catch( ULjException exc ) {
            Demo.displayException( exc );
        }
    }
}