zoukankan      html  css  js  c++  java
  • JDBC学习总结(三)

    1、ResultSet光标控制 
       在创建Statement或PreparedStatement时使用的是Connection的无参数createStatement()方法或preparedStatement()方法。这样获取到的Statement或PreparedStatement对象在执行SQL后所得到的ResultSet只能使用next()方法来逐条取得查询到的每条结果记录。
        在建立Statement对象时,是可以指定结果集类型的:
            · ResultSet.TYPE_FORWARD_ONLY:只能前进的结果集。即结果集ResultSet中的光标只能向前移动(只能调用它的next方法),每移动一次可获取一条结果记录。(默认)
            · ResultSet.TYPE_SCROLL_INSENSITIVE:可滚动的结果集,但其中的数据不受其他会话中用户对数据库更改的影响。
            · ResultSet.TYPE_SCROLL_SENSITIVE:可滚动的结果集,当其他会话中用户更改数据库表的数据时,这个结果集的数据也会同步改变。
        在指定了结果集的类型时,也需要同时指定结果集的并发策略,可以指定的类型如下:
            · ResultSet.CONCUR_READ_ONLY:只读的结果集。结果集中的数据是只读的,不能修改。(默认)
            · ResultSet.CONCUR_UPDATEBLE:可修改的结果集。结果集中的数据可以修改,修改后会同步到数据库中。
    package com.yyq.resultset;
    import com.yyq.factory.CloseFactory;
    import com.yyq.factory.ConnectionFactory;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    /**
     * Created by gao on 16-4-13.
     */
    public class ResultSetTest {
        public static void testPager(int firstResult, int maxResults) {
            Connection conn = ConnectionFactory.getConnection();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql = "select id,name,score,class from student";
            try {
                //可滚动的,只读的结果集
                pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                rs = pstmt.executeQuery();
                //光标移到最后一行
                rs.absolute(-1); //rs.last();
                //获取总记录数
                int count = rs.getRow();
                System.out.println("Recorder SUM:" + count);
                //光标移到指定行的前一行
                rs.absolute(firstResult - 1);
                for (int i = 0; i < maxResults; i++) {
                    if (rs.next()) {
                        System.out.println(rs.getInt(1) + ", " + rs.getString(2) + ", " + rs.getString(4) + ", " + rs.getInt(3));
                    } else {
                        break;
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                CloseFactory.close(pstmt, conn);
            }
        }
        public static void main(String[] args) {
            ResultSetTest.testPager(6, 12);
        }
    }
     
    2、ResultSetMetaData结果集元数据
        ResultSetMetaData表示的是所查询到的数据背后的数据描述——如表名称、列名称、列类型等。ResultSetMetaData提供了很多的方法,用来获取查询到的数据的描述数据。
    package com.yyq.resultset;
    import com.yyq.factory.CloseFactory;
    import com.yyq.factory.ConnectionFactory;
    import java.sql.*;
    /**
     * Created by gao on 16-4-13.
     */
    public class ResultSetMetaDataTest {
        public static void test() {
            Connection conn = ConnectionFactory.getConnection();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql = "select * from student";
            try {
                //可滚动的,只读的结果集
                pstmt = conn.prepareStatement(sql);
                rs = pstmt.executeQuery();
                //在结果集上获取元数据
                ResultSetMetaData rsmd = rs.getMetaData();
                int count = rsmd.getColumnCount(); //获取总列(字段)数
                System.out.println("ColumnCount:" + count);
                for (int i = 1; i <= count; i++) {
                    System.out.println("No:" + i);
                    System.out.println("Catalog Name:" + rsmd.getCatalogName(i));
                    System.out.println("Data Type Class:" + rsmd.getColumnClassName(i));
                    System.out.println("ColumnDisplaySize:" + rsmd.getColumnDisplaySize(i));
                    System.out.println("ColumnLabel:" + rsmd.getColumnLabel(i));
                    System.out.println("Column Type:" + rsmd.getColumnType(i));
                    System.out.println("Column Type Name:" + rsmd.getColumnTypeName(i));
                    System.out.println("Precision:" + rsmd.getPrecision(i));
                    System.out.println("Scale"+rsmd.getScale(i));
                    System.out.println("SchemaName:"+rsmd.getSchemaName(i));
                    System.out.println("Table Name:"+rsmd.getTableName(i));
                    System.out.println("IsAutoIncrement:"+rsmd.isAutoIncrement(i));
                    System.out.println("IsCurrency:"+rsmd.isCurrency(i));
                    System.out.println("IsNullable:"+rsmd.isNullable(i));
                    System.out.println("IsReadOnly:"+rsmd.isReadOnly(i));
                    System.out.println("isSearchable:"+rsmd.isSearchable(i));
                    System.out.println();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                CloseFactory.close(pstmt, conn);
            }
        }
        public static void main(String[] args) {
            ResultSetMetaDataTest.test();
        }
    }
  • 相关阅读:
    POJ 3211 Washing Clothes
    MySQL 优化Limit分页
    signed 与 unsigned 有符号和无符号数
    C/C++ 变量的初始化
    C/C++ 变量的初始化
    返回值的判断
    返回值的判断
    《涅槃经》的研读
    《涅槃经》的研读
    Opencv Surf算子特征提取与最优匹配
  • 原文地址:https://www.cnblogs.com/yangyquin/p/5387331.html
Copyright © 2011-2022 走看看