org.apache.jasper.JasperException: Unable to compile class for JSP<%@ page language="java" import="java.sql.*" pageEncoding="GBK"%>
<html>
<title>数据库连接</title>
<head>
</head>
<body>
<%!
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection
("jdbc:sqlserver://127.0.0.1:1433;databaseName=mydata","sa","110");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select top(3) * from users");
%>
<table border="1">
<%
while(rs.next()) {
%>
<tr>
<td><%=rs.getInt(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getInt(5)%></td>
</tr>
<%
}
st.close();
rs.close();
conn.close();
%>
</table>
</body>
</html>
这是源代码
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 8 in the jsp file: /users.jsp
Syntax error on token(s), misplaced construct(s)
5: </head>
6: <body>
7: <%!
8: Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
9: Connection conn = DriverManager.getConnection
10: ("jdbc:sqlserver://127.0.0.1:1433;databaseName=mydata","sa","110");
11: Statement st = conn.createStatement();
这是错误提示,但用MVC模式时可以连上数据库,而这种方式不能,不知为什么,求高手支招,谢谢!
------解决方案--------------------
<%!
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
改为
<%
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.aiyiweb.com/j2ee/2308.html