I'm trying to get certain values out of my Database by a web application via JSP. But it always gives me an Internal Server Fail because of a NullpointerException. These are my files:
File:
<%@ 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>
File:
<%@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>
Does anyone has encountered that Problem before and might help me? I already did some Research and fixed certain bugs but nothing changed in the deployment. My first File always deploys but whenever i want to retrieve the data the NullpointerException appears.