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