码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA学习笔记(五十五)- 元数据 MetaData和可滚动的结果集

时间:2015-04-05 10:37:15      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:元数据   可滚动的结果集   pstmt   conn   

元数据 Meta Data

/*
 * 元数据 Meta Data
 */
public class Test03 {
    Connection conn = null;
    PreparedStatement pstmt=null;
    ResultSet rs = null;

    public void test(){
        conn=DBUtil.getConnection();
        try {
            DatabaseMetaData dbmd=conn.getMetaData(); 
            //获取数据库元数据
            System.out.println(dbmd.getDatabaseProductName());
            System.out.println(dbmd.getDatabaseProductVersion());
            System.out.println(dbmd.getDriverName());
            System.out.println(dbmd.getDriverVersion());
            pstmt=conn.prepareStatement("select username,password from user");
            rs=pstmt.executeQuery();
            //获取结果集元数据
            ResultSetMetaData rsmd=rs.getMetaData();
            System.out.println(rsmd.getColumnCount());
            System.out.println(rsmd.getColumnName(1));
            System.out.println(rsmd.getColumnName(2));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        new Test03().test();
    }
}

可滚动的结果集

/*
 * 可滚动的结果集
 */
public class Test04 {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    // 读取到指定用户时插入一条记录
    public void test01() {
        String sql = "select * from user";
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            rs = pstmt.executeQuery(); // 可滚动结果集,可以更新表
            while (rs.next()) {
                String name = rs.getString("username");
                if (name.equals("tom")) {// 判断当前读取的用户
                    // 将光标移动到插入行,插入行实际上就是一个缓冲区,用来准备数据
                    rs.moveToInsertRow();
                    rs.updateString("username", "alex");
                    rs.updateString("password", "999");
                    // 提交缓冲区中的数据
                    rs.insertRow();
                    System.out.println("插入用户成功!");
                    // 将光标移动到当前行,即插入记录前的那条记录
                    rs.moveToCurrentRow();
                }
                System.out.println(rs.getInt(1) + "," + rs.getString(2) + ","
                        + rs.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, pstmt, conn);
        }
    }

    // 删除指定用户
    public void test02() {
        String sql = "select * from user";
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String name = rs.getString("username");
                if (name.equals("bbb")) {
                    rs.deleteRow();// 删除当前行
                    //rs.next();// 删除后将光标下移
                    System.out.println("删除用户成功");
                }
                System.out.println(rs.getInt(1) + "," + rs.getString(2) + ","
                        + rs.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, pstmt, conn);

        }
    }

    //移动游标
    public void test03() {
        String sql = "select * from user";
        try {
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String name = rs.getString("username");
                if (name.equals("s001")) {
                    rs.relative(3);
                }
                System.out.println(rs.getInt(1) + "," + rs.getString(2) + ","
                        + rs.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, pstmt, conn);

        }
    }

    public static void main(String[] args) {
        new Test04().test03();
    }
}

JAVA学习笔记(五十五)- 元数据 MetaData和可滚动的结果集

标签:元数据   可滚动的结果集   pstmt   conn   

原文地址:http://blog.csdn.net/wangzi11322/article/details/44885757

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!