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();
                }
                
            }

          

  • 相关阅读:
    29
    28
    27
    950. 按递增顺序显示卡牌
    25
    20190624
    409. 最长回文串
    636. 函数的独占时间
    LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
    LeetCode 942. 增减字符串匹配(DI String Match) 49
  • 原文地址:https://www.cnblogs.com/code-fun/p/11413689.html
Copyright © 2011-2022 走看看