JSF / JDBC - 数据库

时间:2016-12-30 22:22:08

标签: java mysql jsf jdbc glassfish

首先我要说的是,我是Java EE世界的新手。

我试图从MySQL数据库中读取数据,我的程序会多次执行此操作, 我不需要这个。我正在开发在GlassFish 4.1上运行的简单JSF应用程序。

这是我的main.xhtml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
          xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        ...
    </h:head>

    <h:body>

        <div class="main-text">
            <p>Helloww, #{user.username}, have a nice day))</p>
        </div>

        <div class="books">
            <h:dataTable value="#{library.getBooks(false)}" var = "b">

                <h:column>
                    <f:facet name="header">
                        Book ID
                    </f:facet>
                    #{b.getId()}
                </h:column>

                <h:column>
                    <f:facet name="header">
                        Book ISBN
                    </f:facet>
                    #{b.getIsbn()}
                </h:column>

                <h:column>
                    <f:facet name="header">
                        Book name
                    </f:facet>
                    #{b.getName()}
                </h:column>

            </h:dataTable>
        </div>

        <p>#{library.getBooks(true).size()}</p>

        <p>Max memory: #{libraryChecker.maxMemory}</p>
        <p>Used memory: #{libraryChecker.currentMemory}</p>

    </h:body>

    </html>

<!-- end snippet -->

我使用的豆子:

  // Book.java
@ManagedBean
@SessionScoped
public class Book {
    private int id;
    private String name;
    private String isbn;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

}
//Library.java
@ManagedBean(name = "library")
@SessionScoped
public class Library {
    private static final String SQL_GET_ALL_BOOKS = "SELECT * FROM book";

    private Set<Book> books;

    private InitialContext icon;
    private DataSource ds;

    public Library() {
        if (books == null)
            books = new LinkedHashSet<Book>();
        try {
            icon = new InitialContext();
            ds = (DataSource) icon.lookup("jdbc/library");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    public Set<Book> getBooks(boolean ifOnlyGet) {
        if (!ifOnlyGet)
            readBooks();

        return books;
    }


    private void readBooks() {
        try {
            Connection con = ds.getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(SQL_GET_ALL_BOOKS);

            System.out.println("info: before while");
            while (rs.next()) {
                Book book = new Book();

                book.setId(rs.getInt("id"));
                book.setName(rs.getString("name"));
                book.setIsbn(rs.getString("isbn"));

                books.add(book);

                System.out.println(rs.getInt("id"));
            }

            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

在数据库表book中显示like

当我重新加载页面时,我得到this

这是GlassFish日志:

    [2016-12-30T23:30:49.722+0200] [glassfish 4.1] [INFO] [] [javax.enterprise.system.core] [tid: _ThreadID=42 _ThreadName=admin-listener(4)] [timeMillis: 1483133449722] [levelValue: 800] [[
  IMVN-WEB_demo1 was successfully deployed in 4 512 milliseconds.]]

[2016-12-30T23:30:51.843+0200] [glassfish 4.1] [INFO] [jsf.config.listener.version] [javax.enterprise.resource.webcontainer.jsf.config] [tid: _ThreadID=91 _ThreadName=Thread-21] [timeMillis: 1483133451843] [levelValue: 800] [[
  Initializing Mojarra 2.2.7 ( 20140610-1547 https://svn.java.net/svn/mojarra~svn/tags/2.2.7@13362) for context '']]

[2016-12-30T23:30:53.514+0200] [glassfish 4.1] [INFO] [AS-WEB-GLUE-00172] [javax.enterprise.web] [tid: _ThreadID=91 _ThreadName=Thread-21] [timeMillis: 1483133453514] [levelValue: 800] [[
  Loading application [__admingui] at [/]]]

[2016-12-30T23:30:53.516+0200] [glassfish 4.1] [INFO] [NCLS-CORE-00022] [javax.enterprise.system.core] [tid: _ThreadID=91 _ThreadName=Thread-21] [timeMillis: 1483133453516] [levelValue: 800] [[
  Loading application __admingui done in 5 049 ms]]

[2016-12-30T23:31:14.132+0200] [glassfish 4.1] [INFO] [] [javax.enterprise.system.tools.deployment.dol] [tid: _ThreadID=26 _ThreadName=http-listener-1(2)] [timeMillis: 1483133474132] [levelValue: 800] [[
  visiting unvisited references]]

[2016-12-30T23:31:17.500+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477500] [levelValue: 800] [[
  info: before while]]

[2016-12-30T23:31:17.502+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477502] [levelValue: 800] [[
  3]]

[2016-12-30T23:31:17.502+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477502] [levelValue: 800] [[
  4]]

[2016-12-30T23:31:17.502+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477502] [levelValue: 800] [[
  5]]

[2016-12-30T23:31:17.504+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477504] [levelValue: 800] [[
  6]]

[2016-12-30T23:31:17.505+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477505] [levelValue: 800] [[
  7]]

[2016-12-30T23:31:17.506+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477506] [levelValue: 800] [[
  9]]

[2016-12-30T23:31:17.506+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477506] [levelValue: 800] [[
  10]]

[2016-12-30T23:31:17.507+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477507] [levelValue: 800] [[
  12]]

[2016-12-30T23:31:17.507+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133477507] [levelValue: 800] [[
  13]]

[2016-12-30T23:31:19.226+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479226] [levelValue: 800] [[
  info: before while]]

[2016-12-30T23:31:19.227+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479227] [levelValue: 800] [[
  3]]

[2016-12-30T23:31:19.227+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479227] [levelValue: 800] [[
  4]]

[2016-12-30T23:31:19.227+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479227] [levelValue: 800] [[
  5]]

[2016-12-30T23:31:19.228+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479228] [levelValue: 800] [[
  6]]

[2016-12-30T23:31:19.228+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479228] [levelValue: 800] [[
  7]]

[2016-12-30T23:31:19.228+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479228] [levelValue: 800] [[
  9]]

[2016-12-30T23:31:19.228+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479228] [levelValue: 800] [[
  10]]

[2016-12-30T23:31:19.228+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479228] [levelValue: 800] [[
  12]]

[2016-12-30T23:31:19.228+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479228] [levelValue: 800] [[
  13]]

[2016-12-30T23:31:19.734+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479734] [levelValue: 800] [[
  info: before while]]

[2016-12-30T23:31:19.735+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479735] [levelValue: 800] [[
  3]]

[2016-12-30T23:31:19.735+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479735] [levelValue: 800] [[
  4]]

[2016-12-30T23:31:19.735+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479735] [levelValue: 800] [[
  5]]

[2016-12-30T23:31:19.735+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479735] [levelValue: 800] [[
  6]]

[2016-12-30T23:31:19.735+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479735] [levelValue: 800] [[
  7]]

[2016-12-30T23:31:19.735+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479735] [levelValue: 800] [[
  9]]

[2016-12-30T23:31:19.736+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479736] [levelValue: 800] [[
  10]]

[2016-12-30T23:31:19.740+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479740] [levelValue: 800] [[
  12]]

[2016-12-30T23:31:19.740+0200] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=26 _ThreadName=Thread-8] [timeMillis: 1483133479740] [levelValue: 800] [[
  13]]

首先,我无法理解为什么方法 readBooks()被调用三次。
其次,我将所有书籍存储在一个集合中,为什么它会保存重复项?

0 个答案:

没有答案