代码在本来的页面能用的,但我想在一个页面专门做数据库连接,于是新建了个文件,把代码复制进去,然后在查询那里设了个方法,希望能在一个页面实现数据库的增删改,到时要用到这些方法时可以直接调用方法,使代码可以重用,结果一直报错,不知是怎么错的。
<%@ 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=?");

解决方案 »

  1.   

    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:yifu","sa","123456");
    }catch(Exception e)
    {
    out.print(e.getMessage());
    }
    将这个写到你的chai方法中试试
      

  2.   

    这是申明了。
    public boolean chai(String name,String pwd)//错误就在这一行。
    在jsp中不能这样写。
    你要放到<%!   %>的里面
      

  3.   

    在jsp里写方法要在<% 后面加“!”号,不过不建议这么写,最好写在java代码里!