0

I am trying to connect my database in a java file and then use that java file inside a jsp file to show the contents of the database but i keep getting multiple annotations error.

Java File

package library;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Book {
public ResultSet database()throws Exception
{
    final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    final String DB_URL = "jdbc:mysql://localhost/Library";

    final String USER = "root";
    final String PASS = "asdfghjkl";

    Connection conn = null;
    ResultSet rs = null;

    Statement stmt = null;
    try
    {
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL,USER,PASS);


        stmt = conn.createStatement();

        String query = "SELECT * FROM books";


        rs = stmt.executeQuery(query);

    }
    catch(Exception e)
    {

    }
    rs.close();
    stmt.close();
    conn.close();

    return rs;
}
}

Jsp File

<%@page import="library.Book" %>
<%@page import="java.sql.ResultSet" %>
<html>
<head>

</head>
<%
ResultSet rs = database();
%>
<body>

</body>
</html>

The error is :

Multiple annotations found at this line: - The method database() is undefined for the type __2F_Library_20_Management_2F_WebContent_2F_Books_2E_jsp - The method database() is undefined for the type __2F_Library_20_Management_2F_WebContent_2F_Books_2E_jsp

2 Answers2

0

The most fundamental issue is that you are trying to database() with out calling it on an instance of Book.

In your jsp you may have more luck if you try something like:

<%
    Book book = new Book();
    ResultSet rs = book.database();
%>

At the least it will fix your current error.

Additionally you should never swallow an exception, at the very least it should be printed.

MartinByers
  • 1,240
  • 1
  • 9
  • 15
  • You will have a different error. "database()" can only be called on an instance of Book. From the StackTrace you provided it is clear that "database()" was not getting called because of the incorrect code in jsp. If you implement this answer, either: you will have a different error, or it will work. From your comment I can see that you will have an error, what error is it? – MartinByers Jul 13 '17 at 09:57
0

it works fine me try this way

public ResultSet database() throws ClassNotFoundException, SQLException{
    Class.forName("com.mysql.jdbc.Driver");
    connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/springdemo", "root", "devbinod");
    Statement smt=connection.createStatement();
    ResultSet resultSet=smt.executeQuery("select * from Student");

    return resultSet;

}

you call .java file inside jsp this way

  <%
    Book b=new Book();
    ResultSet rs=b.database();

    %>
Binod Pant
  • 332
  • 1
  • 2
  • 16