2017-02-23 35 views
0

我是jsp中的新成員。我正在嘗試使用jsp製作一個網站,但無法弄清楚如何解決此錯誤。我在互聯網上搜索,但無法解決它。這裏是我的代碼:錯誤:變量conn已在方法_jspService(HttpServletRequest,HttpServletResponse)中定義

<%@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和任何名義向連接變量康恩,我用來獲取變量是在已定義_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) 

我試着用很多不同的字符串名稱,但一切都是徒勞。任何幫助將是非常appreciated.Thank你這麼多....

回答

相關問題