Question: ExecuteScalar in java
I am developing web application in java using JSP and servlets. I would like to know one thing that is there any way to extract one value from database just like executescalar in .NET.
Ex. select count(*) from userdetails i want to get count without using resultset... is it possible or not....
You can use the following code:
<[email protected] import="java.sql.*"%> <% Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement stmt=con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM employee"); rs.next(); int count = rs.getInt(1); out.println("Number of rows: "+count); %>