Querying data using the resultset result set

Source: Internet
Author: User
Tags add time

Direct and Lower code:

1  Packagecom.learn.jdbc.chap05;2 3 Importjava.sql.Connection;4 Importjava.sql.PreparedStatement;5 ImportJava.sql.ResultSet;6 Importjava.util.ArrayList;7 Importjava.util.List;8 9 ImportCom.learn.jdbc.model.Album;Ten ImportCom.learn.jdbc.util.DbUtil; One /** A * Use resultset result set (data query) -  * @authorAdministrator -  * the  */ -  Public classDemo1 { -      -     Private StaticDbutil dbutil=Newdbutil (); +      -     Private Static voidListalbum ()throwsexception{ +Connection con =Dbutil.getcon (); AString sql= "SELECT * from Sp_album"; atPreparedStatement pstmt =con.preparestatement (SQL); -ResultSet rs = Pstmt.executequery ();//returns the result set ResultSet -          while(Rs.next ()) { -             //The following wording is seldom used in work -             intId=rs.getint (1); -String name=rs.getstring (2); in             intUid=rs.getint (3); -             LongTime=rs.getlong (4); toSystem.out.println ("No.:" +id+ ", Name:" +name+ ", Member ID:" +uid+ ", add Time:" +Time ); +System.out.println ("-----------------------"); -         } the          *     } $     Panax Notoginseng     Private Static voidListAlbum2 ()throwsexception{ -Connection con =Dbutil.getcon (); theString sql= "SELECT * from Sp_album"; +PreparedStatement pstmt =con.preparestatement (SQL); AResultSet rs = Pstmt.executequery ();//returns the result set ResultSet the          while(Rs.next ()) { +             //The following writing is often done at work . -             intId=rs.getint ("id"); $String name=rs.getstring ("name"); $             intUid=rs.getint ("UID"); -             LongTime=rs.getlong ("Add_time"); -System.out.println ("No.:" +id+ ", Name:" +name+ ", Member ID:" +uid+ ", add Time:" +Time ); theSystem.out.println ("-----------------------"); -         }Wuyi          the     } -      Wu     Private StaticList<album> listAlbum3 ()throwsexception{ -List<album> albumlist =NewArraylist<album>(); About          $Connection con =Dbutil.getcon (); -String sql= "SELECT * from Sp_album"; -PreparedStatement pstmt =con.preparestatement (SQL); -ResultSet rs = Pstmt.executequery ();//returns the result set ResultSet A          while(Rs.next ()) { +             //The following writing is often done at work . the             intId=rs.getint ("id"); -String name=rs.getstring ("name"); $             intUid=rs.getint ("UID"); the             LongTime=rs.getlong ("Add_time"); the              theAlbum ab=NewAlbum (ID, name, uid, time); the Albumlist.add (AB); -         } in          the         returnalbumlist; the     } About      the      Public Static voidMain (string[] args)throwsException { the         /*listalbum (); the System.out.println ("============================================="); + listAlbum2 ();*/ -List<album> Abinfo =listAlbum3 (); the          for(Album abm:abinfo) {//here the ABM default output is called the parent class's ToString method, to output the custom information, needs to override the ToString methodBayi System.out.println (ABM); the             //System.out.println (Abm.getname ()); the         } -     } -}

Querying data using the resultset result set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.