3:21 am - Friday April 19, 2019

Statement COUNT() Example program in java Jdbc mysql database

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Create tableĀ  and insert the values in the table:


mysql>Create table EMP (name varchar(12),id int(5),salary int(10));

INSERT INTO EMP(name,id,salary) VALUES("Karthik",3,5000 );

INSERT INTO EMP(name,id,salary) VALUES("Tamil",4,2500 );

Query OK, 0 rows affected (0.06 sec)

Java code:


package myjava1;

import java.sql.*;

public class Count{

/**
* @param args
*/

// TODO Auto-generated method stub

public static void main(String[] args) throws Exception {

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Arun","root","root");
Statement st = conn.createStatement();

st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(name) FROM EMP");

while (rs.next()) {
String name = rs.getString(1);
System.out.println(name);

}

rs.close();
st.close();
conn.close();

}

}

Output:

Filed in: Z-A All Java Codes

Comments are closed.