How to get the row count in JDBC?
For Explanation Watch Video:
Code::
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class Test {
public static void main(String[] args)throws Exception{
//generate connection
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger");
//sql Query
String query = "select count(*) from stu";
//prparedStatement Object
PreparedStatement ps = con.prepareStatement(query);
//resultSet obj
ResultSet rs = ps.executeQuery();
rs.next();
//fetch the data
int count = rs.getInt(1);
System.out.println("The No. Of Records : "+count);
//close the connection
con.close();
}
}
Comments
Post a Comment