Question: A method getColumnCount.

Ads

 
 
 

Share on Google+Share on Google+

Clark
A method getColumnCount.
2 Answer(s)      6 years and 6 months ago
Posted in : JDBC

There is a method getColumnCount in the JDBC API. Is there a similar method to find the number of rows in a result set?

Ads
View Answers

November 13, 2010 at 6:04 PM


Hi friends,

Create an instance of a JDBC driver or load JDBC drivers through jdbc.drivers Register a driver Specify a database Open a database connection Submit a query Receive results Process results

Thanks.

Hi friends, No, but it is easy to find the number of rows. If you are using a scrollable result set, rs, you can call the methods rs.last and then rs.getRow to find out how many rows rs has. If the result is not scrollable, you can either count the rows by iterating through the result set or get the number of rows by submitting a query with a COUNT column in the SELECT clause.

Hi friends, No, but it is esay to find the number of rows. If you are using a scrollable result set , you can call the method last() and then getRow() to find out how many rows in result set. It result set is not scrollable, you can either count the rows by iterating through the result set or get the number of rows by submitting a query with a COUNT column in the SELECT clause.

======Example=====

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;

public class CountRows { public static void main(String[] args) { try { String mysqlDriver = "com.mysql.jdbc.Driver"; Class.forName(mysqlDriver).newInstance(); String connectionString = "jdbc:mysql://192.168.10.13:3306/studentinformation"; String username = "root"; String password = "root"; Connection con = null; Statement stmt = null; ResultSet rs = null; con = DriverManager.getConnection(connectionString, username, password); stmt = con.createStatement(ResultSet.TYPESCROLLSENSITIVE, ResultSet.CONCUR_UPDATABLE);

        String sqlString = "Select * from s_infor;";
        rs = stmt.executeQuery(sqlString);
        System.out.println("---------Student Information----------");
        while (rs.next()) {
            int roll = rs.getInt("s_Roll");
            String Name = rs.getString("s_Name");
            String Address = rs.getString("s_Address");
            System.out.println(roll + "  " + Name + "  " + Address);
        }
        rs.last();
        int numOfRows = rs.getRow();
        System.out.println("Number of rows in result set : " + numOfRows);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

}

======OutPut=====

---------Student Information---------- 1 Rohit Barabanki 3 Bharat Bareilly 4 Gyan Bareilly Number of rows in result set : 3

Thanks.



November 13, 2010 at 6:07 PM


Hi friends,

Create an instance of a JDBC driver or load JDBC drivers through jdbc.drivers Register a driver Specify a database Open a database connection Submit a query Receive results Process results

Thanks.

Hi friends, No, but it is esay to find the number of rows. If you are using a scrollable result set , you can call the method last() and then getRow() to find out how many rows in result set. It result set is not scrollable, you can either count the rows by iterating through the result set or get the number of rows by submitting a query with a COUNT column in the SELECT clause.

======Example=====

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class CountRows {
public static void main(String[] args) {
    try {
        String mysqlDriver = "com.mysql.jdbc.Driver";
        Class.forName(mysqlDriver).newInstance();
        String connectionString = "jdbc:mysql://192.168.10.13:3306/studentinformation";
        String username = "root";
        String password = "root";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        con = DriverManager.getConnection(connectionString, username,
                password);
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        String sqlString = "Select * from s_infor;";
        rs = stmt.executeQuery(sqlString);
        System.out.println("---------Student Information----------");
        while (rs.next()) {
            int roll = rs.getInt("s_Roll");
            String Name = rs.getString("s_Name");
            String Address = rs.getString("s_Address");
            System.out.println(roll + "  " + Name + "  " + Address);
        }
        rs.last();
        int numOfRows = rs.getRow();
        System.out.println("Number of rows in result set : " + numOfRows);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

}

 ======OutPut=====

---------Student Information----------
1  Rohit  Barabanki
3  Bharat  Bareilly
4  Gyan  Bareilly
Number of rows in result set : 3

Thanks.


Ads









Related Tutorials/Questions & Answers:
A method getColumnCount.
A method getColumnCount.  There is a method getColumnCount in the JDBC API. Is there a similar method to find the number of rows in a result set
method
method   how and where, we can define methods ? can u explain me with full programme and using comments
Advertisements
method
method  can you tell me how to write an abstract method called ucapan() for B2 class class A2{ void hello(){ system.out.println("hello from A2"); }} class B2 extends A2{ void hello(){ system.out.println("hello from B2
method inside the method??
method inside the method??  can't we declare a method inside a method in java?? for eg: public class One { public static void main(String[] args) { One obj=new One(); One.add(); private static void add
Method
Method       In this section, we will explore the concept of method in the reference of object oriented... and behaviors are defined by methods. Method : An brief introduction Methods
get method
get method   how to use get method: secure method is post method and most of use post method why use a get method
method question
method question  How do I figure out the difference in a method heading, a method body, and a method definition
Method Overloading
Method Overloading  In java can method be overloaded in different class
Method Overloading
Method Overloading  In java can method be overloaded in different class
abstract method
abstract method  is final method is in abstract class
method name
method name  Is there any method which is equivalent to c++'s delay(int) function
gc() method
gc() method  what is difference between java.lang.System class gc() method and java.lang.Runtime class gc() method
Static method
Static method  what is a static method?   Have a look at the following link: Java Static Method
main method
main method  Why is the java main method static
abstract method
abstract method  Can a concrete class have an abstract method
Checkbox method
Checkbox method  what is the method to check if the checkbox is checked
main method
main method  What is the argument type of a program's main() method
Method overriding
Method overriding  How compiler decide parent class'method or child class's method to call? What is the meaning of this: Parent obj=new child_obj
java method
java method  can we declare a method in java like this static... book. i don't understand the static { } plz help me. what kind of method is it, what it do, what are the advantages of this kind of method? Answer me soon
_jspService() method
_jspService() method  Why is jspService() method starting with an '' while other life cycle methods do not?   jspService() method... don't override _jspService() method in any JSP page
init method
init method  why init method used in servlet?   The init() method is called only once by the servlet container throughout the life of a servlet. By this init() method the servlet get to know that it has been placed
finalize() method
finalize() method  Can an object's finalize() method be invoked while it is reachable?  An object?s finalize() method cannot be invoked... finalize() method may be invoked by other objects
Method overriding
Method overriding  can a method declared in one package be over ridden in a different package?   A subclass in a different package can only override the non-final methods declared public or protected
method overloading
method overloading  public void test(int a){} public void test(long a){} i will call some x.test(125) which method is called pls tell me
recursive method
recursive method  Write a recursive method to add the first n terms of the series 1 + (1/2) - (1/3) + (1/4) - (1/5
static method
static method  Give some absolute examples and definition for static method also
method overloading
method overloading   public void test(int a); pulic void test(long a);which sutiation x.long is called
main method
in paranthesis denote?   Hi Friend, public-It indicates that the main() method can be called by any object. static-It indicates that the main() method is a class method. void- It indicates that the main() method has no return value
method overloading
method overloading  public void test(int a) public void test(long a) public void test(object a) i will call some x.test(1258448); which method is called if it is called first one why it not call the third one
method overloading
method overloading  what is output of this program & why class Test { public void display(String msg) { system.out.println(msg); } public void display(Object msg) { system.out.println(msg
main method
. Compiler gives error that the main method is already defined in the class
Tutorials   
Java Spring Hibernate Struts Training how to retrieve text and images from mysql database and show on html page using jsp servlet How to install Neo4j on Ubuntu 15.10? please help me in these prog MYSQL - mysql copy table to another table example by creating new table solution for mapping hibernate and pojos from net beans through database Connect GWT application with Oracle inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store This code have created jTable that connected with a database.every cell of last column have a button.But this button doesn't work properly.Where is the error java program Java Database Program jdbc Database connection class in a live project ..?? What is JDBC architecture? Unable to Create DSN for excel to ms access importing data and edit that data Database Scrolling Buttons how to use foreign key in java How to delete content of a log file from linux terminal? JSP-Oracle connectivity Student Management System Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same crud operations with foreign key constraint. I really need help with this assignment question Please help me out Please Backup Failed Mysqldump Got Errno 28 On Write Upgrade MySQL 5.1 to 5.5 in cpanel mysqldump all databases com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist persistence xml class element persistence.xml for MySQL MySQL JDBC Driver pom dependency code Blob column jdk6 and oracle 8i connexion oracle 8i et jdk6 java [ cannot retrive date from sql ] why?? Order Entry System How to threow timeout exception when DB is not responding How to threow timeout exception when DB is not responding This is the employee form to add data to mysql database but I got error of NullPointerException, Is this write code please give me feedback as soon as possible make a method which takes user name & pwd as parameter & validate them against the database. If the corresponding user exist return true otherwise false. prabhakar validating credentials and displaying error message in login .jsp if not valid JDBC jdbc jdbc code register servlet vibhu connectiondetails from vibhu

Ads

 
Advertisement null

Ads