X-Received: by 10.107.184.195 with SMTP id i186mr17996901iof.24.1455556529505; Mon, 15 Feb 2016 09:15:29 -0800 (PST) X-Received: by 10.50.142.103 with SMTP id rv7mr176109igb.4.1455556529491; Mon, 15 Feb 2016 09:15:29 -0800 (PST) Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.glorb.com!ok5no2101012igc.0!news-out.google.com!l1ni14437igd.0!nntp.google.com!hb3no5140279igb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.databases Date: Mon, 15 Feb 2016 09:15:28 -0800 (PST) In-Reply-To: <3AA7BB0C.3E29B600@sea.ericsson.se> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=195.235.202.189; posting-account=8RK-JAoAAACR8ShTMXBiMksBjSJGGN1T NNTP-Posting-Host: 195.235.202.189 References: <3AA7BB0C.3E29B600@sea.ericsson.se> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <66367974-130d-4a9d-a06a-db1611553af2@googlegroups.com> Subject: Re: Getting the size of a result set From: jagilberte@gmail.com Injection-Date: Mon, 15 Feb 2016 17:15:29 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.databases:725 El jueves, 8 de marzo de 2001, 18:02:04 (UTC+1), Valentin Todorov escribi= =F3: > Try this: >=20 > long getCount(Connection con, String table){ > Statement stmt =3D null; > ResultSet rs =3D null; > int count =3D 0; > try{ > stmt =3D con.createStatement(); > rs =3D stmt.executeQuery("SELECT COUNT(*) FROM "+table); > if(rs.next()) > count =3D rs.getInt(1); > }catch(SQLException e) { > // error handling > }finally{ > // close rs &stmt > } > } >=20 > HTH > Valentin >=20 >=20 > Jon Skeet schrieb: >=20 > > Hi everyone, > > > > I'm not new to Java, but I'm extremely new to JDBC, SQL, and the whole > > database "thang". I've been tasked with writing a class to implement ou= r > > own data interface, but accessing a JDBC database. One of the methods I > > have to implement is (on a ResultSet-type object) getCount() which > > should return the number of rows in the results. > > > > I've been trying to figure out a way of doing this using JDBC, and I > > haven't got very far yet. As this is only a prototype at the moment, I'= m > > probably okay to use any standard JDBC extensions etc if necessary, > > although obviously I'd like to keep it as simple as possible. I'd hoped > > that ResultSetMetaData would have something in it, or that I could > > specify an OUT parameter in a PreparedStatement which could contain the > > count. (I realise I can use OUT parameters with a CallableStatement, bu= t > > I can't force our users to add appropriate CallableStatements, > > unfortunately.) I can get the count if I don't specify anything else, > > but I really need the rest of the data as well... > > > > I realise this may be an impossible mission, but I thought it would be > > worth asking. > > > > -- > > Jon Skeet - skeet@pobox.com > > http://www.pobox.com/~skeet Hi, if you want to make a count of rows is better always use COUNT(1) than COUNT(*). Because the * implies project all the atributes and the 1 avoid p= roject the attributes.