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>

// These are the following errors in the functions which are in bold lines.

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
chinna_82
  • 6,353
  • 17
  • 79
  • 134
bunny
  • 27
  • 2

1 Answers1

0

When you use the <% syntax it means you're using a scriptlet, as you may know all the jsp's pages are translated and compiled to a servlet, when you use a scriptlet this code is put inside the service method of the servlet, if you use a scriptlet to define a new method is like you are creating a new method inside an another method, so you need to use the declaration syntax <%! this allows to declare variables or methods in the translated servlet class the code goes outside the service method, and put this declaration after the page directive so:

<%@ 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>
Cesar Loachamin
  • 2,740
  • 4
  • 25
  • 33