错误:变量conn已在方法_jspService中定义(HttpServletRequest,HttpServletResponse)

时间:2017-02-23 12:07:36

标签: jsp

我是jsp的新手。我正在尝试使用jsp创建一个网站,但无法弄清楚如何解决此错误。我在互联网上搜索但无法解决。这是我的代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login As</title>
    </head>
    <body>
        <%@ page import = "java.io.*, java.util.*" %>

    <%@ page import = "javax.servlet.*" %>

    <%@ page import = "java.sql.*, javax.sql.*" %>

        <%
           String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
           String DB_URL = "jdbc:mysql://localhost:3306/cvproject";

   //  Database credentials
           String USER = "root";
           String PASS = "admin";
           Connection conn = null;
           PreparedStatement ps1 = null;

           String uname=request.getParameter("username");
           String uspass=request.getParameter("password");

                  String t = "";
       int a=0;

        try
      {
          //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      ps1 = conn.prepareStatement("select * from user_record where uid=? and pwd=?");
      ps1.setString(1, uname);
      ps1.setString(2, uspass);
      //ps1.setString(3, type);
      ResultSet rs = ps1.executeQuery();
      if(rs.next())
      {
        System.out.println("success");
        a=1;
        t = rs.getString("type");
        System.out.println("type="+t);
         }
      else
      {
        System.out.println("Error occurred");
      }
      if(a==1)
      {  if("A".equals(t))
        {%>
        <%@ include file="admin.jsp" %>
        <%}
      else if("F".equals(t))
        {%>
        <%@ include file="faculty.jsp" %>
        <%}
      else if("S".equals(t))
        {%>
        <%@ include file="student.jsp" %>
        <%}
      else
        {%>
        <%@ include file="error.jsp" %>
        <%}
      }
          rs.close();
          ps1.close();
          conn.close();
      }
      catch(SQLException se){

      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){

      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(ps1!=null)
            ps1.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");

    %>        
    </body>
</html>

问题: 之前我曾经将任何字符串名称分配给JDBC_DRIVER,DB_URL,USER和PASS以及任何连接变量conn的名称,我曾经在_jsp服务方法中定义了变量。 现在它甚至没有编译,这是Netbeans的输出屏幕:

ant -f "D:\\4th year project and presentations\\project4th year main\\Mithapton" -Dnb.internal.action.name=compile.single -DforceRedeploy=false -Djavac.jsp.includes=org/apache/jsp/student_jsp.java "-Djsp.includes=D:\\4th year project and presentations\\project4th year main\\Mithapton\\build\\web\\student.jsp" "-Dbrowser.context=D:\\4th year project and presentations\\project4th year main\\Mithapton\\web\\student.jsp" compile-single-jsp
compile-single-jsp:
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
library-inclusion-in-archive:
library-inclusion-in-manifest:
compile:
Compiling 2 source files to D:\4th year project and presentations\project4th year main\Mithapton\build\generated\classes
D:\4th year project and presentations\project4th year main\Mithapton\build\generated\src\org\apache\jsp\login_jsp.java:198: error: variable JDBC_DRIVER is already defined in method _jspService(HttpServletRequest,HttpServletResponse)
           String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
                  ^
D:\4th year project and presentations\project4th year main\Mithapton\build\generated\src\org\apache\jsp\login_jsp.java:199: error: variable DB_URL is already defined in method _jspService(HttpServletRequest,HttpServletResponse)
           String DB_URL = "jdbc:mysql://localhost:3306/cvproject";
                  ^
D:\4th year project and presentations\project4th year main\Mithapton\build\generated\src\org\apache\jsp\login_jsp.java:202: error: variable USER is already defined in method _jspService(HttpServletRequest,HttpServletResponse)
           String USER = "root";
                  ^
D:\4th year project and presentations\project4th year main\Mithapton\build\generated\src\org\apache\jsp\login_jsp.java:203: error: variable PASS is already defined in method _jspService(HttpServletRequest,HttpServletResponse)
           String PASS = "admin";
                  ^
D:\4th year project and presentations\project4th year main\Mithapton\build\generated\src\org\apache\jsp\login_jsp.java:204: error: variable conn is already defined in method _jspService(HttpServletRequest,HttpServletResponse)
           Connection conn = null;
                      ^
D:\4th year project and presentations\project4th year main\Mithapton\build\generated\src\org\apache\jsp\login_jsp.java:221: error: variable rs is already defined in method _jspService(HttpServletRequest,HttpServletResponse)
      ResultSet rs = stmt.executeQuery(sql);
                ^
6 errors
D:\4th year project and presentations\project4th year main\Mithapton\nbproject\build-impl.xml:986: The following error occurred while executing this line:
D:\4th year project and presentations\project4th year main\Mithapton\nbproject\build-impl.xml:978: The following error occurred while executing this line:
D:\4th year project and presentations\project4th year main\Mithapton\nbproject\build-impl.xml:321: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)

我尝试了许多不同的字符串名称,但都是徒劳的。任何帮助都将受到高度赞赏。非常感谢....

1 个答案:

答案 0 :(得分:0)

上述问题的答案可以通过以下链接中给出的方法解决:

https://stackoverflow.com/a/38699851/6577453