こんにちは。
次の仕事でJavaのJSFを使用する為、こちらのサイトを使って図書検索システムの作成を行っていますが、以下のエラーを出してしまい、検索結果が表示出来ないので、皆様からご教示を頂きたく、質問させて頂きました。
netbeansを使って、JSFにMySQLを接続するには、どうすればよろしいでしょうか。
#開発環境
OS:Windows10 home
Java:15.0.2、jre1.8.0
ドライバ:mysql-connector-java-8.0.25.jar
IDE:Netbeans
DB:MySQL
HTTP Status 500 - Internal Server Error type Exception report messageInternal Server Error descriptionThe server encountered an internal error that prevented it from fulfilling this request. exception javax.servlet.ServletException: Target Unreachable, identifier 'BookSearcher' resolved to null root cause javax.el.PropertyNotFoundException: Target Unreachable, identifier 'BookSearcher' resolved to null note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.
#ソースコード
##ManagedBean
BookSearcher
1import java.util.List; 2import java.util.ArrayList; 3import java.sql.*; 4import javax.sql.*; 5import javax.faces.context.FacesContext; 6import javax.faces.el.ValueBinding; 7 8public class BookSearcher { 9 10 private String word = ""; 11 private String id = ""; 12 private List list = null; 13 private BookData book = null; 14 15 public void setWord(String word) { 16 this.word = word; 17 } 18 19 public String getWord() { 20 return word; 21 } 22 23 public List getBookList() { 24 return list; 25 } 26 27 public BookData getBookData() { 28 return book; 29 } 30 31 public String searchBooks() { 32 searchBooks(word); 33 return "success"; 34 } 35 36 private void searchBooks(String word) { 37 list = new ArrayList(); 38 try { 39 Class.forName("org.gjt.mm.mysql.Driver"); 40 String url = "jdbc:mysql://localhost/library"; 41 Connection con = DriverManager.getConnection(url, "root", "chhagane"); 42 43 String selectStatement = 44 "select * " + 45 "from books where ndc like ? " + 46 "or tyosya_hyouji like ? " + 47 "or id like ? " + 48 "or title like ? " + 49 "or author like ? " + 50 "or publisher like ? "; 51 PreparedStatement prepStmt = 52 con.prepareStatement(selectStatement); 53 prepStmt.setString(1, appendPercent(word)); 54 prepStmt.setString(2, appendPercent(word)); 55 prepStmt.setString(3, appendPercent(word)); 56 prepStmt.setString(4, appendPercent(word)); 57 prepStmt.setString(5, appendPercent(word)); 58 prepStmt.setString(6, appendPercent(word)); 59 60 ResultSet rs = prepStmt.executeQuery(); 61 while (rs.next()) { 62 BookData book = new BookData(); 63 book.setNdc(rs.getString("ndc")); 64 book.setTyosya_hyouji(rs.getString("tyosya_hyouji")); 65 book.setId(rs.getString("id")); 66 book.setTitle(rs.getString("title")); 67 book.setAuthor(rs.getString("author")); 68 book.setPublisher(rs.getString("publisher")); 69 list.add(book); 70 } 71 prepStmt.close(); 72 } catch (ClassNotFoundException e) { 73 e.printStackTrace(); 74 } catch (SQLException e) { 75 e.printStackTrace(); 76 } 77 } 78 79 private String appendPercent(String from) { 80 StringBuffer to = new StringBuffer(); 81 to.append("%"); 82 to.append(from); 83 to.append("%"); 84 85 return new String(to); 86 } 87 88}
BookData
1import javax.inject.Named; 2import javax.enterprise.context.Dependent; 3 4import java.io.Serializable; 5 6@Named(value = "bookData") 7@Dependent 8public class BookData { 9 private String ndc =""; 10 private String tyosya_hyouji =""; 11 private String id =""; 12 private String title =""; 13 private String author =""; 14 private String publisher =""; 15 16 public String getNdc(){ 17 return ndc; 18 } 19 20 public void setNdc(String ndc){ 21 this.ndc=ndc; 22 } 23 public String getTyosya_hyouji(){ 24 return tyosya_hyouji; 25 } 26 27 public void setTyosya_hyouji(String tyosya_hyouji){ 28 this.tyosya_hyouji=tyosya_hyouji; 29 } 30 public String getId(){ 31 return id; 32 } 33 34 public void setId(String id){ 35 this.id=id; 36 } 37 public String getTitle(){ 38 return title; 39 } 40 41 public void setTitle(String title){ 42 this.title=title; 43 } 44 public String getAuthor(){ 45 return author; 46 } 47 48 public void setAuthor(String author){ 49 this.author=author; 50 } 51 public String getPubliser(){ 52 return publisher; 53 } 54 55 public void setPublisher(String publisher){ 56 this.publisher=publisher; 57 } 58} 59
##JSPファイル
search
1<%@ page contentType="text/html; charset=Shift_JIS" %> 2<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 3<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 4 5<html> 6<head> 7<link href="style.css" type="text/css" rel="stylesheet" /> 8<title>図書検索</title> 9</head> 10<body> 11 12<h1>図書検索</h1> 13 14<hr /> 15 16<f:view> 17 <h:form id="searchForm"> 18 <h:inputText id="searchWord" value="#{BookSearcher.word}" /> 19 <h:commandButton id="submit" 20 action="#{BookSearcher.searchBooks}" value="Go!" /> 21 </h:form> 22</f:view> 23 24</body> 25</html>
list
1<%@ page contentType="text/html; charset=Shift_JIS" %> 2<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> 3<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> 4 5<!DOCTYPE html> 6<html> 7 <head> 8 <title>検索結果</title> 9 </head> 10 <body> 11 <h1>検索結果</h1> 12 13 <f:view> 14 <h:form id="listForm"> 15 <h:dataTable id="table" border="1" value="#{BookSearcher.booklist}" var="book"> 16 <h:column> 17 <f:facet name="header"> 18 <h:outputText value="タイトル"/> 19 </f:facet> 20 <h:outputText id="bookTitle" value="#{book.title}"/> 21 </h:column> 22 <h:column> 23 <f:facet name="header"> 24 <h:outputText value="著者"/> 25 </f:facet> 26 <h:outputText id="bookAuthor" value="#{book.author}"/> 27 </h:column> 28 </h:dataTable> 29 </h:form> 30 </f:view> 31 </body> 32</html> 33
##web.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name> JSF: library-search </display-name> <description> JSF: library-search </description> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/jsp/*</url-pattern> </servlet-mapping> </web-app>
#試したこと
①MySQLの接続確認と、DBとテーブルの指定を追加
②ソースコードの見直し
③ライブラリにMySQLのJDBCドライバの指定(mysql-connector-java-8.0.25-bin.jar)
④データベースからのエンティティクラスの作成
以上、拙い質問で大変恐縮ですが、宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/16 04:50
2021/08/18 08:10