2016-11-30 9 views
0

私は、JSPを介してWebアプリケーションによってデータベースから特定の値を取得しようとしています。 しかし、NullpointerExceptionが原因で、常に内部サーバーに障害が発生します。 は、これらは私のファイルです:JSPデータベースからデータを選択

  1. ファイル:

    <%@ page import="java.sql.*" %> 
        <%@ page contentType="text/html" pageEncoding="UTF-8"%> 
        <% Class.forName("com.mysql.jdbc.Driver"); %> 
    <!DOCTYPE html> 
        <html> 
         <head> 
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Webformular</title> 
        </head> 
        <body> 
    <h1>Webformular</h1> 
    <%! 
        public class Actor { 
        String url = "jdbc:mysql://localhost:3306/sakila"; 
        String Benutzername = "root"; 
        String Passwort = ""; 
    
        Connection con = null; 
        PreparedStatement selectActors = null; 
        ResultSet resultSet = null; 
    
        public Actor(){ 
         try { 
    
    
         con = DriverManager.getConnection(url, Benutzername, Passwort); 
         selectActors = con.prepareStatement("Select a.first_name,a.last_name, c.title" 
         + "FROM actor a, film_actor b, film c" 
         +"WHERE a.first_name= ?" 
         +"AND a.last_name = ?" 
         + "AND a.actor_id = b.actor_id" 
         +"AND b.film_id = c.film_id"); 
         } catch (SQLException e){ 
          e.printStackTrace(); 
         } 
        } 
    
        public ResultSet getActors(String first, String last){ 
    
         try { 
          selectActors.setString(1,first); 
          selectActors.setString(2,last); 
         resultSet = selectActors.executeQuery(); 
         } catch (SQLException e){ 
          e.printStackTrace(); 
         } 
    
    
        return resultSet; 
    }} 
    %> 
    <% 
    String firstName = new String(); 
    String lastName = new String(); 
    
    if (request.getParameter("first") !=null) { 
        firstName = request.getParameter("first"); 
    
    } 
    if (request.getParameter("last") !=null) { 
        lastName = request.getParameter("last"); 
    
    } 
    
    Actor actor = new Actor(); 
    ResultSet actors = actor.getActors(firstName, lastName); 
    
    %> 
    <table border="1"> 
    
        <tbody> 
         <tr> 
          <td>First Name</td> 
          <td>Last Name</td> 
          <td>Title</td> 
         </tr> 
         <%while(actors.next()) { %> 
         <tr> 
          <td><%= actors.getString("first_name") %></td> 
          <td><%= actors.getString("last_name") %></td> 
          <td><%= actors.getString("title") %></td> 
         </tr> 
         <% } %> 
        </tbody> 
    </table> 
    
    
        </body> 
    </html> 
    
  2. ファイル:

    <%@page contentType="text/html" pageEncoding="UTF-8"%> 
         <!DOCTYPE html> 
        <html> 
        <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Spezifische Daten auswerten</title> 
        </head> 
         <body> 
         <h1>Spezifische Daten auswerten</h1> 
         <form name="Formular" action="anzeige.jsp" method="POST"> 
    
    <table border="1"> 
    
        <tbody> 
         <tr> 
          <td>First Name:</td> 
          <td><input type="text" name="first" value="" size="50" /></td> 
         </tr> 
         <tr> 
          <td>Last Name:</td> 
          <td><input type="text" name="last" value="" size="50" /></td> 
         </tr> 
        </tbody> 
    </table> 
    
    <input type="submit" value="Weiter" name="weiter" /> 
    <input type="reset" value="Löschen" name="löschen" /> 
         </form> 
        </body> 
        </html> 
    

誰もが前に、その問題が発生したんや私を助けるかもしれませんか? 私は既にいくつかの研究を行い、いくつかのバグを修正しましたが、展開に何も変わりませんでした。最初のファイルは常にデプロイされますが、データを取得するときはいつでもNullpointerExceptionが表示されます。

+0

[NullPointerExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) – AxelH

+0

_内部サーバーはNullpointerException_のために失敗します。また、スタックトレースはどこですか?何かが、ActorのコンストラクタがExceptionをキャッチすると、単純に無視します。このようには動作しないでください。インスタンスにResultsetを格納せず、宣言し、読み込み、閉じます。 – AxelH

答えて

0

あなたのSQLは、これはおそらく、あなたが例外をキャッチしているので、実行を継続するのprepareStatement()またはのexecuteQuery()が失敗することが、あなたのJSPの原因となります

+"WHERE a.first_name= ?"+"AND a.last_name = ?"+"AND a.actor_id = b.actor_id" 

の前にスペースなしでAND演算子を連結されており、スタックトレースをWebサーバーのログに出力します。

その後、selectActorsまたはresultsetのいずれかがnullになり、使用しようとするとすぐにNullPointerExceptionが発生します。

だから:あなたのSQLに疑問符の後ろにスペースを入れてみてください。

+0

ありがとうございました!それはトリッキーな間違いでしたが、将来のイベントについて知っておいてよかったです。次回は、私は何が起こるか見るので、キャッチの間に何かを試してみます:) –

関連する問題