Hiten
Hiten

Reputation: 877

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?

Upvotes: 0

Views: 2613

Answers (2)

Birhan Nega
Birhan Nega

Reputation: 681

You can also display result-set and get result in variable like :

  <%

    while( rs.next()) {
         String= rs.getstring(1);
    }
%>

Upvotes: 0

Chandan Rajput
Chandan Rajput

Reputation: 441

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

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

Upvotes: 1

Related Questions