以下jsp代码在Web浏览器上仅返回一个空白屏幕。该怎么办?
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GAURAV GOSWAMI</title>
<%
try
{
Class.forName("java.sql.DriverManager");
Connection con =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz","root","");
Statement stmt = con.createStatement();
String query = "select * from qa";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
%>
<p><br><%rs.getString(0);%></p>
<p><br><%rs.getString(1);%></p>
<p><br><%rs.getString(2);%></p>
<p><br><%rs.getString(3);%></p>
<p><br><%rs.getString(4);%></p>
<%
}
}
catch(Exception e)
{
%>
<br><%e.getMessage();%>
<%
}
%>
</head>
</html>
答案 0 :(得分:2)
您正在头部分中编写正文。
在主体部分中移动代码,如下所示:
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>GAURAV GOSWAMI</title>
</head> <!-- Add this line -->
<body> <!-- Add this line -->
<%
try
{
Class.forName("java.sql.DriverManager");
Connection con =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/quiz","root","");
Statement stmt = con.createStatement();
String query = "select * from qa";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
%>
<p><br><%rs.getString(0);%></p>
<p><br><%rs.getString(1);%></p>
<p><br><%rs.getString(2);%></p>
<p><br><%rs.getString(3);%></p>
<p><br><%rs.getString(4);%></p>
<%
}
}
catch(Exception e)
{
%>
<br><%e.getMessage();%>
<%
}
%>
</body> <!-- Add this line -->
<!-- **** </head> *** Remove this line -->
</html>
我希望它能帮助您,再见。
PS:我已经在<!-- Add this line -->
之类的注释中标记了要更改的行。
答案 1 :(得分:1)
除了亚历山德罗的答案(不要将内容写到头部)之外,您编写的jdbc驱动程序也不正确
您需要改用com.mysql.jdbc.Driver
。
所以改变
Class.forName("java.sql.DriverManager");
到
Class.forName("com.mysql.jdbc.Driver");