2014-01-23 1 views
0
<%@ page import="java.sql.*"%> 
<%@ page import="javax.sql.*"%> 
<% 
public static **Connection connectToDB()** 
    { 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      return DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll", "root",""); 

     } 
     catch(Exception e) 
     { 
      System.out.println("Error: "+e);  
     } 
    } 

    public static boolean **closeConnection(Connection c)** 
    { 
     try 
     { 
     c.close(); 
     return true; 
     } 
     catch (Exception e) 
     { 
     return false; 
     } 
    } 
%> 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<% 
Connection c=new connectToDB(); 
out.print(c); 
close(c); 
%> 
</body> 
</html> 

// 다음은 굵은 글씨로 표시된 함수에서 나타나는 다음과 같은 오류입니다.JSP를 사용하여 JDBC - MYSQL을 연결할 때 오류가 발생했습니다.

Multiple annotations found at this line: 
    - Line breakpoint:index.jsp [line: 4] 
    - Syntax error, insert "enum Identifier" to complete 
    EnumHeaderName 
    - Syntax error, insert "EnumBody" to complete BlockStatement 
    - Syntax error on token "Connection", @ expected 


Multiple annotations found at this line: 
    - Syntax error on token ")", ; 
    expected 
    - Syntax error on token "(", ; 
    expected 
+0

가 읽어 보시기 바랍니다 [JSP - 파일에서 자바 코드를 방지하는 방법?] (http://stackoverflow.com/q/3177733/1031945) 위의 오류를 해결하기 전에 –

답변

0

당신은 당신이 모든 JSP의 페이지가 서블릿으로 변환하고 컴파일다시피 당신이 스크립틀릿을 사용하는 경우이 코드는 내부에 넣고, 스크립틀릿을 사용하는 의미 <퍼센트 구문을 사용하는 경우 서블릿의 서비스 메소드를 사용하는 경우, 새 메소드를 정의하기 위해 스크립틀릿을 사용하는 경우 다른 메소드 내부에 새 메소드를 작성하는 것과 같으므로 선언 구문 < %를 사용해야합니다. 이 코드는 서비스 방법 밖에 간다 번역 된 서블릿 클래스에서 변수 나 메소드를 선언, 그래서 페이지 지시문 후이 선언을 넣을 수 있습니다 :

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


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<%! 
public static **Connection connectToDB()** 
    { 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      return DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll", "root",""); 

     } 
     catch(Exception e) 
     { 
      System.out.println("Error: "+e);  
     } 
    } 

    public static boolean **closeConnection(Connection c)** 
    { 
     try 
     { 
     c.close(); 
     return true; 
     } 
     catch (Exception e) 
     { 
     return false; 
     } 
    } 
%> 
<% 
Connection c=new connectToDB(); 
out.print(c); 
close(c); 
%> 
</body> 
</html> 
관련 문제