日期:2014-05-17  浏览次数:20677 次

jsp数据库连接出错。
代码在本来的页面能用的,但我想在一个页面专门做数据库连接,于是新建了个文件,把代码复制进去,然后在查询那里设了个方法,希望能在一个页面实现数据库的增删改,到时要用到这些方法时可以直接调用方法,使代码可以重用,结果一直报错,不知是怎么错的。
<%@ page language="java" import="java.sql.*,java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>数据库连接</title>

</head>
<body>
<%
out.print("1<br>");
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:yifu","sa","123456");
}catch(Exception e)
{
out.print(e.getMessage());
}
public boolean chai(String name,String pwd)//错误就在这一行。
{
try{
ps=con.prepareStatement("use YIFU;select * from users where userMing=? and userPwd=?");
ps.setString(1,name);
ps.setString(2,pwd);
rs=ps.executeQuery();
if(rs.next())
{
return true;
}else
{
return false;
}

rs.close();
ps.close();
con.close();
}catch(Exception e)
{
out.print(e.getMessage());
return false;
}

}
 %>
</body>
</html>
给出的错误信息有很多,我选些出来。
HTTP Status 500 - Unable to compile class for JSP: An error occurred at line: 20 in the jsp file: 

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 20 in the jsp file: /dataBase.jsp
Illegal modifier for the variable chai; only final is permitted
17: {
18: out.print(e.getMessage());
19: }
20: public boolean chai(String name,String pwd)
21: {
22: try{
23: ps=con.prepareStatement("use YIFU;select * from users where userMing=? and userPwd=?");

我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.aiyiweb.com/j2ee/2308.html