我正在尝试在jsp页面中检索mysql数据。我面临的问题是检索到的值没有被打印出来。但是,根据表格中的行数正确打印你好
代码是
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%@page import="java.sql.*,javax.servlet.*,javax.sql.*" %>
<%
//Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//Open a connection
Connection conn=null;
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/quiz","root","root");
// Execute SQL query
Statement stmt = conn.createStatement();
String sql="select Question_Id,Question from question_master;";
out.println(sql);
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
out.println("hello");
int id=rs.getInt("Question_Id");
String ques=rs.getString("Question");
System.out.println("ID: " + id + "<br>");
System.out.println("Question " + ques+ "<br>");
}
%>
</body>
</html>
答案 0 :(得分:2)
问题是你在写
System.out.println("ID: " + id + "<br>");
System.out.println("Question " + ques+ "<br>");
将在服务器控制台中打印
你必须写
out.println("ID: " + id + "<br>");
out.println("Question " + ques+ "<br>");
在jsp页面中打印