Hiten Hiten - 3 months ago 9
MySQL Question

how to get mysql query result in jsp page?

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection(url, "root", "");
// assuming your SQL Server's username is "username" // and password is "password"

Statement st = conn.createStatement();
String sql = "Select count(role) as totalEmployee from login where role = 'employee'";
ResultSet rs = st.executeQuery(sql);


I fired this query in mysql and I got the answer is 2.
So how can I get and display the result of this query in jsp page?

Answer

You can iterate result-set and get result in variable like :

  <%
    int totalEmployee = 0;
    if (rs != null && rs.next()) {
         totalEmployee = rs.getInt("totalEmployee");
    }
%>
Comments