zoukankan      html  css  js  c++  java
  • JDBC05 ResultSet结果集

    ResultSet结果集

    -Statement执行SQL语句时返回ResultSet结果集

    -ResultSet提供的检索不同类型字段的方法,常用的有:

          getString():获得在数据库里是varchar,char等数据类型的对象

          getFloat():获得在数据库里是Floatr数据类型的对象

          getDate():获得在数据库里是Date数据类型的对象

          getBoolean():获得在数据库里是Boolean数据类型的对象

    -依序(先打开的后关闭)关闭使用对象及连接

    Connection conn=null;
            PreparedStatement ps=null;
            ResultSet rs=null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?&useSSL=false&serverTimezone=UTC"
                        ,"root","123456");
                
                String sql="select id,username,pwd from t_user where id>?";
                ps=conn.prepareStatement(sql);
                ps.setObject(1, 7);//取出id>7的记录
                rs=ps.executeQuery();
                
                while(rs.next()) {
                    System.out.println(rs.getInt(1)+"---"+rs.getString(2)+"---"+rs.getInt(3));//参数表示第几列
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(rs!=null)
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    if(ps!=null)
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    if(conn!=null)
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
            }

          

  • 相关阅读:
    包路径与沙盒路径
    iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
    从Swift桥接文件到Clang-LLVM
    人生•修养:知行合一
    First-class citizen
    阿里云部署SSL证书详解
    YourPHP笔记
    ThinkPHP函数详解:L方法
    robots书写说明:
    如何设置让网站禁止被爬虫收录?robots.txt
  • 原文地址:https://www.cnblogs.com/code-fun/p/11413689.html
Copyright © 2011-2022 走看看