zoukankan      html  css  js  c++  java
  • 使用ResultSet结果集查询数据

    直接上下代码:

     1 package com.learn.jdbc.chap05;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 import java.sql.ResultSet;
     6 import java.util.ArrayList;
     7 import java.util.List;
     8 
     9 import com.learn.jdbc.model.Album;
    10 import com.learn.jdbc.util.DbUtil;
    11 /**
    12  * 使用ResultSet结果集(数据查询)
    13  * @author Administrator
    14  *
    15  */
    16 public class Demo1 {
    17     
    18     private static DbUtil dbUtil=new DbUtil();
    19     
    20     private static void listAlbum() throws Exception{
    21         Connection con = dbUtil.getCon();
    22         String sql="select * from sp_album";
    23         PreparedStatement pstmt = con.prepareStatement(sql);
    24         ResultSet rs = pstmt.executeQuery(); // 返回结果集 ResultSet
    25         while(rs.next()){
    26             // 下面的写法 在工作中 很少用
    27             int id=rs.getInt(1);
    28             String name=rs.getString(2);
    29             int uid=rs.getInt(3);
    30             long time=rs.getLong(4);
    31             System.out.println("编号:"+id+",名称:"+name+",会员id:"+uid+",添加时间:"+time);
    32             System.out.println("-----------------------");
    33         }
    34         
    35     }
    36     
    37     private static void listAlbum2() throws Exception{
    38         Connection con = dbUtil.getCon();
    39         String sql="select * from sp_album";
    40         PreparedStatement pstmt = con.prepareStatement(sql);
    41         ResultSet rs = pstmt.executeQuery(); // 返回结果集 ResultSet
    42         while(rs.next()){
    43             // 下面的写法 在工作中 经常这么写
    44             int id=rs.getInt("id");
    45             String name=rs.getString("name");
    46             int uid=rs.getInt("uid");
    47             long time=rs.getLong("add_time");
    48             System.out.println("编号:"+id+",名称:"+name+",会员id:"+uid+",添加时间:"+time);
    49             System.out.println("-----------------------");
    50         }
    51         
    52     }
    53     
    54     private static List<Album> listAlbum3() throws Exception{
    55         List<Album> albumList = new ArrayList<Album>();
    56         
    57         Connection con = dbUtil.getCon();
    58         String sql="select * from sp_album";
    59         PreparedStatement pstmt = con.prepareStatement(sql);
    60         ResultSet rs = pstmt.executeQuery(); // 返回结果集 ResultSet
    61         while(rs.next()){
    62             // 下面的写法 在工作中 经常这么写
    63             int id=rs.getInt("id");
    64             String name=rs.getString("name");
    65             int uid=rs.getInt("uid");
    66             long time=rs.getLong("add_time");
    67             
    68             Album ab=new Album(id, name, uid, time);
    69             albumList.add(ab);
    70         }
    71         
    72         return albumList;
    73     }
    74     
    75     public static void main(String[] args) throws Exception {
    76         /*listAlbum();
    77         System.out.println("=============================================");
    78         listAlbum2();*/
    79         List<Album> abInfo = listAlbum3();
    80         for(Album abm:abInfo){ // 此处的abm默认输出是调用父类的toString方法,要输出自定义信息,需要重写toString方法
    81             System.out.println(abm);
    82             //System.out.println(abm.getName());
    83         }
    84     }
    85 }
  • 相关阅读:
    Node.js安装及环境配置(windows)
    table
    检测浏览器
    ickeck插件
    全国三级联动
    css3-calc用法
    jQuery Portamento 滑动定位
    canvas版《俄罗斯方块》
    canvas入门级小游戏《开关灯》思路讲解
    css3 matrix 2D矩阵和canvas transform 2D矩阵
  • 原文地址:https://www.cnblogs.com/eaglezb/p/6055317.html
Copyright © 2011-2022 走看看