简体   繁体   中英

Database Connectivity my with EJB and JSP Error with GlassFish Server

I am a newbie in Java EE development. I have created a new Enterprise Application Project that contains two Modules namely; TestDbEJB and TestDbWeb.

In TestDbEJB, I have created a package book with two classes Book and BookManager. The following codes are as follows:

Book

package book;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Book {

    private int isbn;
    private String title;

    @Id
    public int getIsbn() {
        return isbn;
    }

    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

}

BookManager.java

 package book;
 import java.sql.*;
 import java.util.ArrayList;
 public class BookManager {

String url = "jdbc:mysql://localhost:3306/Esd";
String user = "root";
String password = "root";

public int createBook(int isbn, String title) throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);

String insert = "INSERT INTO book VALUES (?,?)";
PreparedStatement ptt = con.prepareStatement(insert);
ptt.setInt(1, isbn);
ptt.setString(2, title);

int ins = ptt.executeUpdate();

con.close();
ptt.close();

return ins;
}

  public ArrayList<Book> getAllBooks() throws SQLException,     ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);

String sqlSelect = "SELECT * from book";
Statement stt = con.createStatement();
ResultSet rs = stt.executeQuery(sqlSelect);
ArrayList<Book> Books = new ArrayList<Book>();

while(rs.next()){
    int isbn = rs.getInt("isbn");
    String title = rs.getString("title");
    Book b1 = new Book();
    b1.setIsbn(isbn);
    b1.setTitle(title);
    Books.add(b1);
}

con.close();
stt.close();
rs.close();

return Books;
 }

}

Now in my TestDbWeb, I have created a JSP file - addBook.jsp.

addBook.jsp

        <jsp:useBean id = "bm" class="book.BookManager" scope="session"/>

    <%
    try{
        int isbn = Integer.parseInt(request.getParameter("isbn"));
        String title = request.getParameter("title");

        bm.createBook(isbn, title);
    }catch(Exception e){

    }

    %>

    <h1> Welcome to ABC Library</h1>

    <form>
    <table>
        <tr>
            <td> Enter Details </td>
            <td><input type="text" name="isbn"></td>
            <td><input type="text" name="isbn"></td>
            <td><input type="submit" name="add" value="add"></td>
    </tr>
    </table>
</form> 

Now the real issue is that when I run the application, either a dialog box is displayed with the following errors:

cannot Deploy TestDb deploy is failing=Error occurred during deployment: Exception while deploying the app [TestDb] : Invalid ejb jar [TestDbEJB.jar]: it contains zero ejb.

Note:

  1. A valid ejb jar requires at least one session, entity (1.x/2.x style), or message-driven bean.

  2. EJB3 entity beans (@Entity) are POJOs and please package them as library jar.

  3. If the jar file contains valid EJBs which are annotated with EJB component level annotations (@Stateless, @Stateful, @MessageDriven, @Singleton), please check server.log to see whether the annotations were processed properly.. Please see server.log for more details.

OR this error appears: cannot Deploy TestDb deploy is failing=Application with name [TestDb] is not deployed

Also note that previously, when the above errors were not shown. But on my browser I get the error "Resouce Not Found"

Your help would be very much appreciated. Since my search on google has not helped to resolve this issue on my own. Thanks & Regards..

You cannot reach your application since it is not deployed yet to Glassfish. And your TestDbEJB doesn't contain an EJB bean, Glassfish refuses to recognize it as EJB module. Why don't you try to annotate your BookManager.java with @Stateless?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM