gpt4 book ai didi

java - 如何在jsp页面中获取mysql查询结果?

转载 作者:行者123 更新时间:2023-11-29 11:19:50 25 4
gpt4 key购买 nike

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);

我在 mysql 中触发了这个查询,得到的答案是 2。那么如何在jsp页面中获取并显示该查询的结果呢?

最佳答案

您可以迭代结果集并在变量中获取结果,例如:

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

关于java - 如何在jsp页面中获取mysql查询结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com