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

          

  • 相关阅读:
    鼠标悬停改变图片方法
    margin IE6中加倍问题
    js菜单效果
    杂谈
    常见的服务器端口号
    .NET 配置文件设置数据库连接属性
    ASP.NET 利用 Microsoft.Office.Interop.Excel 版本导出Excel数据
    DataGridView 绑定List时 属性不显示的解决方法
    C# 基本文件操作
    构建可克隆对象(ICloneable)
  • 原文地址:https://www.cnblogs.com/code-fun/p/11413689.html
Copyright © 2011-2022 走看看